Error in for loop

I was trying to implement a nested for do loop to run a perl script.

for i in 1 10 50
do
for j in 2 12 55

do
perl  script.pl "$i" "$i" "$j"
done
done

when I implemented it within a shell script, i got the output, but every time j value will 55, or basically the last value of j in the second for loop.

Am I missing something here?

The blank line before do in your second for loop, maybe? Although it seems fine on ksh 93s+ or bash 4.1.5.

What does this output?

for i in 1 10 50
do
for j in 2 12 55

do
 echo perl  script.pl "$i" "$i" "$j"
done
done

As CarloM said, it runs fine. The blank line seems not to be the problem - show your Perl script please.

the numbers that I am trying to import into the perl script is using @ARGV. So these parameter (i, i and j) are user provided. I don't think there is any problem in the perl script. Just have to get the 3 parameters after perl script.pl

---------- Post updated at 08:05 AM ---------- Previous update was at 07:57 AM ----------

@ CarloMThe output of

for i in 1 10 
do
for j in 2 12 

do
 echo perl  script.pl "$i" "$i" "$j"
done
done

id

perl  script.pl 1 1 2
perl  script.pl 1 1 12
perl  script.pl 10 10 2
perl  script.pl 10 10 12

So the shell script loop is fine. Can you share the perl?

The output I want is only

perl  script.pl 1 1 2
perl  script.pl 10 10 12

So you are want to step through pairs of numbers then rather than to display all four combinations (or however many there may be)

The loop as written is working fine, but it's clearly not the logic you want. What is the relationship between the two numbers? There are all sorts of ways that this could be achieved, but the key to choosing a suitable way is to know why we need the values and how they are related. Can you elaborate?

Thanks,
Robin

There is no constant relationship between the pairs of numbers. In some situations I will have 3 or 4 numbers as arguments. So basically, I might not need a for loop.

Can I have all the pairs of numbers in a text file and call it into the executable of the perl script?

for example the text file

1 1 2
10 10 12
...
....

Not sure how to implement that. May be using xargs

Well, we can try a few suggestions:-

$ cat ref_file
1 2
2 4
3 7
4 10
10 12
$ for i in 1 10
do
   echo perl $i `grep "^$i " ref_file`
done
#!/bin/ksh
set -A i 1 2 3 4 10                  # Define 1st range
set -A j 2 4 7 10 120                # Define 2nd range

if [ ${#i[*]} -ne ${#j[*]} ]
then
   echo "Reference count mismatch"
   exit
fi

for ref in 1 10
do
   echo perl ${i[$ref]} ${i[$ref]} ${j[$ref]}
done

There are bound to be loads of other options. What would best for you?

Robin

Don't let the OP's use of a for-loop restrict your thinking. A simple while read x y; do ... would be a much more robust approach.

Regards,
Alister

I am a fool :o

Yes this would be neater with the a suitable input file:-

while read i j
do
   echo perl $i $i $j
done < ref_file

Thanks Alister :cool:

Robin