Shell learning notes-branch statements
I. single branch if Statement 1. syntax format: if [conditional syntax]; then program fi or if [conditional syntax] then program fi Note: there must be a space between the brackets and the conditional syntax. example 1: Determine whether the login user is root #! /Bin/bash if ["$ USER" = root]; then echo "Login User is root" fi ##########! /Bin/bash if ["$ USER" = root] then echo "Login User is root" fi 3. Example 2: Determine partition usage #! /Bin/bash test = $ (df-h | grep sda1 | awk '{print $5}' | cut-d '%'-f 1) if [$ test-ge 90]; then echo "Warning! /Dev/sda1 is full! "Fi 2. Dual-branch if Statement 1. syntax format: if [condition when condition]; when the then condition is set, if the else condition of the executed program is invalid, the program fi or if [condition when condition] then condition is set, when the else condition of the executed program is invalid, the program fi 2 is executed. example 1: Enter a file to check whether it exists #! /Bin/bash read-p "Please input a file:" file if [-f $ file]; then echo "File: $ file exists! "Else echo" File: $ file not exists! "Fi 3. Example 2: Determine whether the apache service is started. If not, start the Code #! /Bin/bash test = $ (ps aux | grep httpd | grep-v 'grep' | wc-l) if [$ test-gt 0]; then echo "$ (date) httpd is running! "Else echo" $ (date) httpd isn' t running, will be started! "/Etc/init. d/httpd startfi 3. multi-branch if Statement 1. syntax format: if [condition 1]; then when condition 1 is set, execute program 1 elif [condition 2]; then when condition 2 is set, execute the program 2 ..... omit more conditions ..... when none of the conditions are met, else finally executes this program fi 2. example: calculator #! /Bin/bash # enter the number a, number B, and operator read-p "Please input number a:" aread-p "Please input number B: "bread-p" Please input operator [+ |-| * |/]: "opt # determining input content correctness testa =$ (echo $ a | sed's/[0-9] // G ') testb = $ (echo $ a | sed's/[0-9] // G ') testopt = $ (echo $ opt | sed's/[+ | \-| * | \/] // G ') if [-n "$ testa"-o-n "$ testb"-o-n "$ testopt"]; then echo "input content is error! "Exit 1 elif [" $ opt "=" + "]; then result = $ ($ a + $ B )) elif ["$ opt" = "-"]; then result = $ ($ a-$ B) elif ["$ opt" = "*"]; then result = $ ($ a * $ B) else result = $ ($ a/$ B )) fi echo "a $ opt B = $ result" 4. case statement and if... elif... else statements are multi-branch condition statements. However, different from the if multi-branch condition statements, case statements can only determine one condition relationship, while if statements can determine multiple condition relationships. 1. syntax format: case $ variable name in "value 1") if the value of the variable is equal to the value 1, execute program 1; "value 2") if the value of the variable is equal to the value 2, execute Program 2 ;;..... omit other branches ..... *) If none of the variable values are the above values, execute this program; esac 2. example: Judge user input #! /Bin/bash read-p "Please choose yes/no:" cmd case $ cmd in "yes") echo "Your choose is yes! ";" No ") echo" Your choose is no! "; *) Echo" Your choose is error! "; Esac