LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make arrays in sh scripting? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-arrays-in-sh-scripting-900270/)

ameylimaye 08-30-2011 11:13 AM

How to make arrays in sh scripting?
 
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!

TB0ne 08-30-2011 12:20 PM

Quote:

Originally Posted by ameylimaye (Post 4457145)
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

ameylimaye 08-30-2011 04:30 PM

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.!

chrism01 08-30-2011 06:07 PM

You should read TB0ne's link; there are several examples and all the info you could want...
Try this stuff first, then ask qns.

TB0ne 08-30-2011 06:16 PM

Quote:

Originally Posted by ameylimaye (Post 4457378)
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?

ameylimaye 09-01-2011 12:01 AM

I have tried
1) script_contents=( $(cat "$0") )
2) arrayZ=( one two three four five five )

it says
error at line 1 : "(" unexpected

But thanks any ways.! i have found another way of doing is using "sed" command! :)
Thanks for the help.!

expert_vision 04-04-2012 07:19 AM

@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?

Anyway, how did you do it ameylimaye with sed?
Hope you follow the thread :(

TB0ne 04-04-2012 09:42 AM

Quote:

Originally Posted by expert_vision (Post 4644466)
@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?

expert_vision 04-04-2012 12:04 PM

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

but you can't create it like this:
Code:

for i in `seq 1 5`; do
array$i=$i
done

in sh shell.

grail 04-04-2012 12:39 PM

The point is, don't reopen, but instead create your own question and reference this one if need be.

TB0ne 04-04-2012 12:44 PM

Quote:

Originally Posted by expert_vision (Post 4644710)
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 (Post 4644710)
Sh lacks a lot of features that bash have .. like the ability to use arrays

...

expert_vision 04-04-2012 03:52 PM

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 preferred                      preferred 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.

expert_vision 04-05-2012 05:27 PM

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. :D

David the H. 04-06-2012 12:12 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.

http://mywiki.wooledge.org/Bashism

David the H. 04-06-2012 12:23 PM

Quote:

Originally Posted by expert_vision (Post 4645847)
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. :D


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.

Eval command and security issues:
http://mywiki.wooledge.org/BashFAQ/048

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.)


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