|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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++ done echo "ssh to 192.168.10.98 starts here" array1=`ssh 192.168.10.98 " vdir /home/danish/scp/* | tr -s \" \"|cut -d \" \" -f5 "` length=${#array1[*]} j=0 while [ $j -lt $length ]; do echo "${array1[$j]}" let j++ done Thanks Danish |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 21 Aug 2006 02:35:54 -0700, Danish wrote:
[...] > integer expression expected > ./ls2.sh: line 18: [: 0 [...] > n=0 > while [ $n -lt ${#array1[@]} ] > do > [ "${array[$n]}" -ne "${array1[$n]}" ] && echo "Error" > done [...] Chances are the file is in MSDOS text formant where lines are terminated by the <CR><LF> sequence instead of <LF> as Unix expects it. So $n contains "0<CR>" which is not a valid integer expression. Use sed l < your-script to identify where the CRs are. Note that $n is not incremented in your loop. You're using arrays in a sh script. Arrays are not a standard sh feature, you should specify which specific sh implementation that script is intended for as you rely on non-standard extension. So #! /bin/bash - or #! /bin/ksh - .... -- Stephane |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Stephane Chazelas wrote: > On 21 Aug 2006 02:35:54 -0700, Danish wrote: > [...] > > integer expression expected > > ./ls2.sh: line 18: [: 0 > [...] > > n=0 > > while [ $n -lt ${#array1[@]} ] > > do > > [ "${array[$n]}" -ne "${array1[$n]}" ] && echo "Error" > > done > [...] > > Chances are the file is in MSDOS text formant where lines are > terminated by the <CR><LF> sequence instead of <LF> as Unix > expects it. > > So $n contains "0<CR>" which is not a valid integer expression. > > Use sed l < your-script to identify where the CRs are. > > Note that $n is not incremented in your loop. > > You're using arrays in a sh script. Arrays are not a standard sh > feature, you should specify which specific sh implementation > that script is intended for as you rely on non-standard > extension. > > So > > #! /bin/bash - > > or > > #! /bin/ksh - > > ... > > -- > Stephane I did sed my-script.sh But it gave me the following error.... #sed ls2.sh sed: -e expression #1, char 2: extra characters after command Thanks Danish |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 21 Aug 2006 04:44:55 -0700, Danish wrote:
[...] >> Use sed l < your-script to identify where the CRs are. [...] > I did sed my-script.sh > > But it gave me the following error.... > > #sed ls2.sh > sed: -e expression #1, char 2: extra characters after command [...] I wrote: sed l < ls2.sh That the letter l (el) after sed. Or cat -vte < ls2.sh or od -c < ls2.sh Or vi ls2.sh Then :set list within vi. -- Stephane |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Stephane Chazelas wrote: > On 21 Aug 2006 04:44:55 -0700, Danish wrote: > [...] > >> Use sed l < your-script to identify where the CRs are. > [...] > > I did sed my-script.sh > > > > But it gave me the following error.... > > > > #sed ls2.sh > > sed: -e expression #1, char 2: extra characters after command > [...] > > I wrote: > > sed l < ls2.sh > > That the letter l (el) after sed. > > Or > > cat -vte < ls2.sh > > or > > od -c < ls2.sh > > Or > > vi ls2.sh > > Then > > :set list > > within vi. > > -- > Stephane Sorry for being a dud...Actually Im really troubled by this script..Here is the output after dong #sed l < ls2.sh and # cat -vte ls2.sh #!/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 Danish |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 21 Aug 2006 05:32:58 -0700, Danish wrote:
[...] > array1=`ssh 192.168.10.98 " vdir /home/danish/scp/* | tr -s \" \"|cut > -d \" \" -f5 "`$ That's not how you define an array. > $ > n=0$ > while [ $n -lt ${#array1[@]} ]$ > do$ > [ "${array[$n]}" -ne "${array1[$n]}" ] && echo "Error"$ Then it will be that "[" invocation that fails. Maybe $array, $array1 contain <CR>s. Try ./ls2.sh 2>&1 | sed l To see what "invalid integer expression" [ is complaining about. -- Stephane |
|
![]() |
| Outils de la discussion | |
|
|