How to write an echo statement if command returns no value in script?

115 views Asked by At

I have a custom script that checks some user information such as shell path, for example. I would like to add another statement to check if a user has a NIS Map, but I would like to write an "echo" command like "NO NIS MAP!" if it returns no value. Below is an example-

echo -e "NIS MAP"
ypcat -k auto.home.ndh | grep -i -w "$@"

I am expecting to write an if/else statement that will "echo" a statement if a NIS Map can't be located for a user.

1

There are 1 answers

0
GoinOff On

You could try something like:

echo -e "NIS MAP"
ypcat -k auto.home.ndh | grep -i -w "$@"
if [ "$?" = "0" ]
then 
  echo "Found NIS MAP"
else
  echo "NO NIS MAP!"
fi

If grep can't find what it's looking for it should return 1. $? looks at the return value from the previous command.