I have a written a script named phone4 like the following:

# #!/bin/bash


  if ["$name" = ""]

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

        read name


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

but now i want to make some change in the script so that if I use the command phone4 xyz if no matching name is found, an appropriate message is displayed: "Name 'xyz' not in directory".

I want to use an 'if' statement to check the value of $? to see if the grep command was successful (remember, '0' indicates success). If the grep is NOT successful, then echo the message (which includes the value of $name).

Make sure this works with both command line arguments and with a name read in from the user, and I need to use the message EXACTLY as "Name 'xyz' not in directory". can anyone help me regarding this?

closed as off-topic by Rui F Ribeiro, Jeff Schaller, sebasth, LinuxSecurityFreak, Anthony Geoghegan Oct 26 '17 at 13:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Requests for learning materials (tutorials, how-tos etc.) are off topic. The only exception is questions about where to find official documentation (e.g. POSIX specifications). See the Help Center and our Community Meta for more information." – Rui F Ribeiro, sebasth, LinuxSecurityFreak
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    This looks like homework to me. – schaiba Oct 26 '17 at 6:46
  • You must be mistaken in the site. We are here for free. "Script solution needed" seems a good post for upwork, there they are paid. – Rui F Ribeiro Oct 26 '17 at 8:58
  • I'm voting to close this question as too broad because it's asking for extra code to provide new functionality rather than seeking help with a specific problem. – Anthony Geoghegan Oct 26 '17 at 13:34

Basically, you know how to write an if statement, you have one in your script.

Now, you should google to see how to compare $? to 0, and put that in your if statement. $? should NOT match 0.

Then you, you need to echo "Name 'xyz' not in directory" if $? is NOT 0. How can you replace 'xyz' with $name ? Should be obvious, right ?

This really reads like an assignment of some kind ... yes, I could write a complete answer, but that would NOT help you.

Give a man a fish, you feed him for the day, teach a man HOW to fish, you feed him for life.

Not the answer you're looking for? Browse other questions tagged or ask your own question.