Find the answer to your Linux question:
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; ...
  1. #1
    Just 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

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    bash has arrays

    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
    otherwise, if you really need to use that method, you can try eval
    man eval for more info.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Pawan Sangal's script 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
    ghostdog74's's improved version as of Thu Nov 1 02:08:33 PDT 2007:
    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
    Several suggestions for ghostdog's otherwise fine script:

    Most importantly, of course, the output redirection must go back in:
    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
    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:
    #!/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
    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 < ${#str[@]}; i++ ))   # not <=
    do
    echo "${str[i]}" >> ${exc}
    done
    A little bit of care must be taken here.
    Code:
    ${#str[@]}
    does not mean the highest-used index plus one; it means the number of elements in the array, so
    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
    would cause the sixth line of output to be empty, not "makan".

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Or (assuming bash):

    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 "&#37;s\n" "$@">"$exc"
    Or the number of parameters is known, just:
    Code:
    printf "%s\n" $str{1..6}>"$exc"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...