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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

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

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:

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

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.

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 01:33 PM.
Reason: minor rewording