|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
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 |
|
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 |
|
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 |
|
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
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 |
|
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 |
|
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 |
|
|
|
#9 |
|
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. It has to do with the way you break out of the loop. You can go on with the next item in the batch by putting "continue" where you've put "done". So the lines there become: > if [ $size -lt 630 ] > then > continue > fi Also, if it doesn't work for you, you can put #!/bin/sh instead of #!/bin/ksh When I run your script with these modifications, I don't get errors, but I do get : ls: file*: No such file or directory ls: file*: No such file or directory there are 0 files to process I'm not an expert at shell-scripting, but I wish you luck .. Greetz, mistige |
|
![]() |
| Outils de la discussion | |
|
|