Afficher un message
Vieux 21/08/2006, 10h35   #3
Danish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays


Chris F.A. Johnson wrote:
> On 2006-08-19, Danish wrote:
> > Hi,
> >
> > Ive got to compare file sizes from two remote linux machines. Because
> > of which I made the following script. It is not complete cos Im having
> > the following problem:
> >
> >
> > 1) On the localhost Im doing `ls -l` on the /path/to/files and cutting
> > the file size.
> > 2) Feeding the output of `ls -l` into an array
> > 3) Doing ssh remote_host "ls -l /path/to/files" and then feeding it
> > into an array
> >
> > Im trying to compare the values of the two arrays, and if the the size
> > of each file is equal Ill put in the `rm`command to delete the files
> > from the local host..
> >
> > Ive been able to feed the output into the array, but am not able to
> > figure out how to compare the value of the arrays....
> >
> > Would like to have your advise on it....
> >
> > #!/bin/sh
> >
> > array=(`ls -l /home/danish/scp | tr -s " " | cut -d " " -f5`)
> >
> > len=${#array[*]}
> >
> > i=0
> >
> > while [ $i -lt $len ]; do
> > echo "${array[$i]}"
> > let i++

>
> I recommend using the portable syntax:
>
> i=$(( $i + 1 ))
>
> > done

>
> You don't need a loop for that:
>
> printf "%s\n" "${array[@]}"
>
> > echo "ssh to 192.168.10.98 starts here"
> >
> > array1=`ssh 192.168.10.98 " vdir /home/danish/scp/* | tr -s \"
> > \"|cut -d \" \" -f5 "`

>
> n=0
> while [ $n -lt ${#array[@]} ]
> do
> [ "${array[$n]}" -ne "${array1[$n]}" ] && echo "Error"
> done
>
> > length=${#array1[*]}
> >
> > j=0
> >
> > while [ $j -lt $length ]; do
> > echo "${array1[$j]}"
> > let j++
> > done

>
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence


Thank you very much for the suggestions, but when I run the script..the
following error is reported....

integer expression expected
../ls2.sh: line 18: [: 0

The following is the script

#!/bin/sh

array=(`ls -l /home/danish/scp | tr -s " " | cut -d " " -f5`)

len=${#array[*]}

i=$(($i + 1))

printf "%s\n" "${array[@]}"

echo "ssh to 192.168.10.98 starts here"

array1=`ssh 192.168.10.98 " vdir /home/danish/scp/* | tr -s \" \"|cut
-d \" \" -f5 "`

n=0
while [ $n -lt ${#array1[@]} ]
do
[ "${array[$n]}" -ne "${array1[$n]}" ] && echo "Error"
done


Thanks you
Danish

  Réponse avec citation
 
Page generated in 0,06338 seconds with 9 queries