Bash programming syntax structure section 2

Source: Internet
Author: User

Bash programming syntax structure

I. For Structure

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 216px; "Title =" wps_clip_image-28091 "border =" 0 "alt =" wps_clip_image-28091 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046951LXys.png "Height =" 216 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

$ VaR is the variable for loop control, and [LIST] is a set to be traversed by $ var,

The do/done pair contains the loop body, which is equivalent to a pair of braces in C. If

Do and for are written in the same line. You must add ";" before do ";". For example:

For $ VaR in [LIST]; do. The following is an example of loop using:

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 182px; "Title =" wps_clip_image-1567 "border =" 0 "alt =" wps_clip_image-1567 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046954H4nt.png "Height =" 182 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 182px; "Title =" wps_clip_image-17452 "border =" 0 "alt =" wps_clip_image-17452 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046958u3PQ.png "Height =" 182 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

2. While is applicable to situations where the number of loops is unknown or a large list is directly generated by;

While test condition; do

Loop body

Done

If the test condition is true, it enters the loop. If the test condition is false, the loop is exited;

The test conditions are generally described by variables. You need to change the value of the variable in the circular body's invariant,

To ensure that the test conditions at a certain time point are false, and then the cycle ends;

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 174px; "Title =" wps_clip_image-2246 "border =" 0 "alt =" wps_clip_image-2246 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046962Ld5u.png "Height =" 174 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

Iii. Until format:

Until test conditions; do

Loop body

Done

The test condition is false and enters the loop. If the test condition is true, the loop is exited;

The test conditions are generally described by variables. You need to change the value of the variable in the circular body's invariant,

To ensure that the test conditions are true at a certain time point, and then end the cycle;

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 174px; "Title =" wps_clip_image-16742 "border =" 0 "alt =" wps_clip_image-16742 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046965a9Yn.png "Height =" 174 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

IV. If Structure

Single Branch Structure dual branch structure and multi branch structure

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 300px; "Title =" wps_clip_image-15407 "border =" 0 "alt =" wps_clip_image-15407 "src =" http://img1.51cto.com/attachment/201408/3/8400375_14070469678C8p.png "Height =" 300 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 300px; "Title =" wps_clip_image-30218 "border =" 0 "alt =" wps_clip_image-30218 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046970oce8.png "Height =" 300 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

Exercise: specify a file path

1. Check whether the file exists. If the file does not exist, the file is not saved and the script is directly ended;

2. If the file is a normal file, it is displayed as "regular file ";

If the file is a directory, it is displayed as "directory ";

If the file is a linked file, it is displayed as "symbolic file ";

Otherwise, "unknown type." is displayed ."

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 177px; "Title =" wps_clip_image-16709 "border =" 0 "alt =" wps_clip_image-16709 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046976yX7o.png "Height =" 177 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

5. Select case conditions

Case string inexp-1) Several command lines 1; exp-2) Several command lines 2 ;;...... *)

Other command lines

Esac

By calculating the string value, shell returns the result in sequence with the expression exp-1,

Exp-2 and so on until a matching expression is found, if

If a match exists, execute the following command until a semicolon (;) is encountered.

You can also use shell wildcards ("*", "?" ,

"[]"). Generally, "*" is used as the final expression of the Case command

If no matching item is found, run the "other command line" command.

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 256px; "Title =" wps_clip_image-20195 "border =" 0 "alt =" wps_clip_image-20195 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046985INIy.png "Height =" 256 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

Example: Service script example

#!/bin/bash# chkconfig: 2345 67 34srvName=$(basename $0)lockFile=/var/lock/subsys/$srvNamestart() {    if [ -f $lockFile ];thenecho "$srvName is already running."return 1    elsetouch $lockFile[ $? -eq 0 ] && echo "Starting $srvName OK."return 0     fi}stop() {    if [ -f $lockFile ];thenrm -f $lockFile &> /dev/null[ $? -eq 0 ] && echo "Stop $srvName OK" && return 0    elseecho "$srvName is not started."return 1    fi}status() {    if [ -f $lockFile ]; thenecho "$srvName is running."    elseecho "$srvName is stopped."    fi    return 0}usage() {     echo "Usage: $srvName {start|stop|restart|status}"     return 0}case $1 instart)start;;stop)stop ;;restart)stopstart ;;status)status ;;*)usageexit 1 ;;

Esac

6. Loop condition control contine and break

Continue: advance to the next cycle

It is used in conditional statements and only enters in advance in some special scenarios;

Break [N]: jump out of the current loop

Used in condition statements to exit the current loop

650) This. width = 650; "style =" background-image: none; Border: 0px; padding-left: 0px; padding-Right: 0px; padding-top: 0px; width: 400px; height: 197px; "Title =" wps_clip_image-2824 "border =" 0 "alt =" wps_clip_image-2824 "src =" http://img1.51cto.com/attachment/201408/3/8400375_1407046990KHH3.png "Height =" 197 "width =" 400 "hspace =" 0 "vspace =" 0 "/>

VII. Functions

Function f_name {function body} f_name () {function body} can be called: The function is automatically replaced with a function when a number of function names are used. Script: function return value: return Value of the function execution result: Print statement in the code output function: Echo. Return Value of the execution status returned after the system command called in the print function is executed: result of the Last Command executed in the function body: return #

Script: write a script to test whether all hosts in the specified network are online by Ping?

#!/bin/bash#quitScript() {    echo "Quit..."}   trap ‘quitScript; exit 5‘ SIGINTcnetPing() {    for i in {1..254}; doif ping -c 1 -W 1 $1.$i &> /dev/null; then     echo "$1.$i is up."else     echo "$1.$i is down."fi     done}bnetPing() {    for j in {0..255}; docnetPing $1.$j    done}anetPing() {    for m in {0..255}; dobnetPing $1.$m    done}netType=`echo $1 | cut -d"." -f1`if [ $netType -ge 1 -a $netType -le 126 ]; then    anetPing $netTypeelif [ $netType -ge 128 -a $netType -le 191 ]; then    bnetPing $(echo $1 | cut -d‘.‘ -f1,2)elif [ $netType -ge 192 -a $netType -le 223 ]; then    cnetPing $(echo $1 | cut -d‘.‘ -f1-3)else    echo "Wrong"    exit 2fi


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.