Learn Linux:shell Script (ii) about scripts

Source: Internet
Author: User

( a ) first look at how the Shell script executes, theShell script suffix is the sh file.

1 sh test.sh

2 Source test.sh

What is the difference between these two ways? The script in test.sh is simple , assigning a name variable after typing it from the keyboard

Read-p "Please input your name:" Name

Do the following

[Email protected]:/home/zhf/zhf/shell_prj# sh test1.sh

Please input your NAME:ZHF

[Email protected]:/home/zhf/zhf/shell_prj# Echo $name


Echo$name is a null value. The reasons are described earlier. When Bash executes a script, it is another child process. When the child process is complete, the variables within the child process are not passed into the parent process.


But executing the script with source is different. The reason for this is that source executes the script in the parent process.

[Email Protected]:/home/zhf/zhf/shell_prj#source test1.sh

Pleaseinput your NAME:ZHF

[Email Protected]:/home/zhf/zhf/shell_prj#echo $name

Zhf



In the script, it is not enough to make various conditional judgments. Conditions are judged by test and [] two ways

Test Mode:

Read-p "Please input your name:" Name

Test-z $name && echo "You must input your name"

- Z is to determine if the string is 0 and an empty string, if it is, prompt for a name

[Email Protected]:/home/zhf/zhf/shell_prj#sh test1.sh

Pleaseinput Your Name:

Youmust Input your Name


Test has a lot of judging parameters, which can be viewed with mantest .


[] way:

Read-p "Please input your name:" Name

[-Z $name] && echo "Please input your name"

Use [] There are a few points to note:

1 The space bar is required for each component within the brackets to separate

2 The variables inside the brackets need to be enclosed in double quotes

With respect to the 2nd, if you do not enclose it in double quotes, the following problems arise:

[Email protected]:/home/zhf/zhf/shell_prj#name= "CQ ZHF"

[email protected]:/home/zhf/zhf/shell_prj#[$name = = "CQ ZHF"]

bash:[: Too many arguments


The reason is $name =cqzhf, If there is no double brackets, then the above inequality equals CQZHF = = "CQ zhf". The system is considered to be 2 variables, so it prompts toomany arguments.

( two ) the variable of the script.

Some parameters are passed in when the script is executed. So how do you monitor these parameters in the script?

$# The number of parameters to represent

[email protected] get all the parameters

$1,$2 ..... Represents the individual parameters. where "$" refers to the name of the script.

[Email Protected]:/home/zhf/zhf/shell_prj#sh test1.sh One and three

THESCRITP name is test1.sh

Thetotal number of parameter is 3

Thefirst parameter is one


If the input variable too many, single from the Order up memory is also more troublesome, there is a method of parameter offset can be easier. shift Command. The function of this command is like a FIFO stack.

[Email Protected]:/home/zhf/zhf/shell_prj#sh test1.sh One and three

THESCRITP name is test1.sh

Thetotal number of parameter is 3

Thefirst parameter is one

Thefirst parameter is


Other process Control statements, such as If,when, For...do...done, and so on, are similar to other language usages.

Learn Linux:shell Script (ii) about scripts

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.