0

May be a novice question but anyways in my intro to linux/unix class were touching on bash scripting and in one of the problems I got the it tasked me with making a script so if the user searched to a name in a file that wasn't there it would output a messaged saying 'your_input is not in the directory'

It says to use if statements and the exit status $?.

So far I got the input portion but I'm not sure how to properly use the $? in a if statement if its possible.

#!/bin/bash
name=$1

if [ "$name" = "" ]

   then echo -n "Enter a name to search for: "

        read name

fi

grep -i $name ~uli101/2014c/phonebook

if [ "$?" < "0" ]

   then echo "error"

fi

I get the error:

./phone4: line 14: 0: No such file or directory

My question is: How can I use the $? with and if statement, and If I can't, can you explain me how to use the $? in this problem?

Note: I did use echo $? to see how $? gave a 0 if grep worked and a 1 if it didn't.

1
  • You're looking to compare the value, right, not redirect the value "0"? Use -lt instead of < in if [ "$?" -lt "0" ] Commented Nov 30, 2014 at 0:36

4 Answers 4

3

There's two bugs in it. The one you already see is that in the [] expression, the < is interpreted not as "less than" but as stream redirection operator. The reason is that [ is just another program (an alias for test), so [ "$?" < "0" ] is similar to cat < filename. The other error is that you don't want to check for "less than" but for "not equal". In sum:

if [ "$?" < "0" ]

should be

if [ "$?" -ne "0" ]

Or you could write

if ! grep "$name" ~uli101/2014c/phonebook

...because if interprets a return code of zero as true and everything else as false.

Sign up to request clarification or add additional context in comments.

2 Comments

oh okay so like the -ne is the equivalent to 'less than'? I'm completely stupid I forgot the < is a redirect and not the less than operator, I also have intro to C this semester and I'm just linking the commands together, thank you for explaining
-ne is "not equal," -lt is "less than." See man test for more information.
0

never mind one of my friends pushed me in the right direction:

all I had to do is:

    if [ "$?" = "1" ]
       then echo "error"

fi

pretty much I was over thinking it, I just needed to say if $? = 1 then error, because in the readings it said $? can be greater then 1 I was trying to compensate for that.

1 Comment

The difference between right and correct is the $? being either zero, or not zero. So, it's actually 0, or it's bigger than zero. Unless you have a very good reason, you should NOT check on any specific non-zero value. You're just checking one very specific error message, not any error message. That's what is simple about zero : it is, or it isn't, and that means a lot. If it is one or not, that can mean very different things, depending on the program you run. You may be just lucky it works in this case now, but you should not check on the number 1.
0

It should be

if [ "$?" -gt 0 ]

The symbol '<' is a redirection operator, and it's not a Python or C - everything in a shell script is a command, including the text after 'if', and you are executing a command named '[' here, you may find it at the location /usr/bin/[, and this command uses -gt and -lt parameters to compare numbers, instead of '>' and '<', which are special shell operators.

You can rewrite this code like this:

if grep -i "$name" ~uli101/2014c/phonebook
  then true # 'true' is also a command, which does nothing and returns success
  else echo "Error"
fi

or even like this, using '||' operator, which will execute following command only if previous command returned an error:

grep -i "$name" ~uli101/2014c/phonebook || echo "Error"

Comments

0

The "$?" doesn't need quotes, as it is a number really. If you want better script, check on existance of the Phonebook file, and exit before asking the Name input if the file is missing. Also, if you reply nothing (enter only) on the READ command, you may need to do something.

#!/bin/bash

name=$1
phonebook=~/phonebook

if [ "$name" = "" ]
  then
  echo -n "Enter a name to search for: "
  read name
fi

grep -i "$name" $phonebook

if [ $? -gt 0 ]
  then
  echo "error, no \"$name\" in $phonebook"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.