Operating System - HP-UX
1752782 Members
6160 Online
108789 Solutions
New Discussion юеВ

Re: exporting variables from awk

 
SOLVED
Go to solution
Nicol├бs
New Member

Re: exporting variables from awk

Ralph,

i'm trying it on linux, but it must work on HP-UX at work.

Yes, i have learned to handle arrays in bash. well, i'm learning it right now, trying these stuff

matter is that i have resolved in a simple way:

would it work on HP-UX?


for dir in $directories
do
echo "Searching in $dir" >> $log
find $dir -name "$filemask*" 1> fullnames 2> /dev/null
#cat fullnames
if [ -n `cat fullnames` ]; then
break;
fi;
done
i=0
flag=0
echo
while read fullname
do
echo "Checking for posible duplicated file" >> $log
for (( e=0 ; e <= i ; e++ ))
do
echo "checking file $i : $fullname iteration number: $e" >> $log
if [ "$fullname" = "${fullnames_array[$e]}" ];
then
echo "$fullname number $i equals another, it's Discarded" >> $log
flag=1
else
echo "$fullname ins unique!" >> $log
fi
done
if (( flag == 0 ))
then
fullnames_array[$i]=$fullname
i=$(( i + 1 ))
#echo ${fullnames_array[*]}
#echo "print $fullname"
fi
done echo "found files: ${fullnames_array[*]}" >> $log
Dennis Handly
Acclaimed Contributor

Re: exporting variables from awk

>would it work on HP-UX?

This works in ksh. You have to replace that funny for loop by a while:
for (( e=0 ; e <= i ; e++ ))
for dir in $directories; do
echo "Searching in $dir" >> $log
find $dir -name "$filemask*" 1> fullnames 2> /dev/null
#cat fullnames
if [ -n "$(< fullnames)" ]; then
break;
fi
done
i=0
flag=0
echo
while read fullname; do
echo "Checking for possible duplicated file" >> $log
(( e = 0 ))
while (( e <= i )); do
echo "checking file $i : $fullname iteration number: $e" >> $log
if [ "$fullname" = "${fullnames_array[$e]}" ]; then
echo "$fullname number $i equals another, it's Discarded" >> $log
flag=1
else
echo "$fullname is unique!" >> $log
fi
(( e += 1 ))
done
if (( flag == 0 )); then
fullnames_array[$i]=$fullname
(( i += 1 ))
#echo ${fullnames_array[*]}
#echo "print $fullname"
fi
done < fullnames
echo "found files: ${fullnames_array[*]}" >> $log

I'm not sure why you want to check for a duplicated file, find won't return those.
And you can always use "sort -u" to get rid of dups.
Nicol├бs
New Member

Re: exporting variables from awk

Thanks dennis,
it looks better with a while, but what looks really better for me is "$(< fullnames)" insteed the horrible `cat $fullnames`.
in a deeper way, what are the differences between both?


you're right, find won't return me duplicated...

but i'll cut string routes before doing comparison
Hein van den Heuvel
Honored Contributor

Re: exporting variables from awk

Well, in the 'cat' case you fork a process to run the cat code which reads the file and pipe back line after line. In the $(< file) solution you tell the shell to just go read the file itself.... best I know.

hth,
Hein.

Dennis Handly
Acclaimed Contributor

Re: exporting variables from awk

>it looks better with a while

The only problem with that while is that a continue won't work, it will skip the increment.

>what are the differences between both?

Besides what Hein said, it helps you in a catless style of scripting. You really only need cat(1) in a few cases, multiple inputs, 2 GB limitations or copying a file to stdout/stderr.