LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-30-2011, 11:13 AM   #1
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Rep: Reputation: Disabled
Unhappy 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!

Last edited by ameylimaye; 08-30-2011 at 11:17 AM.
 
Old 08-30-2011, 12:20 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ameylimaye View Post
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
 
Old 08-30-2011, 04:30 PM   #3
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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.!
 
Old 08-30-2011, 06:07 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You should read TB0ne's link; there are several examples and all the info you could want...
Try this stuff first, then ask qns.
 
Old 08-30-2011, 06:16 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ameylimaye View Post
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?
 
Old 09-01-2011, 12:01 AM   #6
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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.!
 
Old 04-04-2012, 07:19 AM   #7
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
@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
 
Old 04-04-2012, 09:42 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by expert_vision View Post
@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?
 
Old 04-04-2012, 12:04 PM   #9
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
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.
 
Old 04-04-2012, 12:39 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
The point is, don't reopen, but instead create your own question and reference this one if need be.
 
1 members found this post helpful.
Old 04-04-2012, 12:44 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by expert_vision View Post
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 View Post
Sh lacks a lot of features that bash have .. like the ability to use arrays
...
 
Old 04-04-2012, 03:52 PM   #12
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
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.

Last edited by expert_vision; 04-04-2012 at 03:55 PM.
 
Old 04-05-2012, 05:27 PM   #13
expert_vision
LQ Newbie
 
Registered: Mar 2011
Posts: 13

Rep: Reputation: 0
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.
 
Old 04-06-2012, 12:12 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 04-06-2012, 12:23 PM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by expert_vision View Post
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.

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

Last edited by David the H.; 04-06-2012 at 12:33 PM. Reason: minor rewording
 
1 members found this post helpful.
  


Reply



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
bash scripting - arrays and indirect referencing soulcurry Programming 7 04-21-2011 06:41 AM
[SOLVED] Bash Scripting - Using Arrays mrm5102 Linux - Newbie 4 04-05-2011 09:20 AM
Scripting & Arrays : How to fill an array with filenames which consist of spaces? klss Linux - Software 5 01-19-2009 12:16 AM
Can "Arrays" be used in shell scripting som_kurian Programming 6 12-05-2007 11:43 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration