shell: return values of test on file/directory
I'm not able to check the return values of the function test; man test didn't help me much.
#!/bin/bash
test=$(test -d $1)
if [ $test -eq 1 ]
then
echo "the file exists and is a directory"
elif [ $test -eq 0 ]
echo "file does not exist or is not a directory"
else
echo "error"
f开发者_Go百科i
Try, instead
if test -d $1
then
echo 'the file exists and is a directory'
else
echo 'the file doesn't exist or is not a directory'
fi
Every time you use test
on the return code of test
, God kills a kitten.
if test -d "$1"
or
if [ -d "$1" ]
$(test -d $1) is going to be substituted with what test outputs, not its return code. If you want to check its return code, use $?
, e.g.
test -d $1 test=$? if [ $test -eq 1 ] ...
精彩评论