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
-s
is 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
精彩评论