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.
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
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 .)
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).
${!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.
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
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.
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
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.