开发者

shell: return values of test on file/directory

开发者 https://www.devze.com 2022-12-21 10:10 出处:网络
I\'m not able to check the return values of the function test; man test didn\'t help me much. #!/bin/bash

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 ]
...
0

精彩评论

暂无评论...
验证码 换一张
取 消