Post Closed as "duplicate" by mikeserv, PersianGulf, chaos, G-Man, Stephen Kitt of
2 improve formatting
source | link

This is my script: load_1=15231 load_2=20547 load_3=24561 load_4=22874 f4=434

sum_1=expr $load_1 + $f4 sum_2=expr $load_2 + $f4 sum_3=expr $load_3 + $f4 sum_4=expr $load_4 + $f4

min=$sum_1 LIST1="1 2 3 4" for x in $LIST1 do if [ $sum_$x -lt $min ] then min=$sum_$x f3=$x fi done echo $min echo $f3

load_1=15231
load_2=20547
load_3=24561
load_4=22874
f4=434

   sum_1=`expr $load_1 + $f4`
   sum_2=`expr $load_2 + $f4`
   sum_3=`expr $load_3 + $f4`
   sum_4=`expr $load_4 + $f4`

   min=$sum_1
   LIST1="1 2 3 4"
   for x in $LIST1
    do
     if [ $sum_$x -lt $min ]
      then
       min=$sum_$x
       f3=$x
     fi
    done
   echo $min
   echo $f3

below is the output while exeutingexecuting the script using bash -xbash -x: + load_1=15231 + load_2=20547 + load_3=24561 + load_4=22874 + f4=434 ++ expr 15231 + 434 + sum_1=15665 ++ expr 20547 + 434 + sum_2=20981 ++ expr 24561 + 434 + sum_3=24995 ++ expr 22874 + 434 + sum_4=23308 + min=15665 + LIST1='1 2 3 4' + for x in '$LIST1' + [[ 1 -lt 15665 ]] + min=1 + f3=1 + for x in '$LIST1' + [[ 2 -lt 1 ]] + for x in '$LIST1' + [[ 3 -lt 1 ]] + for x in '$LIST1' + [[ 4 -lt 1 ]] + echo 1 1 + echo 1 1

+ load_1=15231
+ load_2=20547
+ load_3=24561
+ load_4=22874
+ f4=434
++ expr 15231 + 434
+ sum_1=15665
++ expr 20547 + 434
+ sum_2=20981
++ expr 24561 + 434
+ sum_3=24995
++ expr 22874 + 434
+ sum_4=23308
+ min=15665
+ LIST1='1 2 3 4'
+ for x in '$LIST1'
+ [[ 1 -lt 15665 ]]
+ min=1
+ f3=1
+ for x in '$LIST1'
+ [[ 2 -lt 1 ]]
+ for x in '$LIST1'
+ [[ 3 -lt 1 ]]
+ for x in '$LIST1'
+ [[ 4 -lt 1 ]]
+ echo 1
1
+ echo 1
1

the value of loop variable 'x' has to be substituted in the test condition variable sum_xsum_x and then value of $sum_x$sum_x has to be given to the test condition. But as shown in the output, the value of 'x' is directly taken in the condition. I have tried various combinations of quotes and paranthesis but nothing worked for me. Please help!!

This is my script: load_1=15231 load_2=20547 load_3=24561 load_4=22874 f4=434

sum_1=expr $load_1 + $f4 sum_2=expr $load_2 + $f4 sum_3=expr $load_3 + $f4 sum_4=expr $load_4 + $f4

min=$sum_1 LIST1="1 2 3 4" for x in $LIST1 do if [ $sum_$x -lt $min ] then min=$sum_$x f3=$x fi done echo $min echo $f3

below is the output while exeuting the script using bash -x: + load_1=15231 + load_2=20547 + load_3=24561 + load_4=22874 + f4=434 ++ expr 15231 + 434 + sum_1=15665 ++ expr 20547 + 434 + sum_2=20981 ++ expr 24561 + 434 + sum_3=24995 ++ expr 22874 + 434 + sum_4=23308 + min=15665 + LIST1='1 2 3 4' + for x in '$LIST1' + [[ 1 -lt 15665 ]] + min=1 + f3=1 + for x in '$LIST1' + [[ 2 -lt 1 ]] + for x in '$LIST1' + [[ 3 -lt 1 ]] + for x in '$LIST1' + [[ 4 -lt 1 ]] + echo 1 1 + echo 1 1

the value of loop variable 'x' has to be substituted in the test condition variable sum_x and then value of $sum_x has to be given to the test condition. But as shown in the output, the value of 'x' is directly taken in the condition. I have tried various combinations of quotes and paranthesis but nothing worked for me. Please help!!

This is my script:

load_1=15231
load_2=20547
load_3=24561
load_4=22874
f4=434

   sum_1=`expr $load_1 + $f4`
   sum_2=`expr $load_2 + $f4`
   sum_3=`expr $load_3 + $f4`
   sum_4=`expr $load_4 + $f4`

   min=$sum_1
   LIST1="1 2 3 4"
   for x in $LIST1
    do
     if [ $sum_$x -lt $min ]
      then
       min=$sum_$x
       f3=$x
     fi
    done
   echo $min
   echo $f3

below is the output while executing the script using bash -x:

+ load_1=15231
+ load_2=20547
+ load_3=24561
+ load_4=22874
+ f4=434
++ expr 15231 + 434
+ sum_1=15665
++ expr 20547 + 434
+ sum_2=20981
++ expr 24561 + 434
+ sum_3=24995
++ expr 22874 + 434
+ sum_4=23308
+ min=15665
+ LIST1='1 2 3 4'
+ for x in '$LIST1'
+ [[ 1 -lt 15665 ]]
+ min=1
+ f3=1
+ for x in '$LIST1'
+ [[ 2 -lt 1 ]]
+ for x in '$LIST1'
+ [[ 3 -lt 1 ]]
+ for x in '$LIST1'
+ [[ 4 -lt 1 ]]
+ echo 1
1
+ echo 1
1

the value of loop variable 'x' has to be substituted in the test condition variable sum_x and then value of $sum_x has to be given to the test condition. But as shown in the output, the value of 'x' is directly taken in the condition. I have tried various combinations of quotes and paranthesis but nothing worked for me. Please help!!

1
source | link

accessing loop variable in a loop

This is my script: load_1=15231 load_2=20547 load_3=24561 load_4=22874 f4=434

sum_1=expr $load_1 + $f4 sum_2=expr $load_2 + $f4 sum_3=expr $load_3 + $f4 sum_4=expr $load_4 + $f4

min=$sum_1 LIST1="1 2 3 4" for x in $LIST1 do if [ $sum_$x -lt $min ] then min=$sum_$x f3=$x fi done echo $min echo $f3

below is the output while exeuting the script using bash -x: + load_1=15231 + load_2=20547 + load_3=24561 + load_4=22874 + f4=434 ++ expr 15231 + 434 + sum_1=15665 ++ expr 20547 + 434 + sum_2=20981 ++ expr 24561 + 434 + sum_3=24995 ++ expr 22874 + 434 + sum_4=23308 + min=15665 + LIST1='1 2 3 4' + for x in '$LIST1' + [[ 1 -lt 15665 ]] + min=1 + f3=1 + for x in '$LIST1' + [[ 2 -lt 1 ]] + for x in '$LIST1' + [[ 3 -lt 1 ]] + for x in '$LIST1' + [[ 4 -lt 1 ]] + echo 1 1 + echo 1 1

the value of loop variable 'x' has to be substituted in the test condition variable sum_x and then value of $sum_x has to be given to the test condition. But as shown in the output, the value of 'x' is directly taken in the condition. I have tried various combinations of quotes and paranthesis but nothing worked for me. Please help!!