0

im trying to check if my script ran correctly if not, to echo a message, however when i use this if statement, it produces an error on line '9' (if [ $? -eq 0 ]) Saying that a ' is missing.

#!/bin/bash
name=$1
if ["$name" = ""]
then
 echo -n "Enter a name to search for: "
        read name
fi

if [ $? -eq 0 ]
then
 echo "Incorrect Input"
fi

1 Answer 1

1
if ["$name" = ""]

is wrong because you must have a space between the [ and the expression (and before the ] as well).

There's nothing obviously wrong with the syntax of

if [ $? -eq 0 ]

but it's always dubious to check exit codes with arithmetic, since that's what if does directly. If your script is as you show it above, the second if statement should always be true. The fact that the previous if statement's expression was false doesn't matter, because the if statement yields zero if no condition tested true. For example:

$ if false; then echo hi; fi; echo $?
0
Sign up to request clarification or add additional context in comments.

6 Comments

Well the first if statement runs fine. its the second one that keeps producing errors asking me to put a ' somewhere.
I think you're thinking into it too deep. Its a small assignment for my starter scripting class. It just echo a message if there was an incorrect input. thats it.
@AndrewBarsoom how does it know there's an incorrect input?
Because it searches through a phonebook list, if the user does not input a name within the phonebook, it throws an error, i want to catch that error and echo back saying its an invalid input.
@Andrew Did you leave out the statement that searches through the phonebook? Your comment suggets that the second if is looking to the result of the phonebook-search.
|

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.