Passing arguments--Error

Hi,
i have a file.txt with data
Bangalore
Chennai
Hyd

filename of the script is: new.sh

result=`cat file.txt | grep $1`
if  [ $result  -eq $1 ]
then
echo pass
else
echo fail
fi

i am executing the file in the cmd line as "sh new.sh Bangalore"
o/p is pass
if i give "sh new.sh delhi"
o/p is test:argument expected ( it does not say fail).

Kindly help, Thank you.

Needs a string comparison (not an integer comparision).

if [ "$result" = "$1" ]

The quotes are important or the command will fail if $result is blank.

This is Useless Use of Cat, this is sufficient:

result=`grep $1 file.txt`
1 Like

Wasn't this answered on your other post?

http://www.unix.com/unix-advanced-expert-users/177278-error-test-argument-expected-even-though-i-give-argument.html

Moreover it is useless use of test . :slight_smile:

if  [ $result  -eq $1 ]

instead use

if  `grep $1 file.txt`  

Bad post. This will cause the script to crash if presented with a valid parameter like "Bangalore" because it will try to execute a program called "Bangalore".
Try it.

Stick to test. It's a shell builtin nowadays.

There is a move against using backticks in modern Shells and using $(command) syntax instead. This does not work in basic Bourne Shell and we don't know what Operating System or Shell the O/P has.

1 Like

[quote=methyl;302600157]
Wasn't this answered on your other post?

Hi Methyl,

Thank you the solution worked, I agree it was answered before, i was getting into basic scripting and wanted to know where excatly i went wrong.

---------- Post updated at 10:06 AM ---------- Previous update was at 09:59 AM ----------

Hi Franklin,

Your solution works, it makes it much simpler, Thank you.:slight_smile: