Checking for null values

Many a time we need to check the value of variable, such as whether it is null. The null value means zero value. If we want to create the string with the null value, then we should use double quotes ("") while declaring it:

if [ "$string" = "" ] 
then 
echo "The string is null" 
fi 

We can even use [ ! "$string" ] or [ -z "$string" ] for null checking of strings.

Let's write the script if_08.sh, which will search for the entered person's name and tell us whether the user is on the computer system:

#!/bin/bash read -p "Enter a user name : " user_name # try to locate username in in /etc/passwd # grep "^$user_name" /etc/passwd > /dev/null status=$? if test $status -eq 0 then echo "User '$user_name' is found in /etc/passwd." ...

Get Learning Linux Shell Scripting - Second Edition 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.