shel script problem regarding arrays
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 |
1) On the localhost Im doing `ls -l` on the /path/to/files and cutting the file size.
Using "cut" assumes you'll have single space separation. If you insist on using "ls" then use something like awk. Using "stat -c %s" gives you exactly what you require (filesize only) so no overhead. Next to that having names and sizes mixed in an array like this won't work unless you have a way to select name then select size to compare. It's not hard but it looks kind of inefficient to me. Of course you can have an array consisting of a single filename and size but the you'd have to ssh a gazillion times. 2) Feeding the output of `ls -l` into an array ... without checking what's in the array. Anyway. If you work with a single value you don't need the array and you can get the length of the string as well: size=$(stat -c %s /some/file); len=${#size}. 3) Doing ssh remote_host "ls -l /path/to/files" and then feeding it into an array ...which should run ssh on each file? Bit much. 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.... Code:
# check numerical Maybe I don't get what you're trying to do, but this looks like it. Code:
#!/bin/sh |
All times are GMT -5. The time now is 11:42 PM. |