Shell Learning notes-branching statements

Source: Internet
Author: User

One, single branch if statement 1. Syntax format
if  Then     program fi or if  [conditional judgment]    then     program fi

Note: There must be a space between the brackets and the conditional judgments

2. Example 1: Determine if the logged on user is root
#!/bin/Bashif["$USER"= = Root]; Then    Echo "Login User is root"fi######## #或者 ##########!/bin/Bashif["$USER"==Root] Then    Echo "Login User is root"fi

3. Example 2: Determine partition usage
#!/bin/bashtest=$ (dfgrepawk'{print $}'cut  '%'1) ;  then    Echo " warning!/dev/sda1 is full! " fi

Two, two-branch if statement 1. Syntax format
if  Then When the     condition is established    , the execution of the program if the else     condition is not established, the execution of the program fi or if  [ Conditional judgment]    then     When the condition is established, the program executed when the else     condition is not established  fi

2. Example 1: Enter a file to determine if there is a
#!/bin/"pleaseinput a file:"fileif [-F $file  then    echo"File: $file exists! " Else    Echo " File: $file not exists! " fi

3. Example 2: Determine if the Apache service is started, and if it is not, start the code
#!/bin/bashtest=$(PSAux |grephttpd |grep-V'grep'|WC-l)if[$test-GT0]; Then    Echo "$ (date) httpd is running!"Else    Echo "$ (date) httpd isn ' t running, 'll be started!"/etc/init.d/httpd Startfi

Three, multi-branch if statement 1. Syntax format
if  Then When     the conditional judgment 1 is established, the execution Program 1elif then     when the conditional Judgment 2 is established, executes the program 2 .... Omit more conditions ..... Else     When all conditions are not true, this program is finally executed fi

2. Example: Implementing a Calculator
#!/bin/bash# Input number A, number B and operator read-P"Please input number A:"Aread-P"Please input number B:"Bread-P"Please input operator[+|-|*|/]:"opt# Judging the correctness of the input content 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!"Exit1elif["$opt"=="+"]; Thenresult=$ (($a +$b))elif["$opt"=="-"]; Thenresult=$ ($a-$b))elif["$opt"=="*"]; Thenresult=$ (($a *$b))Elseresult=$ (($a/$b))fiEcho "a $opt b = $result"

Iv. Case Statements

Case statements and IF...ELIF...ELSE statements are multi-branch conditional statements, but unlike if multi-branch conditional statements, case statements can only judge a conditional relationship, and an if statement may judge a variety of conditional relationships.

1. Syntax format
 Case inch " value 1 "     If the value of the variable equals the value 1, execute program 1    ;; " Value 2 "     If the value of the variable equals the value 2, execute program 2    ;; ..... Omit other branches ..... *)    If the value of the variable is not the above value, execute the program    ;; Esac

2. Example: Judging user input
#!/bin/Bashread-P"Please choose yes/no:"cmd Case$cmdinch "Yes")    Echo "Your Choose is yes!"    ;;"No")    Echo "Your Choose is no!"    ;;*)    Echo "Your Choose is error!"    ;;Esac

Shell Learning notes-branching statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.