Doubt variable substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt variable substitution
# 1  
Old 03-23-2009
Doubt variable substitution

Hello everybody,
I've written the following script:

vOutputFile="subs_out.txt"
vAccount1="ebsemerg"
vAccount2="ebsemer2"
vAccount3="ebsemer3"
vAccount4="ebsemer4"
vAccount5="ebsemer5"
vAccount="vAccount"
vNumber=1234567
cd /opt/east/bin

echo ${print 'vAccount'${ind}}

for (( ind = 1; ind <= 5; ind++ ))
do
echo eracc status `echo vAccount${ind}` $vNumber
done


I'm trying to execute a for loop and access the value for variable vAccount1 to vAccount5. For that I'm construncting the variable name with `echo vAccount${ind}` command but not able to ask for the value of new created string which is a variable name.

The script produces the below output:
eracc status vAccount1 1234567
eracc status vAccount2 1234567
eracc status vAccount3 1234567
eracc status vAccount4 1234567
eracc status vAccount5 1234567

but I want the output to be:
eracc status value_of_vAccount1 1234567
eracc status value_of_vAccount2 1234567
eracc status value_of_vAccount3 1234567
eracc status value_of_vAccount4 1234567
eracc status value_of_vAccount5 1234567

Can anybody help me out

thanks & regards,
Jitu
# 2  
Old 03-23-2009
I was stuck once myself when scripting this way.

One mistake I can point out in your script is that you have written
Code:
vAccount${ind}

This would obviously replace only the ind variable.

If you try and do something like
Code:
echo $vAccount${ind}

you would get something like
Code:
bash: $vAccount${ind}: bad substitution

Guys, if anyone knows what exactly is the problem here, please let us know.

Meanwhile what you could do is use arrays (this is what I did)
Eg: vAccount[1], vAccount[2], etc.
To use them, you would need to access the variable by $vAccount[$ind]
# 3  
Old 03-23-2009
Quote:
Originally Posted by umar.shaikh
I was stuck once myself when scripting this way.
One mistake I can point out in your script is that you have written
Code:
vAccount${ind}

This would obviously replace only the ind variable.
Yeap. I tried many ways ... but those didn't work ...
I too have the same idea of using arrays

Hope will get the solution for the first way
# 4  
Old 03-23-2009
You can use arrays.

With ksh93, zsh ans bash:

Code:
vAccounts=(ebsemerg ebsemerg2 ebsemerg3) vNumber=1234567
for ((i=0;i<${#vAccounts[@]};i++)); do 
  printf 'the value of %s is %d\n' "${vAccounts[i]}" $vNumber
done

NB: In Z shell the arrays are 1 based.

Or just foreach:

Code:
vAccounts=(ebsemerg ebsemerg2 ebsemerg3) vNumber=1234567
for e in "${vAccounts[@]}"; do 
  printf 'the value of %s is %d\n' "$e" $vNumber
done

With pre-POSIX shells that don't support arrays natively
you can emulate such structures with positional parameters:

Code:
set -- ebsemerg ebsemerg2 ebsemerg3 
vNumber=1234567
for e in "$@"; do 
  printf 'the value of %s is %d\n' "$e" $vNumber
done

If you want array indexed by strings (not integers), i.e. associative arrays, you'll need a more sophisticated shell like ksh93, zsh or bash4.

With ksh93 you can use the following syntax:

Code:
a=([vAccount1]=ebsemerg [vAccount2]=ebsemer2 [vAccount3]=ebsemer3)
vNumber=1234567

for k in "${!a[@]}"; do
  printf 'the variable %s contains the key %s which has the value %d\n' "$k" "${a[$k]}" $vNumber
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

command substitution doubt

Hi, I almost always use back quotes in scripts to assigin output of a command to a variable. eg: file=`basename a/b/c/d/file` year_mon=`date +%Y%m` But the same can be achieved like: file=$(basename a/b/c/d/file) year_mon=$(date +%Y%m) I would like to know if there is... (3 Replies)
Discussion started by: wanderingmind16
3 Replies

2. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies

3. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

4. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

5. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

6. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

7. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

8. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

9. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question