Bash Script Ques
Joel Hammer
Joel
Mon May 17 11:48:11 PDT 2004
Well, you said elegant:
#!/bin/bash
dir -1 filex* 2>&1 | grep "No such"
if [ $? -eq 0 ] ; then
echo Dont got one
else
echo Got one
fi
There are two tricks in this simple script.
$? returns the error code of the last command. That's grep, hopefully.
Problem is, dir -1 doesn't send a message to stdout if no file is found,
so you have to redirect stderr to stdout before piping it to grep.
This next script takes advantage of the stdout stuff:
#!/bin/bash
a=`dir -1 filex*`
if [ -z "$a" ] ; then
echo Dont got one
else
echo Got one
fi
Note, I haven't tested these scripts very much.
Joel
On Sun, Jun 08, 2003 at 04:28:05PM -0500, Michael Hipp wrote:
> Hello, first post (in a long time) ...
>
> I thought to do something like:
>
> if [ -f myfile* ]; then
> # do something
> fi
>
> That's to find if there are one or more like myfile001, myfile002, but
> it reports an error "too many arguments" if there is more than one. Any
> simple/elegant way to do this?
>
> Thanks,
> Michael
>
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> Unsubscribe/Suspend/Etc -> http://www.linux-sxs.org/mailman/listinfo/linux-users
More information about the Linux-users
mailing list