PDA

View Full Version : bash: why does this not work?



newbie22
March 7th, 2007, 03:00 PM
if [ -f *rc ]; then echo true; else echo false; fi
Why does this not work even though there is a *rc file?

Ramses de Norre
March 7th, 2007, 03:12 PM
Try this:
for i in *rc
do
if [ -f $i ]
then
echo true
else
echo false
fi
done

EDIT: forgot a '$' as pointed out ;)

duff
March 7th, 2007, 03:43 PM
^^^ if [ -f $i ] ^^^

cwaldbieser
March 8th, 2007, 02:37 PM
if [ -f *rc ]; then echo true; else echo false; fi
Why does this not work even though there is a *rc file?

Because the shell first expands *rc to the actual filenames, so you end up with something like:


if [ -f onerc tworc threerc ]; then echo true; else echo false; fi

That syntax doesn't make sense, so you probably get "bash: [: too many arguments".

Mr. C.
March 9th, 2007, 03:47 AM
See "man test" and search for -f and friends. Get clear in your mind what FILE means.

MrC