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 > How to skip small files in a for loop
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to skip small files in a for loop

Réponse
 
LinkBack Outils de la discussion
Vieux 22/08/2006, 12h25   #1 (permalink)
thomasriise
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to skip small files in a for loop

I e.g. have this script:

---------------------------------
#!/bin/ksh

numfiles=`ls fil* | wc -w | sed 's/ *//'`
files=`ls fil*`
echo "there are $numfiles files to process"

doit(){
for i in $files
do
list=`ls -ut $i`
size=`ls -lut $i 2>/dev/null | awk '{print $5}'`
if [ $size -lt 630 ]
then
done
fi
echo $list
done
}

doit
---------------------------------

My shell reports that the done in line 15 is unexpected. Is there
another way to skip the files smaller than the specified size? Or
should I just do that before the loop should begin?

Thanks.

  Réponse avec citation
Vieux 22/08/2006, 13h20   #2 (permalink)
fengrui
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

you can use continue to skip the loop

another way to do:
if [ $size -ge 630 ]; then
echo $list
fi

"thomasriise" <thomasriise@gmail.com>
??????:1156245931.171886.305230@m79g2000cwm.google groups.com...
> I e.g. have this script:
>
> ---------------------------------
> #!/bin/ksh
>
> numfiles=`ls fil* | wc -w | sed 's/ *//'`
> files=`ls fil*`
> echo "there are $numfiles files to process"
>
> doit(){
> for i in $files
> do
> list=`ls -ut $i`
> size=`ls -lut $i 2>/dev/null | awk '{print $5}'`
> if [ $size -lt 630 ]
> then
> done
> fi
> echo $list
> done
> }
>
> doit
> ---------------------------------
>
> My shell reports that the done in line 15 is unexpected. Is there
> another way to skip the files smaller than the specified size? Or
> should I just do that before the loop should begin?
>
> Thanks.
>



  Réponse avec citation
Vieux 22/08/2006, 13h31   #3 (permalink)
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

thomasriise wrote:
> I e.g. have this script:
>
> ---------------------------------
> #!/bin/ksh
>
> numfiles=`ls fil* | wc -w | sed 's/ *//'`
> files=`ls fil*`
> echo "there are $numfiles files to process"
>
> doit(){
> for i in $files
> do
> list=`ls -ut $i`
> size=`ls -lut $i 2>/dev/null | awk '{print $5}'`
> if [ $size -lt 630 ]
> then
> done


If you want to skip the file then use

continue

if you want to leave the loop then use

break

instead of done.

Janis

> fi
> echo $list
> done
> }
>
> doit
> ---------------------------------
>
> My shell reports that the done in line 15 is unexpected. Is there
> another way to skip the files smaller than the specified size? Or
> should I just do that before the loop should begin?
>
> Thanks.


  Réponse avec citation
Vieux 22/08/2006, 13h32   #4 (permalink)
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

thomasriise wrote:
> I e.g. have this script:
>
> ---------------------------------
> #!/bin/ksh
>
> numfiles=`ls fil* | wc -w | sed 's/ *//'`
> files=`ls fil*`
> echo "there are $numfiles files to process"
>
> doit(){
> for i in $files
> do
> list=`ls -ut $i`
> size=`ls -lut $i 2>/dev/null | awk '{print $5}'`
> if [ $size -lt 630 ]
> then
> done
> fi
> echo $list
> done
> }
>
> doit
> ---------------------------------
>
> My shell reports that the done in line 15 is unexpected. Is there
> another way to skip the files smaller than the specified size? Or
> should I just do that before the loop should begin?
>
> Thanks.
>


Instead of negative logic:

if [ $size -lt 630 ]
then
# break out of loop
fi
echo $list

just use positive:

if [ $size -ge 630 ]
then
echo $list
fi

By the way, your script will fail for files whose names contain white
space. See question 14,
http://home.comcast.net/~j.p.h/cus-faq-2.html#14, in the FAQ.

Regards,

Ed.
  Réponse avec citation
Vieux 22/08/2006, 13h32   #5 (permalink)
thomasriise
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop


fengrui wrote:
> you can use continue to skip the loop


This worked. I just changed "done" to "continue" and then it only
processed the files that had a proper size. Thanks fengrui.

  Réponse avec citation
Vieux 22/08/2006, 13h38   #6 (permalink)
thomasriise
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

> By the way, your script will fail for files whose names contain white
> space. See question 14,
> http://home.comcast.net/~j.p.h/cus-faq-2.html#14, in the FAQ.
>
> Regards,
>
> Ed.


That's right, but there's no filenames with whitespaces in this
fileflow. But thanks for the useful link anyway!

  Réponse avec citation
Vieux 22/08/2006, 13h40   #7 (permalink)
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

thomasriise wrote:
> I e.g. have this script:
>
> ---------------------------------
> #!/bin/ksh
>
> numfiles=`ls fil* | wc -w | sed 's/ *//'`
> files=`ls fil*`
> echo "there are $numfiles files to process"
>
> doit(){
> for i in $files
> do
> list=`ls -ut $i`
> size=`ls -lut $i 2>/dev/null | awk '{print $5}'`
> if [ $size -lt 630 ]
> then
> done
> fi
> echo $list
> done
> }
>
> doit
> ---------------------------------
>
> My shell reports that the done in line 15 is unexpected. Is there
> another way to skip the files smaller than the specified size? Or
> should I just do that before the loop should begin?


Yes, you should do it without loop.
See the -S option of the ls command, if available, or use the -s and
-l option and postprocess the output using awk like this

ls -sl | awk '$5>=630'

I have no Unix system at hand, so you need to replace the $5 by the
column number of ls where the size is displayed.

Janis

> Thanks.


  Réponse avec citation
Vieux 22/08/2006, 16h55   #8 (permalink)
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to skip small files in a for loop

On 22 Aug 2006 04:25:31 -0700, thomasriise wrote:
> I e.g. have this script:
>
> ---------------------------------
> #!/bin/ksh
>
> numfiles=`ls fil* | wc -w | sed 's/ *//'`
> files=`ls fil*`
> echo "there are $numfiles files to process"



Ouch. That's ugly. If you're using ksh, you could as well use
its extensions. Like arrays.

set -A files -- fil*
printf "there are %d files to process\n" "${#files}"

> doit(){
> for i in $files


for i in "${files[@]}"

> do
> list=`ls -ut $i`


What's that for?

> size=`ls -lut $i 2>/dev/null | awk '{print $5}'`

size=`ls -lut -- "$i" 2>/dev/null | awk '{print $5; exit}'`

What is the -t for?

What about -u as you're only interested in the size.

> if [ $size -lt 630 ]


if [ "$size" -lt 630 ]

> then
> done


continue instead of done

> fi
> echo $list


printf '%s\n' "$i"


> done
> }


This script would more reasonably be written:

find . \( -name . -o -prune \) -name 'fil*' \! -size -630c -print

Note zsh's:

print -rl fil*(^L-630)

--
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 10h06.


É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,14100 seconds with 16 queries