Shell script-conditional judgment
1. Condition test
test 条件表达式
Example format①:
- test 1 == 1
Testing 1
is equal to 1
the return value 0
is thetrue
Example format②:
- [ 1 == 1 ]
2. File testing
[ 操作符 文件或目录 ]
Commonly used test operators
- -d test whether it is a directory or whether it exists
- -e test whether the file or directory exists
- -f test whether it is a file
- -r test whether the current user has permission to read
- -w test whether the current user has permission to write
- -x test whether the current user has permission to execute
[ 操作符 文件或目录 ] && echo "YES"
For example:
- [ -e a ] & echo “target found”
Three, integer value comparison
[ 整数1 操作符 整数2 ]
Commonly used test operators
- -eq is equal to
- -ne is not equal to
- -gt is greater than
- -lt is less than
- -le is less than or equal to
- -ge is greater than or equal to
For example:
[ `who | wc-l` -gt 5 ] && echo "没有超额"
Example: Simple regular inspection
a=`free -m | grep "Mem" | awk '{print $4}'`
[ $a -lt 100 ] && echo "剩余"${a}MB
if [ $a -lt 100 ]
then reboot fi;
Fourth, string comparison
Commonly used test operators
- = String content is the same
- != The string content is different,! Sign means the opposite
- -z string content is empty
- n string has content
[ str1 = str2 ]
= And == have the same function
[str1 != str2] Is the string in the variable different?
[-z str1] Check that the string content is empty
Spaces are also strings
[-n str1] Check whether there is a string
Variables without double quotes will be judged as simple strings
For example:
[ $PWD = `pwd` ] && echo "The current directory is `pwd`"
For example:
[ "en_US" != $LANG ] && echo "not en_US"
Five, logic test
[ 表达式1 ] 操作符 [ 表达式2 ]
命令1 操作符 命令2
Commonly used test operators
- -a or &&: logical and, and the meaning
- -o or ||: logical or, or meaning
- !: Logical no
[ $a -ne 1 ] && [ $a != 2 ] 等同于 [ $a -ne 1 -a $a != 2 ]
For example:
- echo [ “1” == “1” ] && echo “true” || echo “yes”
As long as one does not set up to perform the next
note: &&
, ||
operator can exist normal [[ ]]
condition decision structure, but if it appears in a [ ]
structure, the error will be
Example: Use PING to determine whether the website is in stock
- -c: the number of packets sent
- -i: time interval for sending packets
- -w: how many seconds to stop
ping -c 3 -i 0.5 -w 2 baidu.com > /dev/null && echo "$1 online" || echo "$1 off"
ping -c 3 -i 0.5 -w 2 example.com > /dev/null && echo "$1 online" || echo "$1 off"
Example: multiple judgments:
[ -d /boot ] && [ -r /boot ] && echo "you can read"
Six, judgment sentence
if statement
Condition is true
performed when then
the back part is executed
determination must be added last fi
to be closed
if condition
then
statement1
fi
Example: It
(())
is a mathematical calculation command. In addition to the most basic addition, subtraction, multiplication and division operations, it can also perform relational operations such as greater than, less than, equal to, and, or, and non-logical operations.
#!/bin/bash
if ((10 > 1))
then
echo "true"
fi
if else statement
If condition
is true
, will perform statement1
, as is false
, will be executedstatement2
if condition
then
statement1
else
statement2
fi
For example:
#!/bin/bash
read input_1
read input_2
if (( $input_1 == $input_2 ))
then
echo "${input_1}=${inut_2}"
else
echo "${input_1}!=${input_2}"
fi
if elif else statement
As condition1
to true
the time, will be performed statement1
, will next be determined condition2
, if true
the execution statement2
, from the beginning if condition1
it is false
to be performed when else
the statement after thestatementn3
if condition1
then
statement1
elif condition2
then
statement2
else
statementn3
fi
For example:
#!/bin/bash
if ((10 > 1))
then
echo "1"
elif ((2 > 1))
then
echo "2"
else
echo "3"
fi
case in statement
►The beginning of the case line must start with the word "in", and each pattern must end with a single right bracket ")"►Double
semicolon";;" indicates the end of the command sequence
►In the pattern string, square brackets can be used to indicate a continuous Range, such as "[0-9]"; ► You can also use a vertical bar | to indicate or, such as a|b
►*) to indicate the default mode, where * is equivalent to a wildcard
case 变量值 in
判断1)
命令序列
;;
判断2)
命令序列
;;
……
*)
默认命令序列
esac
For example:
read num -p "请输入数字:"
case $num in
1)
echo "1"
;;
2)
echo "2"
;;
*)
echo "error"
esac