Linux Tutorials
Building Hadoop Clusters On Linux In EC2
Installing And Using Hadoop
Setting Up SSH Keys Using SSH Agents And Tunnels
Creating OpenSSL Certificates and Certificate Authorities
Installing and configuring Xen
IPTables Primer
Linux Basic Bash Scripting

Linux Basic Bash Scripting

Conditional Statements

Linux conditional statements are similar to the conditional statements in other programming languages.

if ... then ... else

If statements in shell scripting function just like in any other language, based on conditions certain portions of code are executed or skipped. Bash scripting has many built in tests for checking/comparing files, exprssions, and strings. The if conditional statement is closed with the fi statement.

List of built in conditional tests:

-a FILE -- Checks if file exists -b FILE -- Checks if file is a block device -c FILE -- Checks if file is a character device -d FILE -- Checks if file is a directory -e FILE -- Checks if file exists. -f FILE -- File is a regular file and not a directory or device -g FILE -- Checks if file has sgid bit set -h FILE -- Checks if file is a symbolic link -k FILE -- Checks if file is a named pipe -n STING -- Checks if string is non zero -o OPTIONNAME -- Checks if shell OPTIONNAME is enabled -p FILE -- Checks if file is a pipe -r FILE -- Checks if file exists and is readable -s FILE -- Checks if file exists and has a greater than 0 size -t FD -- Checks if file descriptor is open and refers to a terminal -u FILE -- Checks if file exists and has SUID bit set -w FILE -- Checks if file exists and is writable -x FILE -- Checks if file exists and is executable -z STRING -- Checks if the length of the string is 0 -G FILE -- Checks if file exists and is owned by the effective GID
-L FILE -- Checks if file exists and is a symbolic link
-S FILE -- Checks if file exists and is a socket
FILE1 -nt FILE2 -- Checks if FILE1 has been modified since FILE2
FILE1 -ot FILE2 -- Checks if FILE1 is older than FILE2
STRING1 == STRING2 -- Checks if the strings are equal
STRING1 < STRING2 -- Checks if STRING1 sorts before STRING2
STRING1 > STRING2 -- Checks if STRING1 sorts after STRING2
STRING1 != STRING2 -- Checks if STRING1 does not equal STRING2
ARG1 OP ARG2 -- Uses -eq (equal) -ne (not equal) -lt (less than) -le (less than or equal) -gt (greater than) -ge (greater than or equal) for OP and checks integers ARG1 and ARG2

Example:

#!/bin/bash

FILENAME="/path/to/file"

if [ -a $FILENAME ]
then
echo "$FILENAME exists"
else
echo "$FILENAME doesn't exist"
fi

This simple script checks for and notifies about the existance of a file stored in the variable FILENAME. The syntax statement for if statements is if CONDITION then COMMANDS else COMMANDS fi.

Elif

The elif statement is used if you want your if conditional statement to have more than two possible paths.

Example:[:BR]

#!/bin/bash
FILENAME = /path/to/device

if [ -b $FILENAME ]
then
echo "$FILENAME is a block device"
elif [ -c $FILENAME ]
then
echo "$FILENAME is a character device"
else
echo "$FILENAME is other"
fi

Here we check to see if $FILENAME is a block device or a character device.

You can check multiple conditions by doing COND1 -a COND2 for requiring both the be true. Use COND1 -o COND2 for requiring COND1 or COND2 to be true.

Example:

#!/bin/bash
FILEA=/path/to/file
FILEB=/path/to/file

if [-b $FILEA -a -b $FILEB]
then
echo "$FILEA and $FILEB are block devices"
elif [-b $FILEA -o -b $FILEB]
then
echo "$FILEA or $FILEB is a block device"
else
echo "Neither $FILEA or $FILEB is a block device"

Variables <<  1 2 3 4 5  >> More conditional statements and exit status codes
New Content