Results 1 to 4 of 4
Hi,
Please help in using multiple substitution with variable.
Below is the script Iam using in bash:
exc=/home/pawan/excep1
str1="pawan"
str2="arun"
str3="jagbir"
str4="sourin"
str5="ajay"
str6="makan"
for (( i=1; i <= 6; ...
- 11-01-2007 #1Just Joined!
- Join Date
- Jun 2006
- Posts
- 40
How to use multiple variable susbtitution.
Hi,
Please help in using multiple substitution with variable.
Below is the script Iam using in bash:
exc=/home/pawan/excep1
str1="pawan"
str2="arun"
str3="jagbir"
str4="sourin"
str5="ajay"
str6="makan"
for (( i=1; i <= 6; i++ ))
do
echo "${str$i}" >> ${exc}
done
But, Iam getting below error while trying:
excep2: line 13: ${str$i}: bad substitution
Please share if anybody have any idea to make this working.
Thanks in advance....
Pawan Sangal
- 11-01-2007 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
bash has arrays
otherwise, if you really need to use that method, you can try evalCode:exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[5]="makan" for (( i=0; i <= 5; i++ )) do echo "${str[i]}" done
man eval for more info.
- 11-01-2007 #3
Pawan Sangal's script as of Thu Nov 1 02:08:33 PDT 2007:
ghostdog74's's improved version as of Thu Nov 1 02:08:33 PDT 2007:Code:exc=/home/pawan/excep1 str1="pawan" str2="arun" str3="jagbir" str4="sourin" str5="ajay" str6="makan" for (( i=1; i <= 6; i++ )) do echo "${str$i}" >> ${exc} done
Several suggestions for ghostdog's otherwise fine script:Code:exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[5]="makan" for (( i=0; i <= 5; i++ )) do echo "${str[i]}" done
Most importantly, of course, the output redirection must go back in:
Also, it's always a good idea to add this to the beginning of any bash script, if you're going to always run it under Linux (or elsewhere where bash is available):Code:exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[5]="makan" for (( i=0; i <= 5; i++ )) do echo "${str[i]}" >> ${exc} done
To make this code more maintainable, so that adding a new string won't require you to adjust the for statement:Code:#!/bin/bash exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[5]="makan" for (( i=0; i <= 5; i++ )) do echo "${str[i]}" >> ${exc} done
A little bit of care must be taken here.Code:#!/bin/bash exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[5]="makan" for (( i=0; i < ${#str[@]}; i++ )) # not <= do echo "${str[i]}" >> ${exc} done
does not mean the highest-used index plus one; it means the number of elements in the array, soCode:${#str[@]}
would cause the sixth line of output to be empty, not "makan".Code:#!/bin/bash exc=/home/pawan/excep1 str[0]="pawan" str[1]="arun" str[2]="jagbir" str[3]="sourin" str[4]="ajay" str[6]="makan" for (( i=0; i < ${#str[@]}; i++ )) do echo "${str[i]}" >> ${exc} done
- 11-01-2007 #4
Or (assuming bash):
Or the number of parameters is known, just:Code:exc=/home/pawan/excep1 str1="pawan" str2="arun" str3="jagbir" str4="sourin" str5="ajay" str6="makan" set -- ${!str*} set -- $(eval echo \$str{1..$#}) printf "%s\n" "$@">"$exc"
Code:printf "%s\n" $str{1..6}>"$exc"


Reply With Quote