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" = "" ] 
echo "The string is null" 

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

Let's write the script, 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.