Passing arguments--Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments--Error
# 1  
Old 02-20-2012
Passing arguments--Error

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

filename of the script is: new.sh
Code:
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.

Last edited by jim mcnamara; 02-20-2012 at 08:41 AM.. Reason: code tags please
# 2  
Old 02-20-2012
Needs a string comparison (not an integer comparision).

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

The quotes are important or the command will fail if $result is blank.
# 3  
Old 02-20-2012
Quote:
Originally Posted by harsha85
Code:
result=`cat file.txt | grep $1`

This is Useless Use of Cat, this is sufficient:
Code:
result=`grep $1 file.txt`

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 02-20-2012
Wasn't this answered on your other post?

https://www.unix.com/unix-advanced-ex...-argument.html
# 5  
Old 02-20-2012
Moreover it is useless use of test . Smilie
Code:
if  [ $result  -eq $1 ]

instead use
Code:
if  `grep $1 file.txt`

# 6  
Old 02-20-2012
Quote:
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.
This User Gave Thanks to methyl For This Post:
# 7  
Old 02-21-2012
[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.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

2. Shell Programming and Scripting

Passing arguments that contain space

hi All, i am trying to pass arguments that contain space , value will be stored in variables to be used further in script , i went thru previous posting , still its not clear to how to implement for my case. passing 3 args test.sh it is 'fun to work in unix' inside shell ... (3 Replies)
Discussion started by: gvkk
3 Replies

3. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

4. Programming

Passing arguments to shellcode

Is there any way I could pass arguments to shellcode. My goal is to store a program in a image file, and have another program read and run the code with arguments in memory. Currently I can store a program in a image file, then read it back to the hard-drive run it normally then delete it when... (5 Replies)
Discussion started by: image28
5 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

6. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

7. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

8. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies

9. UNIX for Dummies Questions & Answers

Passing arguments to an alias

I want to have an alias for the command fold -78 filename | lp How do I set my alias so that the argument passed is filename ?? alias lp='fold -78 | lp' then lp filename wont work cuase this is fold -78 | lp filename (1 Reply)
Discussion started by: pmcg
1 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question