1833828 Members
2054 Online
110063 Solutions
New Discussion

Shell Problem

 
Brian Dore
New Member

Shell Problem

My problem is, how do I echo ${filesystem$i[$h]}? What needs to go where the $i is?

filesystem1[1]="north"

filesystem1[2]="south"

echo ${filesystem1[1]}

north

echo ${filesystem1[2]}

south

h=1

echo ${filesystem1[$h]}

north

((h=$h+1))

${filesystem1[$h]}

south

i=1

echo ${filesystem$i[$h]}

sh: ${filesystem$i[$h]}: The specified substitution is not valid for this command.

${filesystem"$i"[$h]}

sh: ${filesystem"$i"[$h]}: The specified substitution is not valid for this command.
10 REPLIES 10
Bejoy C Alias
Respected Contributor

Re: Shell Problem

I think u r trying to create a two dimensional array . See the attachment to see a workaround to emulate a two dimensional array ( it is for linux , u need to write the script with the same logic )
Be Always Joy ......
Mark Grant
Honored Contributor

Re: Shell Problem

99 times out of a hundred, when you think you need to do this, in fact you don't. Maybe if you were to explain in more details, the specifics of what you were after, it might be possible to come up with an alternative.
Never preceed any demonstration with anything more predictive than "watch this"
Bill Hassell
Honored Contributor

Re: Shell Problem

You have a spelling error. The array is filesystem1 but your failing example shows:

filesystem$i

You can avoid this type of problem (by getting a useful error message) using the set -u option at the start of your script. Any time an undefined variable is referenced (ie, echo) the script will stop and identify the location.


Bill Hassell, sysadmin
harry d brown jr
Honored Contributor

Re: Shell Problem


He is trying to do this:

filesystem1[1]="north"
filesystem1[2]="south"
filesystem2[1]="east"
filesystem2[2]="west"

then referencing the SUBSCRIPTS of the variables
"filesystem1"
and
"filesystem2"

He wants this to happen:
$i=1
$h=2
echo $filesystem$i[$h]
producing "south" as output

or

$i=2
$h=1
echo $filesystem$i[$h]
producing "east" as output

live free or die
harry d brown jr
Live Free or Die
Patrick Wallek
Honored Contributor

Re: Shell Problem

What about using a variable to address the filesystem$i issue?

Something like:

i=1
h=1

FS=filesystem${i}

echo ${FS[$h]}
north

i=2
h=2
FS=filesystem${i}

echo ${FS[$h]}
west
MarkSyder
Honored Contributor

Re: Shell Problem

Sorry, this is off-topic.

Mark - nice to see your name on here again. Where've you been?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Brian Dore
New Member

Re: Shell Problem

What I am actually trying to do is

Outer Loop:-

prompt the user for a server name

Inner Loop:-

Prompt the user for filesystems (1 at a time in this inner loop)


When all input has been gathered I want to display the information on screen

e.g.
_______________________________________
Server:- "Server Name"
Filesystems:- "Filesystem" "Filesystem" ...

Server:- "Server Name"
Filesystems:- "Filesystem" " Filesystem" ...

etc...
_________________________________________

Hope this makes my problem clearer.
Bejoy C Alias
Respected Contributor

Re: Shell Problem

I forgot to attach the script...
Be Always Joy ......
Ermin Borovac
Honored Contributor

Re: Shell Problem

You can do it like this

$ filesystem1[1]="north"
$ h=1
$ i=1
$ eval echo \${filesystem$i[\$h]}
north
Bejoy C Alias
Respected Contributor

Re: Shell Problem

Balle Balle.....
Brian.....Ermin's idea works.....
Be Always Joy ......