How to deal with bash[$style] arrays in a POSIX compliant script?
I've been considering trying to make a project I'm working on be POSIX compliant.
I've been trying to test it using the Dash shell and have seemed to come across the un-enjoyable fact that POSIX non-Bash shells do not support arrays. array[1]='hello' # doesn't work set -A array... # doesn't work. echo ${array[1]} # doesn't work. $(eval echo \${$svar[6]}) # bad substitution. etc.. If my script has made extensive use of arrays (and variable/array substitution FWIW), is it even practical or worthwhile to try to work around this array business? Is it even feasible? Any input on this would be nice, and also, if you can recommend or suggest another 100% POSIX compliant shell besides Ash or Dash (but not Korn) (just for my fiddling with) I'll be happy to hear of it. Sasha |
what kind of project are you working on? I suggest you skip programming in shells and use a programming language like Python (or Perl). you can be guaranteed that programming constructs like arrays, dictionaries etc works the same across different language versions. Personally, i dislike these POSIX compliant this and that stuff...
|
Indeed I tend to agree with ghostdog74, once you say you "make extensive use of arrays", you have to start wondering if bash is really what you should be using.
I'm pretty sure that the above will work in 'sh' or 'bash --posix', I've never really used the others, except ksh. |
In POSIX sh I've used newline delimited lists.. but you have to be very careful not to allow any items with newlines in or use some escaping mechanism. Or pray to $DIETY.
|
If its ok to mess up the enviroment you could:
Code:
n = 0 |
I believe I have come up with a viable solution. It works in Dash Shell, so I'm figuring it's fairly POSIXy, hopefully POSIXy enough to pass in a full POSIX shell (if Dash is less than 100% POSIX) and will verify later..
Since I made sort of a little tutorial on the subject, I'm going to post a new thread HERE: http://www.linuxquestions.org/questi...rings.-775622/ |
|
I still don't understand the solution on this
How to replace array[1]='hello' to work in posix shell? |
Methinks it was rather clear: this is a non-POSIX extension of bash, so it won't work. You should use an actual programming language such as perl/php/python/awk
|
All times are GMT -5. The time now is 12:34 AM. |