Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hey I am a newbie with shell scripting. Could anyone please help me with my doubt?
i am not able to make an array in sh scripting. I have tried many methods like below
array = (`cat site_list|tr '/n''' `)
#for accepting elements from a file.
#even
array = ($(<site_list))
please please give me some solution! :-\
Thank you.. in advance!
The word "question" and the word "doubt", do not mean the same things. And if you put in "linux shell script array example" into Google, you'll pull up over 600K hits...first one is this: http://tldp.org/LDP/abs/html/arrays.html
I am sorry!
Heres the question...
How do I accept the rows of a file as the elements of an array in "sh" Scripting?
In some link i read that sh script does not support arrays. Is that true?
and yes i have already checked the link you have shared. Thanks neways!
Sorry I was not able to put my question clearly.
I need to put the file elements as the array elements and then operate on each one. Is it possible? and how ? please tell.!
I am sorry!
Heres the question...
How do I accept the rows of a file as the elements of an array in "sh" Scripting?
In some link i read that sh script does not support arrays. Is that true?
and yes i have already checked the link you have shared. Thanks neways!
Sorry I was not able to put my question clearly.
I need to put the file elements as the array elements and then operate on each one. Is it possible? and how ? please tell.!
Yes it is possible...did you bother to read my first reply, where I gave you a link, with examples on how to do it?
@TB0ne YOU didn't read the OP's post. He asked how to use arrays in sh script, SH, S.H., SSSSSSHHHHH, not bash, can you see it know?
Yes, I saw it...now can you explain the difference between the two? Here's a hint: "/bin/sh" is typically a link to /bin/bash, /bin/ksh, or some other shell. On Solaris, the "sh" is for the Bourne shell...the port of Bourne on Linux is the Bourne Again SHell...that would be "bash".
Quote:
Anyway, how did you do it ameylimaye with sed?Hope you follow the thread
This thread has been closed since last year. Why reopen it?
What do you mean "explain the differences between the two" ? Sh lacks a lot of features that bash have .. like the ability to use arrays (myarray[$count]=<something>), and some linux distributions don't have bash, ksh, etc, they have only sh, especially the embedded linux firmwares.
So, due to compatibility issues, some would like to implement the script with sh shell.
Why I reopened the post? Because I still couldn't find a clear answer to this.
So far I found that you can view your array (array1 array2 ....) with:
Code:
for ((i=1; i<=n; i++)); do
eval echo "\$array$i"
done
What do you mean "explain the differences between the two" ? Sh lacks a lot of features that bash have .. like the ability to use arrays (myarray[$count]=<something>), and some linux distributions don't have bash, ksh, etc, they have only sh, especially the embedded linux firmwares.
Still a couple of problems with your post.
You admit that an (old) sh shell can't do arrays, yet you're still looking for a way to do it.
Which linux distro doesn't ship with bash?? Embedded linuxes are different, and I've not seen a 'full' Linux distro (Fedora, Mandriva, openSUSE, etc.)
ship without bash.
Quote:
So, due to compatibility issues, some would like to implement the script with sh shell.
Why I reopened the post? Because I still couldn't find a clear answer to this.
So far I found that you can view your array (array1 array2 ....) with:
Code:
for ((i=1; i<=n; i++)); do
eval echo "\$array$i"
done
but you can't create it like this:
Code:
for i in `seq 1 5`; do
array$i=$i
done
in sh shell.
If you can't create the array like you're wanting to, how *DID* you create it, and why can't you continue to create it like that? And going back to what you originally said:
Quote:
Originally Posted by expert_vision
Sh lacks a lot of features that bash have .. like the ability to use arrays
Ok ...
I use Oleg's firmware on a WL-500gPv2 router, that only has sh shell. If I attach a USB HDD to the router I can install bash through ipkg on the external HDD, which I did and the script works fine with bash. The thing is I would like to run the script without the HDD(i.e. using sh shell not bash), so that's why I'm interested in this.
Yes, as far as I could find, sh shell does not support arrays, BUT there are always workarounds. For example, sh does not support multi-threading but you can use screen or detach process from TTY with ./script &.
Actually, someone said is possible to simulate arrays here using environment variables (set -- "$space_separated_variables" and then echo $1 $2 ...), but it doesn't suit my needs, I need to edit each array's element separately (array$count=...). If I can do this than I can easily use them (var=`eval echo "\$array$i ..."`).
I could actually build the array like this:
Code:
not preferredpreferred but not possible yet
for count in `seq 1 n`;do for count in `seq 1 n`; do
if [ $count == 1 ];then array$count=....
array1=... done
if [ $count == 2 ];then
array2=...
...
but would prefer not to, because the array is pretty big.
Hope is more clear now.
Last edited by expert_vision; 04-04-2012 at 03:55 PM.
When you call a script with /bin/sh, it will be interpreted according to the posix specifications for shell scripting, whatever the actual interpreter used. Do note that this doesn't preclude the use of shell-specific commands in sh scripts, only that scripts written according to strict posix syntax will run as intended under any shell, on pretty much any OS.
Arrays are simply not defined in the posix standard. This means that what happens when a sh script contains one is up to the actual shell doing the interpreting. bash continues to support them, but if your system is set up to use dash, a shell built much more closely to posix-compliance (and without array support), you get an error.
This page details most of the main differences between bash and dash, and thus also most of the things you should watch out for when working with /bin/sh.
Apparently I'm not very clever, and someone else on the forum have pointed the obvious:
Code:
for count in `seq 1 n`; do
eval array$count=....
done
So, yeah .. you can create and work with arrays in sh shell.
Uggh... The use of eval should never be recommended, and you should only ever resort to it when you know exactly, and I mean exactly, what you are doing. Improper use of eval is one of the more severe security issues you can encounter in shell scripting.
Besides, this isn't really an "array", just an array simulation using indirect variable referencing. It doesn't provide all the benefits a true array, and comes with its own set of problems.
See here for discussion on the use of indirect variables, including the use of eval, and also the problems of using them in this kind of fashion: http://mywiki.wooledge.org/BashFAQ/006
The only real "array" you have in sh is the positional parameters ($@), as shown in the bashism page I gave above. If you can design your script so that you can free them up, then you can likely do what you want with them (note that it will likely require the use of eval to do so; in a good example of how to use it properly.)
Last edited by David the H.; 04-06-2012 at 12:33 PM.
Reason: minor rewording
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.