2

I have a for loop and case statements. The for loop has quite a bit element list and the case statement will assign an 1D array or a vector. These values will be used in for loop after. I have the following code.

What happens is that the for loop does the job only for the first value in the vector. For example, if the f=C, the case is "C") isotope=(6012 6013);;

for n in $isotope: It loops only for 6012 not for 6013. Same issue with f=Ce, it only does the loop for 58136 not for the rest.

# loop through elements
for f in C Ce 
do
cd ${f}

case $f in 
   "Al") isotope=(13027) ;;
   "C")  isotope=(6012 6013);;
   "Ce") isotope=(58136 58138 58140 58142);;
esac

for n in $isotope

do

....# loop through elements
for f in C Ce 
do
cd ${f}

case $f in 
   "Al") isotope=(13027) ;;
   "C")  isotope=(6012 6013);;
   "Ce") isotope=(58136 58138 58140 58142);;
esac

for n in $isotope

do

....

Thank you for the help

Birsen

1
  • 1
    Did you really intend to write the same code twice?
    – Barmar
    May 3 at 15:06

2 Answers 2

7

In Bash and ksh, referencing an array without an index ($foo) is the same as referencing it with an index of 0 (${foo[0]}). The way to get all elements of the array is to index with the special value @ ("${foo[@]}"), noting that the expansion needs to be quoted for it to work properly.

In zsh, the expansion without an index ($foo) would give all non-empty values.

So, assuming you're using Bash or ksh, after isotope=(6012 6013), you need

for n in "${isotope[@]}"; do
   ...

See also:

1
  • 1
    For completeness, in yash, $foo would give you all values but subject to split+glob (not empty-removal), "$foo" would give you all values, same as "$foo[@]" / "${(@)foo}" in zsh. "${foo[@]}" works in all of zsh/ksh/bash/yash. May 3 at 15:41
3

Aside from the incorrect use of array variables, I'm seeing a nested use of the same loop variables ($f and $n):

for f in ...
do
    for n in ...
    do
        for f in ...
        do
            for n in ...
            do

Although these loops will work, each loop variable isn't scoped to its loop. Once the inner set of loops have ended the value for each loop variable will remain as the last value set rather than reverting to the value set in the an outer loop.

If that's unclear, try running this and reviewing the output:

for a in 1 2
do
    echo; echo "Outer: $a"

    for a in y z
    do
        echo "Inner: $a"
    done

    echo "Out of inner loop: $a"
done
4
  • I'm not sure they're really supposed to be nested -- I think the OP may have inadvertently posted the same code twice.
    – Barmar
    May 3 at 15:06
  • @Barmar I suspect so too, but I wasn't completely sure May 3 at 15:23
  • Roaima,Thank you, it worked.
    – BircanA
    May 3 at 15:28
  • @BircanA I'm pleased you've got a solution (or two). Please remember to use the tick mark ✓ to accept the one answer that best worked for you. May 3 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .