0

I have the code below, but whenever I run it and enter an incorrect name it doesn't show the error message, it just goes blank.

#!/bin/bash

name=$1
if [ "$name" = "" ]
    then echo -n "Enter a name to search for: "
    read name
else
    echo "Name '$name' is not in directory"
fi
grep -i $name ~uli101/2015a/phonebook

1 Answer 1

2

Read the code: The "error message" is shown when $1 is not empty. You probably wanted something like

if ! grep -i "$name" ~uli101/2015a/phonebook ; then
    echo "Name '$name' is not in directory"
fi
Sign up to request clarification or add additional context in comments.

1 Comment

For good measure you can also write the message to stderr instead of stdout with echo >&2 "...."

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.