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.
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
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?
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
$ 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?