开发者

Shell scripting illegal number error

I was checking whether a file exists or not but i get error with the following code

filename="a.txt"
if [ -s $filename ] ; then  
    echo "exists"
else
    echo "not exists"
fi

It gives the开发者_如何转开发 error [: 116: Illegal number

What could be the problem?


You have to use -f:

filename="a.txt"
; touch $filename
; echo $filename
if [ -f "$filename" ] ; then  
    echo "exists"
else
    echo "not exists"
fi

-sis to check that "FILE exists and is a socket".

Notes:

  • uncomment the touch sentence to ensure the file exists.
  • uncomment the echo $filename sentence to ensure he var content.
  • Try to enclose with " to ensure no space or special chars inside $filename.

References:

  • Man page for test
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜