Records : 437
Scenario : Shell script test, [], [[]], (()) statement application.
Version : CentOS Linux release 7.9.2009.
1. test and [] and [[]] and (()) statement
test, the test command is used to check whether a certain condition is true. You can perform tests on numerical values, characters, and files.
[], the test command is abbreviated as []. Supports relational operators, etc.
[[ ]], supports logical operators.
Format: test expression
Format: [expression]
Format: [[ expression01 || expression02 ]]
Format: (( expression01 && expression02 ))
Note: There is a space between [] and expression, these two spaces are required, otherwise a syntax error will result during execution.
2. Use the test command
2.1 Script
Screenplay name: b2023052901.sh
Script content:
#!/bin/bash
echo "请分别输入GDP,人口数,以空格分割:"
read gdp population
#使用test表达式
if test ${gdp} -gt 30000 && test ${population} -lt 2000 ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是一类城市."
elif test ${gdp} -gt 20000 && test ${population} -lt 1500 ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是二类城市."
elif test ${gdp} -gt 10000 && test ${population} -lt 1000 ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是三类城市."
elif test ${gdp} -gt 5000 && test ${population} -lt 500 ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是四类城市."
else
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万."
fi
2.2 Execution and output
Execute the command: bash b2023052901.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052901.sh
请分别输入GDP,人口数,以空格分割:
30001 1999
城市GDP: 30001 亿, 人口: 1999 万,这是一类城市.
3. Use []
3.1 Script
Screenplay name: b2023052902.sh
Script content:
#!/bin/bash
echo "请分别输入GDP,人口数,以空格分割:"
read gdp population
#使用[]表达式
if [ ${gdp} -gt 30000 ] && [ ${population} -lt 2000 ] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是一类城市."
elif [ ${gdp} -gt 20000 ] && [ ${population} -lt 1500 ] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是二类城市."
elif [ ${gdp} -gt 10000 ] && [ ${population} -lt 1000 ] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是三类城市."
elif [ ${gdp} -gt 5000 ] && [ ${population} -lt 500 ] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是四类城市."
else
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万."
fi
3.2 Execution and output
Execute the command: bash b2023052902.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052902.sh
请分别输入GDP,人口数,以空格分割:
20001 1499
城市GDP: 20001 亿, 人口: 1499 万,这是二类城市.
4. Use [[ ]]
4.1 Script
Screenplay name: b2023052903.sh
Script content:
#!/bin/bash
echo "请分别输入GDP,人口数,以空格分割:"
read gdp population
#使用[[]]表达式
if [[ ${gdp} -gt 30000 && ${population} -lt 2000 ]] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是一类城市."
elif [[ ${gdp} -gt 20000 && ${population} -lt 1500 ]] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是二类城市."
elif [[ ${gdp} -gt 10000 && ${population} -lt 1000 ]] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是三类城市."
elif [[ ${gdp} -gt 5000 && ${population} -lt 500 ]] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是四类城市."
else
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万."
fi
4.2 Execution and output
Execute the command: bash b2023052903.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052903.sh
请分别输入GDP,人口数,以空格分割:
10001 999
城市GDP: 10001 亿, 人口: 999 万,这是三类城市.
5. Use (())
5.1 Script
Screenplay name: b2023052904.sh
Script content:
#!/bin/bash
echo "请分别输入GDP,人口数,以空格分割:"
read gdp population
#使用(())表达式
if (( ${gdp} > 30000 && ${population} < 2000)) ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是一类城市."
elif (( ${gdp} > 20000 && ${population} < 1500)) ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是二类城市."
elif (( ${gdp} > 10000 && ${population} < 1000)) ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是三类城市."
elif (( ${gdp} > 5000 && ${population} < 500)) ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是四类城市."
else
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万."
fi
5.2 Execution and output
Execute the command: bash b2023052904.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052904.sh
请分别输入GDP,人口数,以空格分割:
5001 499
城市GDP: 5001 亿, 人口: 499 万,这是四类城市.
6. Test and [] and [[]] and (()) statements are used in conjunction
6.1 Script
Screenplay name: b2023052905.sh
Script content:
#!/bin/bash
echo "请分别输入GDP,人口数,以空格分割:"
read gdp population
#test和[]和[[]]和(())语句联合使用
if test ${gdp} -gt 30000 && test ${population} -lt 2000 ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是一类城市."
elif [ ${gdp} -gt 20000 ] && [ ${population} -lt 1500 ] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是二类城市."
elif [[ ${gdp} -gt 10000 && ${population} -lt 1000 ]] ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是三类城市."
elif (( ${gdp} > 5000 && ${population} < 500)) ;then
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万,这是四类城市."
else
echo "城市GDP: ${gdp} 亿, 人口: ${population} 万."
fi
6.2 Execution and output
Execute the command: bash b2023052905.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052905.sh
请分别输入GDP,人口数,以空格分割:
30001 1999
城市GDP: 30001 亿, 人口: 1999 万,这是一类城市.
[root@hadoop211 tutorial]# bash b2023052905.sh
请分别输入GDP,人口数,以空格分割:
20001 1499
城市GDP: 20001 亿, 人口: 1499 万,这是二类城市.
[root@hadoop211 tutorial]# bash b2023052905.sh
请分别输入GDP,人口数,以空格分割:
10001 999
城市GDP: 10001 亿, 人口: 999 万,这是三类城市.
[root@hadoop211 tutorial]# bash b2023052905.sh
请分别输入GDP,人口数,以空格分割:
5001 499
城市GDP: 5001 亿, 人口: 499 万,这是四类城市.
[root@hadoop211 tutorial]# bash b2023052905.sh
请分别输入GDP,人口数,以空格分割:
30001 2001
城市GDP: 30001 亿, 人口: 2001 万.
Above, thanks.
May 29, 2023