Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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." ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content