test: argument expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test: argument expected
# 1  
Old 04-23-2010
test: argument expected

Code:
# 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

Last edited by Scott; 04-23-2010 at 08:48 AM.. Reason: Code tags, please...
# 2  
Old 04-23-2010
Most likely $fname is blank, or contains spaces.

It's good practice to quote variables.

i.e.
Code:
if [ ! -e "$fname" ] #-d $fname

And please use code tags in future posts. Thank you.
# 3  
Old 04-23-2010
Thanks Scottn

right now my code looks like :
Code:
# 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 :
Code:
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 !!

Last edited by Scott; 04-23-2010 at 09:06 AM.. Reason: Code tags
# 4  
Old 04-23-2010
Hi.

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

Code:
$ ./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 [code] and [/code] 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
# 5  
Old 04-23-2010
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.

Code:
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.
# 6  
Old 04-23-2010
Quote:
Originally Posted by methyl
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.

Code:
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.
# 7  
Old 04-23-2010
yes thanks..
Methyl I have got it working now Smilie
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 !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test: argument expected

The following example prompts are passed into the shell script. $1 = /tmp/dir/ $2 = varies (test.txt, test1.txt, test2.txt...) $3 = test_YYYYMMDD.txt --------------------------------------------------------------------------- #!/bin/sh cd $1 if ; then if ; then ... (3 Replies)
Discussion started by: smkremer
3 Replies

2. Shell Programming and Scripting

Error- test: argument expected

check_build_info_table() { if then export build_info_table=`sqlplus -s sna/dbmanager <<! set pagesize 0 heading off feedback off SELECT DISTINCT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'XYZ' AND TABLE_NAME = 'MY_TABLE'; exit !` ... (3 Replies)
Discussion started by: ambarginni
3 Replies

3. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

4. Shell Programming and Scripting

error : test: argument expected

Hello all, I am trying to figure out why i am getting an error while executing the script...altought it seems like its work...but still get the test arguement error...any help would be appericiate...this script basically connects to any oracle db ( just have to pass db name to it)... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

5. Shell Programming and Scripting

Test: argument expected.

Hi, Since i am new to Unix and on suggestion on some smart guys on unix... i have decide to learn more deeply on Unix...so i was kind of playing with if statements and found this error... though i tried to correct is for hours now i couldnt find whats wrong in my loop. if then ... (4 Replies)
Discussion started by: bhagya2340
4 Replies

6. Shell Programming and Scripting

test: argument expected

+ test.sh: test: argument expected #!/bin/bash if then echo thennnn else echo elseeee fi why does it show this error? Clearly from debug mode, the argument is passed. I also tried if Run on Solaris 9. Thanks (10 Replies)
Discussion started by: lalelle
10 Replies

7. Shell Programming and Scripting

test: argument expected

I'm newbie to coding script so i found test: argument expected when i run it. please help me a=`df -k |awk '{print $5 }'|egrep "(100%|%)"|cut -d"%" -f1|tail -1` if then df -k|egrep "(100%|%)"|awk '{print $1,$5,$6}' else echo "No disk capacity more than 80%" fi thk in advance (7 Replies)
Discussion started by: unitipon
7 Replies

8. Shell Programming and Scripting

test: argument expected

Can someone help me with a very simple query I have the following script: #!/bin/sh VAR1="" if then VAR1="Message" fi echo $VAR1 put when i run it i get the following error test_job.sh: test: argument expected (5 Replies)
Discussion started by: andy202
5 Replies

9. UNIX for Dummies Questions & Answers

test:argument expected

Hi all, I am getting "test:argument expected" error in the following script LOGDIR=$XXAR_TOP/log PROGRAM_NAME=XXAR_GPS_LBFDMSGEN .. .. .. Check_Errors() { sqllogfile=$1 cd ${LOGDIR} countfile=${LOGDIR}/${PROGRAM_NAME}.tmp echo "countfile is " $countfile >> $LOGFILE echo... (4 Replies)
Discussion started by: rrs
4 Replies

10. Shell Programming and Scripting

ERROR-> test: argument expected , what does it mean?

I am trying to compare two integer variables in the if statement, but i am getting this "test:argument expected". What am i missing? Why is the if loop not executing correctly? trunkPCM="100000"; more $FILE |while read line do PCM=`echo $line | awk '{ print $2 }'` ... (4 Replies)
Discussion started by: tan102938
4 Replies
Login or Register to Ask a Question