PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > shel script problem regarding arrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

shel script problem regarding arrays

Réponse
 
LinkBack Outils de la discussion
Vieux 19/08/2006, 13h33   #1
Danish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 19/08/2006, 16h48   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays

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
  Réponse avec citation
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
Vieux 21/08/2006, 11h24   #4
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays

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
  Réponse avec citation
Vieux 21/08/2006, 12h44   #5
Danish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays


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

  Réponse avec citation
Vieux 21/08/2006, 13h10   #6
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays

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
  Réponse avec citation
Vieux 21/08/2006, 13h32   #7
Danish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays


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

  Réponse avec citation
Vieux 21/08/2006, 17h54   #8
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shel script problem regarding arrays

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h13.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15645 seconds with 16 queries