![]() ![]() In your case the main problem is in the redirection operator > (you probably now have files with names that are numbers in the working directory). stat -cs filename should return a string that looks like an integer. ![]() A string may look like an integer and it's enough. If you know your inputs won't need trimming, that bit is unnecessary. In bash / sh variables contain strings, there is no strict concept of integers. In these cases, the $ADDITIONAL_OPTIONS variable will start with a space. So you could use that to normalize your values. However, while Bash doesn't understand decimals, its builtin printf can format them. The only way I've come up with to reliably handle all options safely is thus: convert_array() -Dsome.property=foo" If your values are guaranteed to be in the same form and range, you can do string comparisons: if x > 0.70 then echo 'It's true' fi This will fail if x is '.8' (no leading zero), for example. My solution is overkill for the simple case above, but I wanted to document it for others looking for this solution, because it was non-trivial coming up with something that handled my use case. ![]() So, I needed to make a way to conditionally convert a scalar to an array, depending on whether the user's sourced script is declared in the new way or the old way. But, users are also allowed to set those variables themselves, optionally, with a file that is sourced on startup. I am in an unfortunate position where I have a long-standing shell script that is upgrading from internally using scalar variables to using arrays. ![]()
0 Comments
Leave a Reply. |