RE: listing files without ls


Subject: RE: listing files without ls
From: Mike Barsalou (mbarsalou@aidea.org)
Date: Tue May 07 2002 - 13:36:05 AKDT


That's because I don't know how to do that! Until now! Thanks Arthur.

-----Original Message-----
From: Arthur Corliss [mailto:arthur@corlissfamily.org]
Sent: Monday, May 06, 2002 7:40 PM
To: Mike Barsalou; aklug@aklug.org
Subject: Re: listing files without ls

> While playing around with scripting, I ran across a trick I thought
> worth sharing:
>
> You can use:
>
> echo m*
>
> to do the same as:
>
> ls m*
>
> However, with the echo command, you will get back the m* if there
> are no file names that match the expression. This is good because
> in a script like this:
>
> #!/bin/bash
> filenames=`ls m*`
> for foo in filenames;
> do
> echo $foo
> done
>
> you would get an error of 'no such file or directory' from your script.
> With the echo you would get 'm*'. So you could add a conditional
> like this:
>
> #!/bin/bash
> filenames=`echo m*`
> if [ "$filenames" == "m*"]
> then
> for foo in filenames;
> do
> echo $foo
> done
> else
> Echo "There aren't any"
> fi
>
> Just thought I would share.

Sounds a little convoluted. Why not just do:

   filenames=`ls m* 2> /dev/null`
   if [ $? ne 0 ]; then

or some such? Remember, exit codes are the bread and butter of automated
processes. Get in the habit of using them, and you'll find that you can
have
scripts do intelligent adaptation based on the codes returned.

--

--Arthur Corliss Bolverk's Lair -- http://arthur.corlissfamily.org/ Digital Mages -- http://www.digitalmages.com/ "Live Free or Die, the Only Way to Live" -- NH State Motto

--------- To unsubscribe, send email to <aklug-request@aklug.org> with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Tue May 07 2002 - 13:38:45 AKDT