Record: 431
Scenario: Shell script if else judgment statement application. General format: if then fi, if then else fi, if then elif then else fi, if then elif then elif then else fi.
Version: CentOS Linux release 7.9.2009.
1.if else common format
1.1 Format 1: if then fi
if condition then command1 ... commandN fi
1.2 Format 2: if then else fi
if condition then commandA else commandB fi
1.3 format three: if then elif then else fi
if condition1 then commandA elif condition1 then commandB else command C fi
2.if else judgment statement (()) and [] and [[]]
2.1 Use (()) judgment statement
In the judgment statement (()), operators such as >, <, !=, etc. are used.
2.2 Use test and [] and [[]] to judge the statement
Format: test expression
Format: [expression]
In the judgment statement test and […], operators such as -eq, -ne, -gt, lt, -ge, and le are used.
Note: The test command is abbreviated as [], therefore, test and [] are equivalent. The space between [] and expression, these two spaces are required, otherwise a syntax error will result during execution.
Example: [ expression], when executing this command, an error will be reported: [: missing `]’.
3. Use if then fi statement
3.1 Use (()), test, [], [[]] judgment statement
3.2 script
Script name: b2023052301.sh
Script content:
#!/bin/bash echo "Please enter GDP and population separately, separated by spaces:" read gdp population # 1. Use the (()) expression if (( ${gdp} > 20000 & amp; & amp; ${population} < 1500)) then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." the fi # 2. Use the test expression if test ${gdp} -gt 20000 & amp; & amp; test ${population} -lt 1500 then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." the fi # 3. Use [] expressions if [ ${gdp} -gt 20000 ] & amp; & amp; [ ${population} -lt 1500 ] then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." the fi # 4. Use [] expression, you can also use [[]] directly if [[ ${gdp} -gt 20000 & amp; & amp; ${population} -lt 1500 ]] then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." fi
3.3 Execution and output
Execute the command: bash b2023052301.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052301.sh Please enter GDP and population separately, separated by spaces: 21235 1312 City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city.
4. Use if then else fi statement
4.1 Use (()), test, [], [[]] judgment statement
4.2 script
Script name: b2023052302.sh
Script content:
#!/bin/bash echo "Please enter GDP and population separately, separated by spaces:" read gdp population # 1. Use the (()) expression if (( ${gdp} > 20000 & amp; & amp; ${population} < 1500)) then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 2. Use the test expression if test ${gdp} -gt 20000 & amp; & amp; test ${population} -lt 1500 then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 3. Use [] expressions if [ ${gdp} -gt 20000 ] & amp; & amp; [ ${population} -lt 1500 ] then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 4. Use [] expression, you can also use [[]] directly if [[ ${gdp} -gt 20000 & amp; & amp; ${population} -lt 1500 ]] then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." else echo "City GDP: ${gdp} million, population: ${population} million." fi
4.3 Execution and output
Execute the command: bash b2023052302.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052302.sh Please enter GDP and population separately, separated by spaces: 21235 1312 City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. [root@hadoop211 tutorial]# bash b2023052302.sh Please enter GDP and population separately, separated by spaces: 11235 1312 City GDP: 1123.5 billion, population: 13.12 million. City GDP: 1123.5 billion, population: 13.12 million. City GDP: 1123.5 billion, population: 13.12 million. City GDP: 1123.5 billion, population: 13.12 million.
5. Use if then elif then else fi statement
5.1 Use (()), test, [], [[]] judgment statement
5.2 script
Script name: b2023052303.sh
Script content:
#!/bin/bash echo "Please enter GDP and population separately, separated by spaces:" read gdp population # 1. Use the (()) expression if (( ${gdp} > 20000 & amp; & amp; ${population} < 1500)) ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." elif (( ${gdp} > 10000 & amp; & amp; ${population} < 800)) ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a competitive city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 2. Use the test expression if test ${gdp} -gt 20000 & amp; & amp; test ${population} -lt 1500 ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." elif test ${gdp} -gt 10000 & amp; & amp; test ${population} -lt 800 ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a competitive city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 3. Use [] expressions if [ ${gdp} -gt 20000 ] & amp; & amp; [ ${population} -lt 1500 ] ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." elif [ ${gdp} -gt 10000 ] & amp; & amp; [ ${population} -lt 800 ] ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a competitive city." else echo "City GDP: ${gdp} million, population: ${population} million." the fi # 4. Use [] expression, you can also use [[]] directly if [[ ${gdp} -gt 20000 & amp; & amp; ${population} -lt 1500 ]] ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a big city." elif [[ ${gdp} -gt 10000 & amp; & amp; ${population} -lt 800 ]] ;then echo "City GDP: ${gdp} million, population: ${population} million, this is a competitive city." else echo "City GDP: ${gdp} million, population: ${population} million." fi
5.3 Execution and output
Execute the command: bash b2023052303.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052303.sh Please enter GDP and population separately, separated by spaces: 21235 1312 City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. City GDP: 2123.5 billion, population: 13.12 million, this is a big city. [root@hadoop211 tutorial]# bash b2023052303.sh Please enter GDP and population separately, separated by spaces: 11235 789 City GDP: 1.1235 billion, population: 7.89 million, this is a competitive city. City GDP: 1.1235 billion, population: 7.89 million, this is a competitive city. City GDP: 1.1235 billion, population: 7.89 million, this is a competitive city. City GDP: 1.1235 billion, population: 7.89 million, this is a competitive city. [root@hadoop211 tutorial]# bash b2023052303.sh Please enter GDP and population separately, separated by spaces: 21235 1689 City GDP: 2123.5 billion, population: 16.89 million. City GDP: 2123.5 billion, population: 16.89 million. City GDP: 2123.5 billion, population: 16.89 million. City GDP: 2123.5 billion, population: 16.89 million.
6. Use if then elif then else fi statement
6.1 Use (()), test, [] judgment statement
6.2 script
Script name: b2023052304.sh
Script content:
#!/bin/bash echo "Please enter a string date (format: yyyymmdd):" read day_str # date command to get the week index number, 0 is Sunday, 1-6 is Monday to Saturday week_index=`date -d ${day_str} + %w` if (( ${week_index} == 0 )) ; then echo "Date: ${day_str}, it is Sunday." elif test ${week_index} == 1 ;then echo "Date: ${day_str}, it is Monday." elif [ ${week_index} == 2 ] ;then echo "Date: ${day_str}, it is Tuesday." elif [ ${week_index} == 3 ] ;then echo "Date: ${day_str}, it is Wednesday." elif [ ${week_index} == 4 ] ;then echo "Date: ${day_str}, it is Thursday." elif [ ${week_index} == 5 ] ;then echo "Date: ${day_str}, it is Friday." elif [ ${week_index} == 6 ] ;then echo "Date: ${day_str}, it is Saturday." else echo "The input date is incorrect." fi
6.3 Execution and output
Execute the command: bash b2023052304.sh
Results of the:
[root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230521 Date: 20230521, is Sunday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230522 Date: 20230522, is Monday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230523 Date: 20230523, it is Tuesday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230524 Date: 20230524, it is Wednesday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230525 Date: 20230525, it is Thursday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230526 Date: 20230526, it is Friday. [root@hadoop211 tutorial]# bash b2023052304.sh Please enter a string date (format: yyyymmdd): 20230527 Date: 20230527, it is Saturday.
Above, thanks.
May 23, 2023
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Cloud native entry skill treeHomepageOverview 12912 people are studying systematically