- Bash Cookbook
- Ron Brash Ganesh Naik
- 159字
- 2021-07-23 19:17:29
Evaluating binary numbers
Let's say we want to introduce another if condition and use elif (short for else if):
#!/bin/bash
AGE=21
if [ ${AGE} -lt 18 ]; then
echo "You must be 18 or older to see this movie"
elif [ ${AGE} -eq 21 ]; then
echo "You may see the movie and get popcorn"
else
echo "You may see the movie!"
exit 1
fi
echo "This line might not get executed"
If AGE is set and equals 21, then the snippet will echo:
You may see the movie and get popcorn
This line might not get executed
Using if, elif, and else, combined with other evaluations, we can execute specific branches of logic and functions or even exit our script. To evaluate raw binary variables, use the following operators:
- -gt (greater than >)
- -ge (greater or equal to >=)
- -lt (less than <)
- -le (less than or equal to <=)
- -eq (equal to)
- -nq (not equal to)
推薦閱讀
- Spring 5.0 By Example
- Progressive Web Apps with React
- Windows系統管理與服務配置
- Django開發從入門到實踐
- 微服務設計原理與架構
- 編寫整潔的Python代碼(第2版)
- Learning SQLite for iOS
- WebRTC技術詳解:從0到1構建多人視頻會議系統
- Learning Continuous Integration with TeamCity
- Building Wireless Sensor Networks Using Arduino
- PHP 7從零基礎到項目實戰
- Troubleshooting Citrix XenApp?
- 零基礎學Scratch 3.0編程
- Java7程序設計入門經典
- HTML 5與CSS 3權威指南(第4版·上冊)