19.5. Testing the values returned by a function

To test a value returned by a called function you can use the last status command directly after the function is called from your script. For example:

check_it_is_a_directory $FILENAME # this is the function call and check 
if [ $? = 0 ] # use the last status command now to test 
then 
  echo "All is OK" 
else 
  echo " Something went wrong!" 
fi

A much better way is to use an if statement when testing on either a return 0 or a return 1 value. Enclosing the function call with an if statement makes better logical reading. For example:

if check_it_is_a_directory $FILENAME; then 
  echo "All is OK" 
  # do something ?? 
else 
  echo "Something went wrong !" 
  # do something ?? 
fi

If the function is going to echo out ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.