Application of If flow control statement in shell script

Source: Internet
Author: User

On the use of test statements and related use rules, if only isolated use is difficult to have its application, of course, in the shell script can not exist in isolation, only with conditional statements and control statements and other uses to make its function to maximize the embodiment, so in daily use, its function should not be underestimated.

First, the control process

If control flow can be divided into branch if statement and multi-branch if statement, its use language and C language have a lot of similarities, the specific usage is described below.

1.1 But the branch if statement structure:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/86/31/wKioL1e3NOSTxnwPAABwSTN90Dc382.png-wh_500x0-wm_3 -wmp_4-s_1402037824.png "title=" If.png "alt=" Wkiol1e3nostxnwpaabwstn90dc382.png-wh_50 "/>

1.2 Basic syntax

If condition or if condition; then//semicolon indicates that two commands are written on one line and do not affect each other, and the order is executed sequentially from left to right. Then Statement statement fi Fi

Example One.

#!/bin/bashif [$USER = = root];thenecho "Error" Exit 127//exit here for error output prompt fi

1.3 Dual-branch if structure

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/86/31/wKiom1e3Nwvycaf0AABzYtbp2zY512.png-wh_500x0-wm_3 -wmp_4-s_3031216061.png "title=" If.png "alt=" Wkiom1e3nwvycaf0aabzytbp2zy512.png-wh_50 "/>

1.4 Dual-Branch if statement syntax structure

If condition 1; then command 1 Else Command 2 fi

Example Two.

#!/bin/bashping -c 3 $1 &> /dev/nullif  [ $? -eq 0 ];then        echo  the  host $1 is up "                   //the location parameter in the row, the script runs the instance else         echo  "The host $1 is down" fi[[email protected] ~]# sh  If1.sh 192.168.213.101the host 192.168.213.101 is up 
#!/bin/bashservice $ status &>/dev/nullif [$? = = 0];then echo "The $ is running" else EC Ho "The IS Stop" Fi[[email protected] ~]# sh if1.sh networkthe Network is running[[email protected] ~]# sh if1.sh networ  Kmanagerthe NetworkManager is running[[email protected] ~]# service NetworkManager stopstopping NetworkManager daemon: [ OK][[email protected] ~]# sh if1.sh networkmanagerthe NetworkManager is stop[[email protected] ~]#

Two, multi-branch Process Control

2.1 Process Control schematic diagram

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/86/31/wKioL1e3OknAVA7QAACu8uIIpUM708.png-wh_500x0-wm_3 -wmp_4-s_3964960725.png "title=" If.png "alt=" Wkiol1e3oknava7qaacu8uiipum708.png-wh_50 "/>

2.2 Grammatical structure

If condition 1; then command 1elif condition 2; then command 2:elif condition 3; then command 3........................else command NFI

Example Three.

#!/bin/bashch=chinaj=japanread-p "Please enter your country:" Coun//read implementation of human-computer interaction directives are typically used with-p-t and-n with if [$coun = = $ch];then echo "You are Chinese" elif [$coun = = $J]; then echo "You are Japanese" else echo "You are others" fi[[email protected] ~] #./if1.sh Please enter your country: U.S. You are American [[email protected] ~]#./if1.sh Please enter your country: China you are Chinese [email protected] ~]#
[Email protected] ~]# VI if.sh
#!/bin/bashread-p "Please enter the file to be tested:" File//operator prompts for input when running the script if [-D $file]; Then echo "The file is a directory file" Elif [-f $file]; Then echo "The file is a normal file" Elif [-R $file]; Then echo "The file is a writable file" else echo "the file permission is unknown" Fi[[email protected] ~]#./if.sh Please enter the file you want to test: passwd The file is a normal file

The above lists about if a few commonly used flow control statements, try it!!!

This article is from the "11705634" blog, please be sure to keep this source http://11715634.blog.51cto.com/11705634/1840661

Application of If flow control statement in shell script

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.