LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-11-2009, 06:37 PM   #1
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Question 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
 
Old 12-11-2009, 07:30 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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...
 
Old 12-12-2009, 05:39 AM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
Old 12-12-2009, 02:39 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
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.
 
Old 12-12-2009, 02:45 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
If its ok to mess up the enviroment you could:
Code:
n = 0
X_$n = $n
n = $n + 1
X_$n = $n
I think that will work.
 
Old 12-14-2009, 02:48 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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/
 
Old 12-16-2009, 10:24 AM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Solved!

See here: http://www.linuxquestions.org/questi...52#post3793352

Sasha
 
  


Reply

Tags
array, bash, dash, posix, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POSIX compliant distributed file systems without using shared storage anon.addon Linux - Software 1 12-12-2009 09:54 AM
LXer: Demo features POSIX-compliant separation kernel, Linux LXer Syndicated Linux News 0 01-23-2007 11:54 PM
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
c programming compliant with posix ---HELP! v333k Programming 4 02-28-2005 03:08 PM
Open source, posix compliant, person renderer? Elomis Linux - Software 0 01-10-2005 04:21 PM


All times are GMT -5. The time now is 03:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration