- 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)
推薦閱讀
- Getting Started with Gulp(Second Edition)
- Kali Linux Web Penetration Testing Cookbook
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- INSTANT Sencha Touch
- 小程序開發原理與實戰
- C語言程序設計
- OpenStack Networking Essentials
- jQuery for Designers Beginner's Guide Second Edition
- 測試架構師修煉之道:從測試工程師到測試架構師
- Instant Automapper
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Learn Linux Quickly
- 輕松學Scratch 3.0 少兒編程(全彩)
- 你好!Python
- Android項目實戰:博學谷