Process control, function, and script debugging in Shell

Source: Internet
Author: User
Tags echo command

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"]; thenecho "varTwo: $ varTwo "elif [" $ varOne "=" $ varThree "]; thenecho" varThree: $ varThree "elseecho" 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"]; thenecho "| enter" elseecho "No | enter" fiif ["$ varOne" = "$ varOne"] & ["$ varOne" = "$ varTwo"]; thenecho "& enter" elseecho "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/shftype = '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:

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

Sample Code:

1234567 #! /Bin/shecho "What is your favorite OS? "Selectvarin" Linux "" Gnu Hurd "" Free BSD "" Other "; dobreak; 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"]; doecho "while Done" breakdone # forforvarStrinH I m I; doecho "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 #? 3You have selected Himilocalhost: Desktop Himi $

Function:

If you have written complicated scripts, you may find that the same code may be used in several places. In this case, it is much easier to use functions. The functions are roughly as follows:

123456 functionname () {# inside the body $1 is the first argument given to the function #$2 the second... body}

Sample Code:

1234567 #! /Bin/shhimi () {echo "Function is OK" exit0} himi

Script debugging:

The simplest debugging method is to use the echo command. You can use echo to print variable values wherever possible, which is why most shell Programmers spend 80% of their time debugging. The benefit of Shell scripts is that it does not take much time to insert an echo command without recompilation. Shell also has a real debugging mode. If the script "strangescript" has an error, run the following command to debug it:

Sh-x strangescript

The preceding command executes the script and displays the values of all variables. Shell also has a mode to check the syntax without executing the script. The command is as follows:

Sh-n your_script

OK. This chapter mainly aims to familiarize yourself with some control statements and common knowledge points, so the basic knowledge of Shell programming will come to an end. If necessary, you will continue to study in depth;

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.