thetux4
thetux4

Reputation: 1633

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?

Upvotes: 0

Views: 1789

Answers (1)

FerranB
FerranB

Reputation: 36827

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:

Upvotes: 1

Related Questions