1

I would like to run a for loop for a set of variables which I incorrectly denote as {A,B}. Following that my script looks like:

#!/bin/sh
for {A,B} in {1,2}  {3,4} {5,6} {7,8}
do
echo A=$A B=$B
C=$(($A+$B))
echo Sum,C = $C
done

How can I correct it?

2
  • What are you trying to do ? Commented Jul 2, 2015 at 7:14
  • I want to find sum of two numbers that I get in pairs. For my example, I want to print the numbers from each pair A and B and the sum C=A+B. Have modified the code a bit. Commented Jul 2, 2015 at 7:35

3 Answers 3

4

I would use

while read A B
do echo A=$A B=$B
    C=$(( $A + $B ))
    echo Sum,C = $C
done<<EOF
1 2
3 4
5 6
7 8
EOF

For more complex operations dc is your friend (polish reverse notation!)

C=$(echo "$A $B +pq"|dc)
2
set   1 2 3 4 5 6 7 8
while [ "$#" -gt 0 ]
do    C="$(($1+$2))"
      shift 2
done

for doesn't allow for two simultaneous assignments in that way. So get an array and shift it away at your desired interval. If you use the standard shell $@ array as I do above, then you'll always be working with your first two positionals $1 and $2. If you use some kind of extension array then you'll be working with either ${array[0]} and ${array[1]} or ${array[1]} and ${array[2]} depending on your shell.

Else, with a for loop you can encode some kind of delimiter into each argument:

for x in 1+2 3+4 5+6 7+8
do  C="$(($x))"
done

...which actually works perfectly here, but that is usually not as cleanly done.

0

I could make a possible work-around (Thanks @mikeserv for providing me the idea of array):

array=('1 2' '3 4' '5 6' '7 8')

for x in "${array[@]}"
do

A=`echo $x | awk '{print $1}'`
B=`echo $x | awk '{print $2}'`
echo A=$A B=$B

C=$(($A+$B))
echo Sum,C = $C

done

Suggest if further simplification is possible.

5
  • Well, yeah: a=(1+2 3+4 5+6 7+8); for x in "${a[@]}"; do echo "Sum,C = $((C=$x))"; done. You almost never want to call out to some other process within a loop like that. And if it's not as simple as all that, you can still split out the array members with shell param expansions like for x in "${a[@]}"; do C=$((${x% *}+${x#* }))"; done. The awk stuff is way overkill. Commented Jul 2, 2015 at 11:37
  • @mikeserv Thanks. Point is that I don't always want to do sum. The sum is for a required simple demo. I can treat A and B separately to get something else, say C=A^2 and D=1-B^3. I was thinking of function statement where I can use A and B as argument $1 and $2. But I didn't try yet. :( Commented Jul 2, 2015 at 11:57
  • That's what I meant by suggesting the parameter expansions - if you set a delimiter you can divide on it. Try A=${x% *} B=${x#* }; echo "\$A=$A \$B=$B" to see what you get. Commented Jul 2, 2015 at 12:10
  • Yeah, that works, thanks. What do x% * and x#* do? Suppose, my set has three variables, i.e. {A,B,C}. Then could I have done in a similar fashion? Commented Jul 2, 2015 at 13:24
  • Yes, and no. They trim from the tail or head of a variable based on a pattern. Read this. Commented Jul 2, 2015 at 13:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.