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-28-2010, 11:33 AM
Martin McCormick
 
Default Shell Expansion in Bourne Shell Script Question

I could have sworn I have done this before but obviously
not because I can't get it to work no matter what I try.

I am running a shell script that is supposed to find
every .zip or .ZIP file in a directory and do an extraction of
the contents. I don't want any other files to be included in the
script as they are not zip files. What works exactly from the
command line is:

ls *.[Zz][Ii][Pp]

I get any .zip or .ZIP file that is in the directory. In the
script, there is some code as follows:

for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
#lots of other stuff
done

I think I have tried about every form of escaping there
is and the error is that *.[Zz][Ii][Pp] is no such file or
directory.

If I leave out the attempted regular expression of
*.[Zz][Ii][Pp], the loop works but then any other non-zip or
non-ZIP files get processed.

Many thanks for any suggestions or explanations as to
why this does not seem to work.

Martin McCormick


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 201007281133.o6SBX9eQ065793@dc.cis.okstate.edu">ht tp://lists.debian.org/201007281133.o6SBX9eQ065793@dc.cis.okstate.edu
 
Old 07-28-2010, 11:51 AM
Cesar Garcia
 
Default Shell Expansion in Bourne Shell Script Question

Perhaps, try with this:

for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`; do





El 28/07/10 13:33, Martin McCormick escribió:
> I could have sworn I have done this before but obviously
> not because I can't get it to work no matter what I try.
>
> I am running a shell script that is supposed to find
> every .zip or .ZIP file in a directory and do an extraction of
> the contents. I don't want any other files to be included in the
> script as they are not zip files. What works exactly from the
> command line is:
>
> ls *.[Zz][Ii][Pp]
>
> I get any .zip or .ZIP file that is in the directory. In the
> script, there is some code as follows:
>
> for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
> #lots of other stuff
> done
>
> I think I have tried about every form of escaping there
> is and the error is that *.[Zz][Ii][Pp] is no such file or
> directory.
>
> If I leave out the attempted regular expression of
> *.[Zz][Ii][Pp], the loop works but then any other non-zip or
> non-ZIP files get processed.
>
> Many thanks for any suggestions or explanations as to
> why this does not seem to work.
>
> Martin McCormick
>
>
>


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4C5019DB.6040902@gmail.com">http://lists.debian.org/4C5019DB.6040902@gmail.com
 
Old 07-28-2010, 12:06 PM
Jordon Bedwell
 
Default Shell Expansion in Bourne Shell Script Question

On 7/28/10 6:33 AM, Martin McCormick wrote:

I could have sworn I have done this before but obviously
not because I can't get it to work no matter what I try.

I am running a shell script that is supposed to find
every .zip or .ZIP file in a directory and do an extraction of
the contents. I don't want any other files to be included in the
script as they are not zip files. What works exactly from the
command line is:

ls *.[Zz][Ii][Pp]

I get any .zip or .ZIP file that is in the directory. In the
script, there is some code as follows:

for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
#lots of other stuff
done

I think I have tried about every form of escaping there
is and the error is that *.[Zz][Ii][Pp] is no such file or
directory.

If I leave out the attempted regular expression of
*.[Zz][Ii][Pp], the loop works but then any other non-zip or
non-ZIP files get processed.

Many thanks for any suggestions or explanations as to
why this does not seem to work.

Martin McCormick




#!/bin/sh
for MAGFILE in $(ls *.[zZ][iI][pP])
do
echo "File: $MAGFILE";
done

I would prefer to rely on $() before `` in a bash script.


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

Archive: 4C501D35.1020907@envygeeks.com">http://lists.debian.org/4C501D35.1020907@envygeeks.com
 
Old 07-28-2010, 12:08 PM
Jordon Bedwell
 
Default Shell Expansion in Bourne Shell Script Question

On 7/28/10 7:06 AM, Jordon Bedwell wrote:


#!/bin/sh
for MAGFILE in $(ls *.[zZ][iI][pP])
do
echo "File: $MAGFILE";
done

I would prefer to rely on $() before `` in a bash script.



Sorry, I did that script on OS X, you should switch the SH shebang to
Bash, it's just aliased on OS X but not on Debian, but in your case it
probably won't matter that much since you aren't doing anything but a
for loop so far that I can see.



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

Archive: 4C501DB6.5000107@envygeeks.com">http://lists.debian.org/4C501DB6.5000107@envygeeks.com
 
Old 07-28-2010, 12:42 PM
Jochen Schulz
 
Default Shell Expansion in Bourne Shell Script Question

Martin McCormick:
>
> ls *.[Zz][Ii][Pp]

Note that 'ls' doesn't see this pattern at all. The pattern is expanded
by the shell to all existing files matching the pattern. This list of
files is then passed to ls. Using 'echo' would yield (almost) the same
result in this case.

> for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
> #lots of other stuff
> done

I think you meant to write

for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`

instead. What is happening with your version is the following:

- the shell tries to find files matching your pattern in the current
directory
- since there is no file matching your pattern, it leaves the pattern
unchanged
- $MAGDIR is expanded
- the unexpanded pattern and the content of $MAGDIR are passes to 'ls'
- 'ls' prints an error message for your pattern and lists the content of
$MAGDIR

Another hint: you don't need 'ls' for your case at all. 'ls' without any
parameter just prints out the names of the files that have been passed
to it by the shell. You can get away without the command substitution:

for MAGFILE in $MAGDIR/*.[Zz][Ii][Pp]
# …
done


J.
--
Watching television is more hip than actually speaking to anyone.
[Agree] [Disagree]
<http://www.slowlydownward.com/NODATA/data_enter2.html>
 
Old 07-28-2010, 01:27 PM
Martin McCormick
 
Default Shell Expansion in Bourne Shell Script Question

Cesar Garcia writes:
> Perhaps, try with this:
>
> for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`; do

That worked. Thank you.

As soon as I saw the example, I realized that in the
script, there was no way for it to know where these files were
that I was looking for. Also my thanks to the others who replied
with equally useful information.

Martin


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 201007281327.o6SDRWLZ070271@dc.cis.okstate.edu">ht tp://lists.debian.org/201007281327.o6SDRWLZ070271@dc.cis.okstate.edu
 
Old 07-28-2010, 03:21 PM
Mart Frauenlob
 
Default Shell Expansion in Bourne Shell Script Question

On 28.07.2010 14:42, Jochen Schulz wrote:

Martin McCormick:


ls *.[Zz][Ii][Pp]


Note that 'ls' doesn't see this pattern at all. The pattern is expanded
by the shell to all existing files matching the pattern. This list of
files is then passed to ls. Using 'echo' would yield (almost) the same
result in this case.


for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
#lots of other stuff
done


I think you meant to write

for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`

instead.

[...]


Another hint: you don't need 'ls' for your case at all. 'ls' without any
parameter just prints out the names of the files that have been passed
to it by the shell. You can get away without the command substitution:

for MAGFILE in $MAGDIR/*.[Zz][Ii][Pp]
# …
done


It's actually the prefered way (quoting "$MAGDIR" would also be
encouraged). Otherwise word splitting occurs on the list (files and
directories with spaces in their names will fail).
set +f is also required, which is the default, but sometimes turned off
in non-interactive shells.



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: 4C504AF3.1070807@chello.at">http://lists.debian.org/4C504AF3.1070807@chello.at
 
Old 07-28-2010, 05:22 PM
Clive Standbridge
 
Default Shell Expansion in Bourne Shell Script Question

> for MAGFILE in `ls *.[Zz][Ii][Pp] $MAGDIR/`; do
> #lots of other stuff
> done

As others noted, the ls command is superfluous and possibly harmful
here.

One more thing you can do is case-insensitive pathname expansion:

shopt -s nocaseglob
for MAGFILE in $MAGDIR/*.zip
do
#lots of other stuff
done

That will work with bash (begin your script with #!/bin/bash) but not
with dash as far as I know.




> If I leave out the attempted regular expression of
> *.[Zz][Ii][Pp], the loop works but then any other non-zip or
> non-ZIP files get processed.

Note that that is not a regular expression, it's a shell pattern or
glob pattern. They are different, and used in different contexts. Both
are immensely useful.


Cheers,


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20100728172239.GA20159@rimmer.esmertec.com">http://lists.debian.org/20100728172239.GA20159@rimmer.esmertec.com
 
Old 07-28-2010, 05:29 PM
Paul E Condon
 
Default Shell Expansion in Bourne Shell Script Question

On 20100728_082732, Martin McCormick wrote:
> Cesar Garcia writes:
> > Perhaps, try with this:
> >
> > for MAGFILE in `ls $MAGDIR/*.[Zz][Ii][Pp]`; do

It probably doesn't really matter in practice, but this will
pick up also files that match $MAGDIR/*.zIp , etc. (mixed case)

To avoid getting these, try

for MAGFILE in `ls $MAGDIR/*.ZIP $MAGDIR/*.zip`; do

I haven't tested this, but I'm pretty sure it will work. I
prefer this in a script because I find it much faster to
aprehend what is intended when reading old code.


>
> That worked. Thank you.
>
> As soon as I saw the example, I realized that in the
> script, there was no way for it to know where these files were
> that I was looking for. Also my thanks to the others who replied
> with equally useful information.
>
> Martin
>
>
> --
> To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> Archive: http://lists.debian.org/201007281327.o6SDRWLZ070271@dc.cis.okstate.edu
>

--
Paul E Condon
pecondon@mesanetworks.net


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20100728172925.GB3449@big.lan.gnu">http://lists.debian.org/20100728172925.GB3449@big.lan.gnu
 
Old 07-28-2010, 06:05 PM
 
Default Shell Expansion in Bourne Shell Script Question

>> 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". Try your script when MAGDIR doesn't
have any zipfiles, and MAGFILE will hold the expanded value of $MAGDIR
with the string "*.[Zz][Ii][Pp]" appended. Bash, sh, and ksh all do
that for some dopey reason, which is why I would do

for file in $(ls $MAGDIR/*.[Zz][Ii][Pp] 2> /dev/null); do ...

if there's any chance that you won't find any files.

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

Love and respect for the great American experiment
in free government does not appear out of thin air. --Larry P. Arnn


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20100728180522.353A1BED9@kev.msw.wpafb.af.mil">htt p://lists.debian.org/20100728180522.353A1BED9@kev.msw.wpafb.af.mil
 

Thread Tools




All times are GMT. The time now is 02:37 AM.

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