Re: listing files without ls


Subject: Re: listing files without ls
From: Arthur Corliss (arthur@corlissfamily.org)
Date: Mon May 06 2002 - 19:40:02 AKDT


> 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:33:51 AKDT