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! |
Quote:
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.! |
You should read TB0ne's link; there are several examples and all the info you could want...
Try this stuff first, then ask qns. |
Quote:
|
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.! |
@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 :( |
Quote:
Quote:
|
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 Code:
for i in `seq 1 5`; do |
The point is, don't reopen, but instead create your own question and reference this one if need be.
|
Quote:
Quote:
Quote:
|
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 Hope is more clear now. |
Apparently I'm not very clever, and someone else on the forum have pointed the obvious:
Code:
for count in `seq 1 n`; do |
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 |
Quote:
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. |