Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4.3.1 if conditional test statement


May 23, 2021 That's what Linux should learn



The if conditional test statement allows the script to automatically execute the appropriate command based on the actual situation. From a technical point of view, if statements are divided into single-branch structure, dual-branch structure, multi-branch structure;

The single branch structure of the if conditional statement consists of if, then, and fi keywords, and executes preset commands only after the condition is established, equivalent to the co-spoken "if ... S o..." A single-branch if statement belongs to one of the simplest conditional judgment structures, and the syntax format is shown in Figures 4-17.

4.3.1 if conditional test statement

Figure 4-17 A single branch of the if statement

Here's a single-branch if condition statement to determine whether a /media/cdrom file exists, and if so, to end the conditional judgment and the entire Shell script, and instead create the directory:

[root@linuxprobe ~]# vim mkcdrom.sh

!/bin/bash

  1. DIR="/media/cdrom"
  2. if [ ! -e $DIR ]
  3. then
  4. mkdir -p $DIR
  5. fi

Since Chapter 5 only explains user identity and permissions, the script continues to be executed in the "bash script name" way. Under normal circumstances, there is no output information after the script file has been successfully executed, but you can use the ls command to verify that the /media/cdrom directory has been successfully created:

[root@linuxprobe ~]# bash mkcdrom.sh [root@linuxprobe ~]# ls -d /media/cdrom /media/cdrom

The dual branch structure of the if condition statement consists of if, then, else, fi keywords, which make a conditional matching judgment, and if it matches the condition, executes the corresponding preset command; S o...... O r...... S o..." The dual-branch structure of the if conditional statement is also a very simple judgment structure, as shown in figures 4-18.

4.3.1 if conditional test statement

Figure 4-18 double-branched if conditional statement

The following double-branched if condition statement is used to verify that a host is online, and then, based on the results of the returned value, either displays the host's online information or displays the host's non-online information. The script here mainly uses the ping command to test network connectivity with the other host, while the ping command in linux system ends with four attempts unlike Windows, so in order to avoid long user wait times, the number of attempts needs to be specified by the -c parameter, and the -i parameter is used to define the send interval for each packet, and the wait timeout is defined using the -W parameter.

[root@linuxprobe ~]# vim chkhost.sh

!/bin/bash

  1. ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
  2. if [ $? -eq 0 ]
  3. then
  4. echo "Host $1 is On-line."
  5. else
  6. echo "Host $1 is Off-line."
  7. fi

We used the $?variable in section 4.2.3 to show the return value of the execution of the last command. I f the previous statement executes successfully, the $?variable displays the number 0 and, conversely, a non-zero number (perhaps 1, or 2, depending on the system version). T herefore, you can use the integer comparison operator to determine whether the $? variable is 0, thus knowing the final judgment of the statement. The server IP address here is 192.168.10.10, so let's verify the effect of the script:

[root@linuxprobe ~]# bash chkhost.sh 192.168.10.10 Host 192.168.10.10 is On-line. [root@linuxprobe ~]# bash chkhost.sh 192.168.10.20 Host 192.168.10.20 is Off-line.

The multi-branch structure of the if conditional statement consists of if, then, else, elif, fi keywords, which make multiple conditional matching judgments, and any one of these multiple judgments executes the appropriate preset command after a successful match, equivalent to the co-spoken "if ... S o...... I f...... S o..." T he multi-branch structure of if conditional statements is one of the most commonly used conditional judgment structures in work, although relatively complex but more flexible, and the syntax format is shown in Figures 4-19. C hapter 4 Vim Editor with Shell Command Script. Chapter 4 Vim Editor with Shell Command Script.

Figure 4-19 multi-branch if conditional statement

Use the multi-branch if conditional statement below to determine which score range the user enters, and then output prompts such as Excelent, Pass, Fail, and so on. I n Linux system, read is a command used to read user input information, which can assign the received user input information to a later specified variable, and the -p parameter is used to display a certain amount of prompt information to the user. In the following script example, the word Excelent is output only if the score entered by the user is greater than or equal to 85 points and less than 100 points, and if the score does not meet the criteria (i.e. the match is unsuccessful), continue to determine whether the score is greater than or equal to 70 points and less than 84 points, and if so, the word Pass is output, and if both fail (i.e. both match operations fail), the word Fail is output:

[root@linuxprobe ~]# vim chkscore.sh

!/bin/bash

  1. read -p "Enter your score(0-100):" GRADE
  2. if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
  3. echo "$GRADE is Excellent"
  4. elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
  5. echo "$GRADE is Pass"
  6. else
  7. echo "$GRADE is Fail"
  8. fi
  9. [root@linuxprobe ~]# bash chkscore.sh
  10. Enter your score0-100):88
  11. 88 is Excellent
  12. [root@linuxprobe ~]# bash chkscore.sh
  13. Enter your score0-100):80
  14. 80 is Pass

The script is executed below. When the user enters scores of 30 and 200, respectively, the results are as follows:

[root@linuxprobe ~]# bash chkscore.sh
Enter your score(0-100):30 30 is Fail [root@linuxprobe ~]# bash chkscore.sh Enter your score(0-100):200 200 is Fail

Why does The Fail still appear when the score you enter is 200? T he reason is simple - the two conditional judgment statements in the script are not successfully matched, so the final bottom-of-the-loop strategy is automatically executed. It can be seen that this script is not perfect, it is recommended that the reader perfect the script, so that the user when entering a score greater than 100 or less, give Error the wrong word prompt.