开发者

Shell scripting illegal number error

开发者 https://www.devze.com 2023-02-23 09:29 出处:网络
I was checking whether a file exists or not but i get error with the following code filename=\"a.txt\"

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

精彩评论

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