LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-19-2006, 05:01 AM   #1
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Rep: Reputation: 31
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
 
Old 08-19-2006, 06:30 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,335
Blog Entries: 55

Rep: Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535
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
echo "${array0[1]}"|grep -qv [a-z]; case "$?" in
 0) if [ "${array0[1]}" -eq "${array1[1]}" ]; then doSomething; fi;;
 *)  if [ "${array0[1]}" = "${array1[1]}" ]; then doSomething; fi;;
esac
else check the example in your previous thread about the same subject?



Maybe I don't get what you're trying to do, but this looks like it.
Code:
#!/bin/sh
# first size is array0[0]; first filename is array0[1]
array0=($(find /home/danish/scp -type f -maxdepth 1 2>/dev/null | while read f; do stat -c "%s %n" "${f}"; done))
array1=($(ssh 192.168.10.98 'find /home/danish/scp -type f -maxdepth 1 2>/dev/null | xargs -iF stat -c "%s %n" "F"'))
if [ "${#array0[@]}" = "${#array1[@]}" ]; then
 n=0; until [ "$n" -eq "$[${#array0[@]}+1]" ]; do fn=$[${n}+1]
 # compare name
 if [ "${array0[$fn]}" = "${array1[$fn]}" ]; then
  # compare size and if match echo rm
  if [ "${array0[$n]}" = "${array1[$n]}" ]; then
   # somehow size turns up in the rm list; 
   # too lazy to figure out why: wedge in file check instead
   [ -f "${array0[$fn]}" ] && echo "rm ${array0[$fn]}"
  fi
 else
  echo "filenames don't match"
 fi
 ((n++))
 done
else
 echo "Dissimilar amount of elements in arrays"
fi
# always return zero on script exit
exit 0
This is horrid and should *never* be used.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
Shel script mail send ("/" in mail address) problem anaid Linux - Networking 3 08-23-2005 07:41 AM
shel script resources b123coder Programming 1 06-28-2005 08:02 AM
Korn Shel Script Error Lun1x Linux - Newbie 5 07-29-2004 04:58 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM


All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration