-1
#/bin/sh
INPUT_ETH="eth0,eth1,eth2,eth3"
i=0
for eth in $(echo $INPUT_ETH | tr "," "\n")
do

 eth_$i=$eth

 echo "$eth_$i"

i=`expr $i + 1`

if [ $eth_$i = $BLA_BLA]

  then;
       ..............
  fi
      done   

*sh split.sh**

split.sh: eth_0: command not found

split.sh: eth_1: command not found

split.sh: eth_2: command not found

split.sh: eth_3: command not found

final output shold be .. in variable " eth_0 " the string valu shold be "eth0" same as for eth_1....eth_2 etc..etc... after this I want to make a loop on this varibles eth_0,eth_1 etc

Jatin Bodarya
  • 1,425
  • 2
  • 20
  • 32

4 Answers4

1

This is an expansion of William Pursell's answer, if you are actually using bash and are not restricted to sh:

#!/bin/bash

INPUT_ETH=(eth0 eth1 eth2 eth3)
for eth in ${INPUT_ETH[@]}
do
    echo "$eth"


    if [[ $eth = $BLA_BLA ]]
    then;
       ..............
    fi
done

Use a real array, and don't bother trying to simulate them with dynamic variable names.

If you really must, bash also provides the declare command, which is safer than eval because it can't execute arbitrary code: it just performs parameter expansion and sets the value of a variable:

declare "eth_$i=$eth"
chepner
  • 497,756
  • 71
  • 530
  • 681
0

You cannot have spaces in an assignment, nor can you use a variable in the name without eval:

eth_$i = $eth

must be written:

eval eth_$i=$eth

See Bash script variable declaration - command not found

As to the 2nd question, and you can do:

if eval test $eth_$i = blah; then

or (and again you need more whitespace):

if eval [ $eth_$i = blah ]; then

By the way, both chepner and glenn jackman are correct about using an array. I would go one farther and say you probably should not be doing this at all. Any time you want to access these variables you are building, just iterate over the original string instead.

Community
  • 1
  • 1
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

Assuming bash:

split the string into an array

INPUT_ETH="eth0,eth1,eth2,eth3"
IFS=, read -a eth <<< "$INPUT_ETH"
for (( i=0; i<${#eth[@]}; i++ )); do echo "$i - ${eth[$i]}"; done

outputs

0 - eth0
1 - eth1
2 - eth2
3 - eth3

For creating dynamic variable names, use declare

declare eth_$i=$eth

But using dynamic variable names tends to make your life more difficult. Use arrays, it's what they're good at.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

As others have said just loop around the list.

An old fashioned way of doing this is using IFS.

#/bin/sh
INPUT_ETH="eth0,eth1,eth2,eth3"
OFS=$IFS IFS=,
set -- $INPUT_ETH
IFS=$OFS
BLA_BLA=eth2
for eth in $*; do
    if [ $eth = $BLA_BLA ] ; then
        echo "$eth OK - now work."
    else
        echo "$eth ignored"
    fi
done

Output:

eth0 ignored
eth1 ignored
eth2 OK - now work.
eth3 ignored
sotapme
  • 4,695
  • 2
  • 19
  • 20
  • you are not getting my question. It says I suppose to split IN into varios part, and store them in to separate variables like "eth_0" ,"eth_1" etc... here the _0 and _1 are the another variable values .. lkets say $i .... means the variable should be " $eth_$i" its only one variable (here I am wrong).. now after that I want value of this variable. so How can I use it ? – Jatin Bodarya Feb 08 '13 at 05:04
  • In effect your example says ``foreach eth in eth_list ... do ...``. It's not clear why you'd want to build vars called ``eth_1 .. eth_(n)`` because when you're outside the loop how are you going to know how many you have. You need modify your question to show why you need to create ``eth_1`` etc as your question makes it seem that all you need to do is loop around the ``eth_list`` - there seems to be no benefit in wanting to create these ``eth_1`` etc, it's hard to imagine why you'd want to do it without just looping on the list again. Please expand your question for the next logical part. – sotapme Feb 08 '13 at 10:12