Process control, function, and script debugging in Shell

Source: Internet
Author: User
Introduce Process Control in Shell, such as judgment statements... If statement: first, learn a few knowledge: [-f "somefile"]: Determine whether a file is [-x "/bin/ls"]: determine whether/bin/ls exists and has the executable permission [-n "$ var"]: determines whether the $ var variable has a value ["$ a" = "$ B"]: judge $ a and $

Introduce Process Control in Shell, such as judgment statements...

If statement:

First, I want to know a few things:

[-F "somefile"]: determines whether it is a file.

[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.

[-N "$ var"]: determines whether the $ var variable has a value.

["$ A" = "$ B"]: determines whether $ a and $ B are equal.

Sample Code:

1234567891011 #! /Bin/shvarOne = 1 varTwo = 2 varThree = 3if ["$ varOne" = "$ varTwo"]; then echo "varTwo: $ varTwo "elif [" $ varOne "=" $ varThree "]; then echo" varThree: $ varThree "else echo" varOne: $ varOne "fi

Note that do not forget the spaces before and after the brackets during [] comparison! = There must be spaces before and after the equal sign;

& | Operator:

Sample Code:

123456789101112131415 #! /Bin/shvarOne = 1 varTwo = 2 varThree = 3if ["$ varOne" = "$ varThree"] | ["$ varOne" = "$ varTwo"]; then echo "| enter" else echo "No | enter" fi if ["$ varOne" = "$ varOne"] & ["$ varOne" = "$ varTwo" ]; then echo "& enter" else echo "No & enter" fi

Case statement:

Note:

The case expression can be used to match a given string, not a number (do not use the switch... Case obfuscation ).

Sample Code:

123456789101112 #! /Bin/sh ftype = 'file "$1" '# Note' and 'is differentcase "$ ftype" in "$1: Zip archive "*) unzip "$1"; "$1: gzip compressed" *) gunzip "$1"; "$1: bzip2 compressed" *) bunzip2 "$1 ";; *) echo "File $1 can not be uncompressed with smartzip"; esac

Special variable $1, which contains the first parameter value passed to the script, that is, $1 is the string articles.zip.

Select statement:

The select expression is an extended application of bash and is good at interactive scenarios. You can select from different values:

1234 Selectvarin...; do break; done ...... now $ var can be used ....

Sample Code:

1234567 #! /Bin/sh echo "What is your favorite OS? "Selectvarin" Linux "" Gnu Hurd "" Free BSD "" Other "; do break; doneecho" You have selected $ var"

If the preceding script appears, select: not found will be #! /Bin/sh #! /Bin/bash:

1234567 What is your favorite OS? 1) Linux2) Gnu Hurd3) Free BSD4) Other #? 1You have selected Linux

While/for Loop:

Sample Code:

1234567891011121314 #! /Bin/shvarOne = 1 varTwo = 1 # whilewhile ["$ varOne" = "$ varOne"]; do echo "while Done" breakdone # forforvarStrinH I m I; do echo "varStr is $ varStr" done

Output:

123456 WhileDonevarStr is HvarStr is IvarStr is MvarStr is Ilocalhost: Desktop Himi $

Select statement:

Note: The select expression is an extended application of bash and is good at interactive scenarios. You can select from different values:

Sample Code:

123456 #! /Bin/shecho "What is your favorite? "Selectvarin" iOS "" Android "" Himi "" Other "; dobreak; doneecho" You have selected $ var"

After the script is executed, wait for the user to enter your selection in the terminal and press enter, as shown below:

12345678 What is your favorite? 1) iOS2) Android3) Himi4) Other #? 3

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.