test: argument expected

# to search a file if it exists and whether its readable or not
# if yes print its first 5 lines
echo enter the filename to be searched
read fname
if [ ! -e $fname ] #-d $fname
then
echo file doesn exists
elif [ -d $fname ]
then
echo its a directory
elif [ -r $fname ]
then
cat $fname
else
echo its not readable
fi
# end of program

###########################
this program is running fine but is getting a warning message i guess
it says
/filesearch2.ksh[5]: test: argument expected
and then showing the result.
please suggest

Most likely $fname is blank, or contains spaces.

It's good practice to quote variables.

i.e.

if [ ! -e "$fname" ] #-d $fname

And please use code tags in future posts. Thank you.

right now my code looks like :

# to search a file if it exists and whether its readable or not
# if yes print its first 5 lines
echo enter the filename to be searched
read fname
if [ ! -e "$fname" ] #-d $fname
then
echo file doesn exists
elif [ -d "$fname" ]
then
echo its a directory
elif [ -r "$fname" ]
then
cat "$fname"
else
echo its not readable
fi
# end of program

but still showing the same error !!
and no.. my $fname is not blank as i am getting the contents of this filename as it is supposed to be.
but still getting the error/warning msg :

filesearch2.ksh[5]: test: argument expected

##########
and yes, i am not getting .. what u r referring when u say "code tag" . plz explain so that i'll post in my next threads to get served better !!

Hi.

It works for me in ksh. What shell are you using?

$ ./MyTest
enter the filename to be searched
blah
file doesn exists

It's also not bad practice to quote strings, like echo's, etc.

As for CODE-TAGS:

z.I.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

I can reproduce the error message in ksh. The test "-e" is not valid in "ksh". It is valid in POSIX shells such as bash.

Change
if [ ! -e $fname ]
To
if [ ! -f $fname ] 

It is also good practice to put a shebang line as the first line of your script to make it clear to other administrators which shell you prefer.

Are you saying that ksh is not a POSIX shell?

-e is perfectly valid in ksh.

In sh not, so I would suspect that to be the shell in use by the O/P.

yes thanks..
Methyl I have got it working now :slight_smile:
and Scott,
I understood ur concern and will take care posting "code tags" henceforth
many thanks//

---------- Post updated at 06:10 PM ---------- Previous update was at 06:04 PM ----------

Could any one help me out here , where i want the user to type some thing and..
i want this typed text to be assigned to a file and save.

scottn,
should i create another thread , or is it ok !!

@scottn
in general Korn Shell is not a POSIX shell but I believe that ksh93 is one.
I find the various inconsistent implementations of POSIX "standards" over the years tiresome and prefer ksh88 syntax for cross-platform portability. This includes using "cat" where necessary.

What O/S do you have where "test -e" works in ksh?

Hi Methyl.

AIX and Solaris 8 (it doesn't get much more ksh88 than on Solaris 8!)

Thanks scottn, I'll add that to my collection of quirks.