FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 07-29-2010, 02:37 AM
 
Default Shell Expansion in Bourne Shell Script Question

>> On Thu, 29 Jul 2010 01:04:27 -0000,
>> Cameron Hutchison <lists@xdna.net> said:

C> find $MAGDIR -iname '*.zip' -print0 | xargs -0 some-command
C> -iname matches names case insensitively. Since you then dont need grep,
C> you also dont need tr0.

I need to think before posting. I didn't mention that I have FreeBSD,
Linux, and Solaris boxes, and unfortunately I can't guarantee the same
access to GNU find. I can install xargs if the system version doesn't
recognize the "-0" option, so I usually end up scripting for the lowest
common denominator.

--
Karl Vogel I don't speak for the USAF or my company

Cute Celebrity Couple Names #6:
Lance Armstrong + Ivanka Trump = Armstump


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20100729023745.D8A58BED9@kev.msw.wpafb.af.mil">htt p://lists.debian.org/20100729023745.D8A58BED9@kev.msw.wpafb.af.mil
 
Old 07-29-2010, 05:12 AM
"Boyd Stephen Smith Jr."
 
Default Shell Expansion in Bourne Shell Script Question

On Wednesday 28 July 2010 13:05:22 Karl Vogel wrote:
> >> On 28.07.2010 14:42, Jochen Schulz wrote:
> J> I think you meant to write
> J> for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`
> J> Another hint: you don't need 'ls' for your case at all.
>
> I'd recommend keeping the "ls".

Then, you would be wrong. Doing the splitting on the output of ls causes IFS
characters in file names to be handled incorrectly:

bss@dellbuntu:/tmp% touch 'file with spaces'
bss@dellbuntu:/tmp% for f in `ls -d *`; do echo file; done | wc -l
9
bss@dellbuntu:/tmp% for f in *; do echo file; done | wc -l
7
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
bss@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/
 
Old 07-29-2010, 05:17 AM
"Boyd Stephen Smith Jr."
 
Default Shell Expansion in Bourne Shell Script Question

On Wednesday 28 July 2010 21:37:44 Karl Vogel wrote:
> >> On Thu, 29 Jul 2010 01:04:27 -0000,
>
> >> Cameron Hutchison <lists@xdna.net> said:
> C> find $MAGDIR -iname '*.zip' -print0 | xargs -0 some-command
> C> -iname matches names case insensitively. Since you then dont need grep,
> C> you also dont need tr0.
>
> I need to think before posting. I didn't mention that I have FreeBSD,
> Linux, and Solaris boxes, and unfortunately I can't guarantee the same
> access to GNU find. I can install xargs if the system version doesn't
> recognize the "-0" option, so I usually end up scripting for the lowest
> common denominator.

In that case, you'll have to be very careful in order to handled file names
that contain IFS characters or newlines:

find "$MAGDIR" -name "*.[zZ][iI][pP]" -exec
sh -c 'handle_single_file "$1"' ignored {} ;

That will work on any POSIX system, or most systems with POSIX-conforming find
and sh. You *may* have to specify full paths to the POSIX-conforming find and
sh commands.
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
bss@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/
 
Old 07-29-2010, 10:27 AM
Mart Frauenlob
 
Default Shell Expansion in Bourne Shell Script Question

On 29.07.2010 07:17, Boyd Stephen Smith Jr. wrote:

On Wednesday 28 July 2010 21:37:44 Karl Vogel wrote:

On Thu, 29 Jul 2010 01:04:27 -0000,



Cameron Hutchison<lists@xdna.net> said:

C> find $MAGDIR -iname '*.zip' -print0 | xargs -0 some-command
C> -iname matches names case insensitively. Since you then dont need grep,
C> you also dont need tr0.

I need to think before posting. I didn't mention that I have FreeBSD,
Linux, and Solaris boxes, and unfortunately I can't guarantee the same
access to GNU find. I can install xargs if the system version doesn't
recognize the "-0" option, so I usually end up scripting for the lowest
common denominator.


In that case, you'll have to be very careful in order to handled file names
that contain IFS characters or newlines:

find "$MAGDIR" -name "*.[zZ][iI][pP]" -exec
sh -c 'handle_single_file "$1"' ignored {} ;

That will work on any POSIX system, or most systems with POSIX-conforming find
and sh. You *may* have to specify full paths to the POSIX-conforming find and
sh commands.



yes, '-exec command {} +' is also POSIX, but newer (collects argument
list similar to xargs).
What I've been reading on a recent discussion on de.comp.os.unix.shell
-print0 is about of equal age, so about an equal chance to have one or
the other feature available.
I think using '-type f' would not be wrong either, as otherwise we would
match directories also.


Best regards

Mart


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4C515797.7070809@chello.at">http://lists.debian.org/4C515797.7070809@chello.at
 
Old 07-29-2010, 04:16 PM
"Boyd Stephen Smith Jr."
 
Default Shell Expansion in Bourne Shell Script Question

On Thursday 29 July 2010 05:27:35 Mart Frauenlob wrote:
> On 29.07.2010 07:17, Boyd Stephen Smith Jr. wrote:
> > On Wednesday 28 July 2010 21:37:44 Karl Vogel wrote:
> >> I need to think before posting. I didn't mention that I have
> >> FreeBSD, Linux, and Solaris boxes, and unfortunately I can't
> >> guarantee the same access to GNU find. I can install xargs if the
> >> system version doesn't recognize the "-0" option, so I usually end
> >> up scripting for the lowest common denominator.
> >
> > In that case, you'll have to be very careful in order to handled file
> > names that contain IFS characters or newlines:
> >
> > find "$MAGDIR" -name "*.[zZ][iI][pP]" -exec
> >
> > sh -c 'handle_single_file "$1"' ignored {} ;
> >
> > That will work on any POSIX system, or most systems with POSIX-conforming
> > find and sh. You *may* have to specify full paths to the
> > POSIX-conforming find and sh commands.
>
> yes, '-exec command {} +' is also POSIX, but newer (collects argument
> list similar to xargs).

In that case, you may be able to avoid spawning quite as many processes by
doing something like:

find "$MAGDIR" -name "*.[zZ][iI][pP]" -exec
sh -c 'handle_many_files "$@"' ignored {} +

Note that I used the "-exec cmd ;" form explicitly because the "-exec cmd +"
form is newer.

> I think using '-type f' would not be wrong either, as otherwise we would
> match directories also.

Probably not a bad idea.
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
bss@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/
 
Old 07-30-2010, 10:55 AM
Chris Davies
 
Default Shell Expansion in Bourne Shell Script Question

Karl Vogel <vogelke+debian@pobox.com> wrote:
> for file in $(ls $MAGDIR/*.[Zz][Ii][Pp] 2> /dev/null); do ...

mkdir $MAGDIR/silly.zip
touch $MAGDIR/silly.zip/not-a-zip-file

If you're going to insist on using "ls" you should consider "ls -d".

Personally, I'd still go for this construct:

for FILE in "$MAGDIR"/*.[Zz][Ii][Pp]
do
test -f "$FILE" || continue
...

But each to our own.
Chris


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: tlnai7x1ch.ln2@news.roaima.co.uk">http://lists.debian.org/tlnai7x1ch.ln2@news.roaima.co.uk
 

Thread Tools




All times are GMT. The time now is 05:14 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org