6 Bash Conditional Expression Examples ( -e, -eq, -z, !=, [, [[ ..)

Bash expression is the combination of operators, features, or values used to form a bash conditional statement. Conditional expression could be binary or unary expression which involves numeric, string or any commands whose return status is zero when success.

There are several conditional expressions that could be used to test with the files. Following are few conditional expressions that are helpful.

  • [ -e filepath ] Returns true if file exists.
  • [ -x filepath ] Returns true if file exists and executable.
  • [ -S filepath ] Returns true if file exists and its a socket file.
  • [ expr1 -a expr2 ] Returns true if both the expression is true.
  • [ expr1 -o expr2 ] Returns true if either of the expression1 or 2 is true.

For more conditional expression to check the files, strings and numerics please refer the bash man page.

Bash Example 1. Check File Existence

The following Bash shell script code-snippet gets the filename with its absolute path, and checks if the file exists or not and it throws the appropriate information.

$ cat exist.sh
#! /bin/bash
file=$1
if [ -e $file ]
then
	echo -e "File $file exists"
else
	echo -e "File $file doesnt exists"
fi

$ ./exist.sh /usr/bin/boot.ini
File /usr/bin/boot.ini exists

Refer to our previous article to understand the various bash if statement types.

Bash Example 2. Compare Numbers

The below script reads two integer numbers from user, and checks if both the numbers are equal or greater or lesser than each other.

$ cat numbers.sh
#!/bin/bash
echo "Please enter first number"
read first
echo "Please enter second number"
read second

if [ $first -eq 0 ] && [ $second -eq 0 ]
then
	echo "Num1 and Num2 are zero"
elif [ $first -eq $second ]
then
	echo "Both Values are equal"
elif [ $first -gt $second ]
then
	echo "$first is greater than $second"
else
	echo "$first is lesser than $second"
fi

$ ./numbers.sh
Please enter first number
1
Please enter second number
1
Both Values are equal

$ ./numbers.sh
Please enter first number
3
Please enter second number
12
3 is lesser than 12

If you are new to bash scripting, refer to our Bash Introduction tutorial.

Bash Example 3. Basic Arithmetic Calculator

This examples reads input, which is a type of arithmetic operation wants to perform on bash variables (inp1 and inp2). The arithmetic operation could be addition, subtraction or multiplication..

$ cat calculator.sh
#!/bin/bash
inp1=12
inp2=11
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo -n "Please choose a word [1,2 or 3]? "
read oper

if [ $oper -eq 1 ]
then
	echo "Addition Result " $(($inp1 + $inp2))
else
	if [ $oper -eq 2 ]
	then
		echo "Subtraction Result " $(($inp1 - $inp2))
	else
		if [ $oper -eq 3 ]
		then
			echo "Multiplication Result " $(($inp1 * $inp2))
		else
			echo "Invalid input"
		fi
	fi
fi

$ ./calculator.sh
1. Addition
2. Subtraction
3. Multiplication
Please choose a word [1,2 or 3]? 4
Invalid input

Knowing how to use the bash special parameters ( $*, $@, $#, $$, $!, $?, $-, $_ ) will make your scripting life easy.

Bash Example 4. Read and Ping IP address

The following script is used to read the IP address and check whether the IP address is reachable, and prints the appropriate message.

$ cat ipaddr.sh
#!/bin/bash
echo "Enter the Ipaddress"
read ip

if [ ! -z $ip ]
then
	ping -c 1 $ip
	if [ $? -eq 0 ] ; then
		echo "Machine is giving ping response"
	else
		echo "Machine is not pinging"
	fi
else
	echo "IP Address is empty"
fi

$ ./ipaddr.sh
Enter the Ipaddress
10.176.191.106

Pinging 10.176.191.106 with 32 bytes of data:

Reply from 10.176.191.106: bytes=32 time<1ms TTL=128

Ping statistics for 10.176.191.106:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
Machine is giving ping response

In this example, -z returns true if ipaddress is zero length, When the condition is preceded by ! (negate) operator, if expression is false, it enters into if part and executes. So when the IP address is not null, it enters and checks whether the ip address is reachable.

Bash Example 5. Installer Script

Installer script of most of the packages will not allow to execute those as a root user. Script checks the user who is executing and throws the error.

The following script, allows you to execute the oracle installer script only if the user who is executing is non root.

$ cat preinstaller.sh
#!/bin/bash
if [ `whoami` != 'root' ]; then
	echo "Executing the installer script"
	./home/oracle/databases/runInstaller.sh
else
	echo "Root is not allowed to execute the installer script"
fi

Executing the script as a root user,
# ./preinstaller.sh
Root is not allowed to execute the installer script

In this example the output of the command whoami is compared with the word “root”. For string comparison ==, !=, < and should be used and for numeric comparison eq, ne,lt and gt should be used.

Bash Example 6. Enhanced brackets

In all the above examples, we used only single brackets to enclose the conditional expression, but bash allows double brackets which serves as an enhanced version of the single-bracket syntax.

$ cat enhanced.sh
#!/bin/bash
echo "Enter the string"
read str
if [[ $str == *condition* ]]
then
	echo "String "$str has the word \"condition\"
fi

$ ./enhanced.sh
Enter the string
conditionalstatement
String conditionalstatement has the word "condition"
  • [ is a synonym for test command. Even if it is built in to the shell it creates a new process.
  • [[ is a new improved version of it, which is a keyword, not a program.
  • [[ is understood by Korn and Bash.
  • In the above example, if the variable $str contains the phrase “condition” anywhere, the condition is true.
  • This is the shell globbing feature, which will be supported only when you use [[ (double brackets) and therefore many arguments need not be quoted.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值