|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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. > |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
> 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! |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|