Loop iteration with two variables

Hello,

I have been stuck on this for some time and invested many hours trying to find a solution. I am trying to either loop through two variables or or two arrays and not sure how to do it. I am limited to ksh only, and don't have the ability to do a foreach, or for i AND for j etc...I want to do a diff and compare version1 cmd directory to version2 cmd directory, and then iterate and compare version1 properties directory to version 2 properties directory. Can't find a similar thread. I will write a pseudo code below. I am open to other ideas.. Any help is appreciated.

old_dir="/usr/version1/cmd /usr/version1/properties" 
new_dir="/usr/version2/cmd /usr/version2/properties"

start loop
diff -r $old_dir $new_dir
end loop

After trying for hours, which loop constructs did you find in ksh ? And what reasons would stop you from using them?

Thanks for responding Rudy.
Here is an example. I know what is wrong here as the j loop iterates both, prior to going back and iterating the second i loop. Like i said i don't have the foreach or for i AND for j. I'm realizing the for loop may not be what i need here, but not sure what to do. Hope that makes sense.

#/usr/bin/ksh
old_dir="/usr/version1/cmd /usr/version1/properties"
new_dir="/usr/version2/cmd /usr/version2/properties"
for i in old_dir
do
for j in new_dir
do
print $old_dir $new_dir
done
done

Output is

/usr/version1/cmd /usr/version1/properties /usr/version2/cmd /usr/version2/properties

I need the output to be

/usr/version1/cmd /usr/version2/cmd /usr/version1/properties /usr/version2/properties

How about

echo $old_dir $new_dir | tr ' ' '\n' | sort -t'/' -k4,4 | tr '\n' ' '
/usr/version1/cmd /usr/version2/cmd /usr/version1/properties /usr/version2/properties 

Thanks Rudy. Sorry i may not have been clear, but I do not believe I'm looking for a sort here, but rather something to iterate though two variable list or two array. Here is another example:
I need one iteration for Joe Kim
then Brian Annie
then John Kate
then Mark Anna

#/usr/bin/ksh
group1="Joe Brian John Mark"
group2="Kim Annie Kate Anna"
print $group1 $group2

You are still not being clear at all. There are lots of ways to iterate through two lists. The code you have shown us above obviously does not perform any iterations and just concatenates your two lists, producing output like:

Joe Brian John Mark Kim Annie Kate Anna

But if you want something that iterates through the elements of $group2 for each element of $group1 , that would be something more like:

#/usr/bin/ksh
group1="Joe Brian John Mark"
group2="Kim Annie Kate Anna"
for g1 in $group1
do	for g2 in $group2
	do	print $g1 $g2
	done
done

which would give you:

Joe Kim
Joe Annie
Joe Kate
Joe Anna
Brian Kim
Brian Annie
Brian Kate
Brian Anna
John Kim
John Annie
John Kate
John Anna
Mark Kim
Mark Annie
Mark Kate
Mark Anna

But, if you just want corresponding pairs from equal length arrays, that would be something more like:

#/usr/bin/ksh
group1=(Joe "Brian Cranston" John "Brian Wilson" Mark)
group2=(Kim Annie "Sue Ann" Anna "Sue Ellen")
i=0
while [ $i -lt ${#group1[@]} ]
do	printf '%s:%s\n' "${group1}" "${group2}"
	i=$((i + 1))
done

which produces the output:

Joe:Kim
Brian Cranston:Annie
John:Sue Ann
Brian Wilson:Anna
Mark:Sue Ellen

Is one of these what you are trying to do?

You haven't said what operating system and release of that operating system you're using, so we don't know which version of the Korn shell you're using. The above was tested on Mac OS X El Capitan release 10.11.6 using /bin/ksh which is version 93u+ 2012-08-01. But I think the arrays used in these examples will also work on 1988 versions as well as 1993 versions of the Korn shell. (There are shorter, easier ways to do the last script above with a 1993 or later version of ksh .)

ksh allows for arrays, so this might help:

A1=($group1)
A2=($group2)
for i in $(seq 0 ${#A1[@]}); do echo ${A1[$i]} ${A2[$i]}; done
Joe Kim
Brian Annie
John Kate
Mark Anna

Might need some polishing, as there's an trailing empty line...

EDIT: Hola, Don Cragun just outperformed me by 2 minutes...

This is tested in bash , but a recent ksh offers the central functionalities as well:

{ exec 4<&0; { exec 3<&0; while read -u3 -d" " A && read -u4 -d" " B; do echo $A $B; done; } <<< $group1" "; } <<< $group2" "
Joe Kim
Brian Annie
John Kate
Mark Anna

EDIT: Actually, the file descriptor 3 stuff can be omitted and the normal stdin be used.

You can get rid of the trailing empty line by changing ${#A1[@]} in the above to $((${#A1[@]}-1)) . I usually avoid seq in loops like this because it isn't a built-in in either ksh or bash (at least on OS X).

In bash , ${!A1[@]} returns all the indices of the array A1, so this simplified approach will work:

for i in ${!A1[@]}; do echo ${A1[$i]} ${A2[$i]}; done
Joe Kim
Brian Annie
John Kate
Mark Anna

Not sure if it does in ksh , though...

${!array[@]} expands to a list of the subscripts of the current elements of array in 1993 and later versions ksh . In 1988 versions of ksh it is a syntax error.

With standard Posix stuff:

group1="Joe Brian John Mark"
group2="Kim Annie Kate Anna"
exec 3<<EOF
$(printf "%s\n" $group2)
EOF
while read name1 && read name2 <&3
do
  echo "$name1:$name2"
done <<EOF
$(printf "%s\n" $group1)
EOF

Or, if you use newlines as list separators (which is better if you use names with spaces, plus it saves two subshells):

group1="\
Joe
Brian
John
Mark"

group2="\
Kim
Annie
Kate
Anna"

exec 3<<EOF
$group2
EOF

while read name1 && read name2 <&3
do
  echo "$name1:$name2"
done <<EOF
$group1
EOF

Output:

Joe:Kim
Brian:Annie
John:Kate
Mark:Anna

Rudy and Don. I really appreciate your responses, and I was able to get both these to run on ksh93 my original diff -r tasks. I hate to flip flop on you, but i was using the print names as an easy example to follow. However it is not running on ksh88. To cut to the chase, i am able to get below code to run ksh88 as long as there is only 1 directory per group, however I need it to run with multiple directories. I will post both examples below. If you have any ideas please let me know. Scrutinizer i will have a look at your example next. Thanks again.

This below does not work in ksh88 when there are two directories per group 1/2.

#/usr/bin/ksh
group1="/usr/bin /usr/games"
group2="/usr/local/bin /usr/local/games"
i=0
j=0
while [ $i -lt ${#group1[@]} ] && [ $j -lt ${#group2[@]} ]
do	diff -r "${group1}" "${group2[j]}"
	i=$((i + 1))
        j=$((j + 1)) 
done

This below WILL work in ksh88 with only one directory variable per group, note i cannot use () to surround the group1/2 variables in ksh88.

#/usr/bin/ksh
group1="/usr/bin"
group2="/usr/local/bin"
i=0
j=0
while [ $i -lt ${#group1[@]} ] && [ $j -lt ${#group2[@]} ]
do	diff -r "${group1}" "${group2[j]}"
	i=$((i + 1))
        j=$((j + 1)) 
done

Please show us the exact output you get from running the following script:

#/usr/bin/ksh
set -xv
group1=("/usr/bin" "/usr/games")
group2=("/usr/local/bin" "/usr/local/games")
i=0
while [ $i -lt ${#group1[@]} ] && [ $i -lt ${#group2[@]} ]
do	echo diff -r "${group1}" "${group2}"
	i=$((i + 1))
done

Hey Don,
I had to fix my shebang to #!/usr/bin/ksh, but it did not help. I have to run on ksh88 as i have no other choice and the output from your request is as follows with ksh88: The ksh88 does not like the parenthesis. ksh93 has no problem with the parenthesis, but that is not the version which i need it to run. Thanks again.

The following code in ksh88:

#!/usr/bin/ksh
set -xv
group1=("/usr/bin" "/usr/games")
group2=("/usr/local/bin" "/usr/local/games")
i=0
while [ $i -lt ${#group1[@]} ] && [ $i -lt ${#group2[@]} ]
do	echo diff -r "${group1}" "${group2}"
	i=$((i + 1))
done

produces the output:

Syntax error at line 3 : '(' is not expected

---------- Post updated at 08:33 AM ---------- Previous update was at 07:35 AM ----------

Don - I have a working solution, thanks to your efforts. This will allow me to take vendor code upgrades and spit out which of the property files have changes in numerous directories. This will save a ton of time, as currently we are going through each of them manually. I can't tell you now much i appreciate this. Here is the final solution. Thanks again.

#!/usr/bin/ksh
set -A old_dir /usr/version1/cmd /usr/version1/properties
set -A new_dir /usr/version2/cmd /user/version2/properties
i=0
while [ $i -lt ${#old_dir[@]} ] && [ $i -lt ${#new_dir[@]} ]
do	diff -r "${old_dir}" "${new_dir}" |awk 'NR==1{print $3}'
	i=$((i + 1))
done

How about defining a function like

CV() { for i in $2; do echo diff -r $1${3% *}/$i $1${3#* }/$i; done; }

that you call with a path, a number (two or more) of subdirectories, and two versions to compare, both latter separated by spaces?

CV /usr/version 'cmd properties val' '1 2'
diff -r /usr/version1/cmd /usr/version2/cmd
diff -r /usr/version1/properties /usr/version2/properties
diff -r /usr/version1/val /usr/version2/val

I'm glad you got arrays to work with your version of ksh88.

Did you also try the solutions Scrutinizer suggested in post #12?

An extra possibility I thought of:

If the names may contain spaces:

group1="\
Joe
Brian
John Doe
Mark"

group2="\
Kim
Annie
Kate
Anna"

printf "%s\n" "$group1" "$group2" | pr -t2s:

--
If the lists always consist of only single words, you might get away with something like this:

group1="Joe Brian John Mark"
group2="Kim Annie Kate Anna"
printf "%s\n" $group1 $group2 | pr -t2s:

The post#12 does not work with an old Bourne shell

/tmp/sh37022: cannot open

Maybe a bug? The shell seems to remember the file name, not the handle.
This modification makes it work:

#!/bin/sh
group1="/usr/version1/cmd /usr/version1/properties"
group2="/usr/version2/cmd /user/version2/properties"

printf "%s\n" $group1 |
while read name1 && read name2 <&3
do
  echo "$name1" "$name2"
done 3<<EOF
`printf "%s\n" $group2`
EOF

The previous post can easily feed a while loop

#!/bin/sh
group1="/usr/version1/cmd /usr/version1/properties"
group2="/usr/version2/cmd /user/version2/properties"

printf "%s\n" $group1 $group2 | pr -t2 |
while read name1 name2
do
  echo "$name1" "$name2"
done

The Bourne shell is not a POSIX compliant shell, so the code was not intended to work with it. The $(....) construct does not exist in the Bourne shell, however this could easily be replace with backticks.

Using backticks, I got the same error with a Bourne shell.
This part does not seem to work:

exec 3<<EOF
`printf "%s\n" $group2`
EOF

But I do not see a reason why it should not work.

Anyway, there is rarely a reason to script with the Bourne shell nowadays, since even now ancient Unix systems have at least one Posix compliant shell available.