Error : test: argument expected

May 7, 2009 at 01:02:16
Specs: Unix
Hi,

When i execute the following script i'm getting test: argument expected error at if [ -z $file ]. Can someone plz advise.

if [ -z $file ]
then
echo " DID NOT ENTER A FILE NAME "
elif [ ! -f $file ]
then
echo " \t File ' $file ' Doesn't Exists In $PWD Directory \n"
else
echo " \t File ' $file ' Exists In $PWD Direcotry \n"
fi

Thanks.


See More: Error : test: argument expected

Report •


#1
May 7, 2009 at 07:02:57
Your script worked for me with no problems:

#!/bin/ksh

read file
if [ -z "$file" ]
then
   echo " DID NOT ENTER A FILE NAME "
elif [ ! -f "$file" ]
then
   echo " \t File ' $file ' Doesn't Exists In $PWD Directory \n"
else
   echo " \t File ' $file ' Exists In $PWD Direcotry \n"
fi

BTW, it's always a good idea to include the shell invocation on line 1 of your script. I'd surround $file with double quotes "$file"

Have you tried the extended test facility - two brackets instead of 1:


#!/bin/ksh

read file
if [[ -z $file ]]
then
   echo " DID NOT ENTER A FILE NAME "
elif [[ ! -f $file ]]
then
   echo " \t File ' $file ' Doesn't Exists In $PWD Directory \n"
else
   echo " \t File ' $file ' Exists In $PWD Direcotry \n"
fi


Report •
Related Solutions


Ask Question