Bash Script Ques

Kurt Wall kwall
Mon May 17 11:48:11 PDT 2004


An unnamed Administration source, Michael Hipp, wrote:
> Hello, first post (in a long time) ...
> 
> I thought to do something like:
> 
> if [ -f myfile* ]; then
>    # do something
> fi

I get the same error here. It is the [ that's issuing the error
message (yes, "[" is an actual program, a synonym for "test").

> 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?

cnt=$(ls -1 myfile* | wc -l)
if [ $cnt -ge 1 ] ; then
	echo ?only 1"
else
	echo "more than 1: $cnt"
done

That's a numeric one, not an ell. Consider:

$ ls [zZ]*
zcat*   zegrep*      zforce*  zipcloak*  zipnote*   zmore*  Zoo*
zcmp*   zeisstopnm*  zgrep*   zipgrep*   zipsplit*  znew*   zsoelim*
zdiff*  zfgrep*      zip*     zipinfo*   zless*     zoo*

The script xx has that little program in it

$ cnt=$(ls -1 z* | wc -l) && echo $cnt
19
$ cnt=$(ls -1 Z* | wc -l) && echo $cnt
1

Kurt
-- 
Chef, n.:
	Any cook who swears in French.


More information about the Linux-users mailing list