Shell script Programming (i)

Source: Internet
Author: User
Tags arithmetic

One, bash variables:

Variable naming laws:

1. The reserved words in the program cannot be made: for example if, for;

2, only use numbers, letters and underscores, and cannot start with a number

3. See the meaning of the name

4, the Uniform naming rule: Hump name law

Bash Variable type:

Local variables

Local variables

Environment variables

Positional variables

Special variables (built-in)

Different types of variables:

First, local variables:

Varname=value: Scopes are available for the entire bash process;

Second, local variables:

Local Varname=value: Scope is the current code snippet;

Third, environment variables:

The scope is the current shell process and its child processes and cannot affect its parent process;

Export Varname=value, if the variable is already defined, it can only be exported varname with the variable name, i.e.

1. Export Varname=value

2. Varname=value

Export VarName

The script launches a child shell environment variable when the command is executed:

Scripts that are automatically executed by the system (not command-line startup) require a self-defined environment variable;

Four, Position variable:

$, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.

Cat Test.txt Hello




Shift: kicks off parameters

Shift N: kicks off n parameters and kicks off a parameter by default

V. Special variables:

$?: The return value of the previous command execution state:

There are two possible types of return values for program execution:

1. Program execution Results

2. Does the program state return (0-255)

0 to perform the correct

1-255 Execution Error (1,2,127 system reservation);

$#: Gets the total number of parameters in the current shell command line

$*: Gets all the parameters of the current Shell "$ ..., under IFS control

[Email protected]: All parameters of this program "$1″" $2″ "$3″" ... ", not controlled by IFS

$ A Gets the file name of the currently executing shell script

$n gets the nth parameter value of the currently executing shell script, n=1..9

$$ gets the current shell's process number (PID)

$! PID that executes the previous instruction

$* and [email protected] Test Examples of the difference:

First, let's look at an example of the same result after executing the script:

1. Create a script content

650) this.width=650; "src=" Http:// "title=" 111. PNG "alt=" Wkiom1evfzhqr8xnaaapyltago4368.png "/>

2. Execute the script:

650) this.width=650; "src=" Http:// "title=" 112. PNG "alt=" Wkiol1evf0adcjriaaan37vkjdy962.png "/>

It can be seen that the two results are the same.

Let's look at a different example of the execution result:

Create two scripts, content

650) this.width=650; "src=" Http:// "title=" 113. PNG "alt=" Wkiom1evf-hxpc3naaan7zxhcbq868.png "/>

650) this.width=650; "src=" Http:// "title=" 114. PNG "alt=" Wkiol1evf_sq_2olaaanosi2spw322.png "/>


650) this.width=650; "src=" Http:// "title=" 115. PNG "alt=" Wkiom1evgcwgq139aaaxrr6al2c990.png "/>

After execution, it can be concluded that:

$*: All parameters passed to the script, all parameters are combined into a string

[Email protected]: All parameters passed to the script, each parameter is a separate string


[Email protected] $* only when it's wrapped in double quotes.

Second, arithmetic operation:

Arithmetic operations in BASH: Help-let

+,-, *,/,% modulus (remainder), * * (exponentiation)

To implement arithmetic operations:

(1) Let var= arithmetic expression

(2) var=$[arithmetic expression]

(3) var=$ (arithmetic expression)

(4) var=$ (expr arg1 arg2 arg3 ...)

(5) Declare–ivar= value

(6) echo ' Arithmetic expression ' | Bc

Multiplication symbols need to be escaped in some scenarios, such as *

Three, condition test:

To determine whether a demand is satisfied, it needs to be realized by testing mechanism;

A dedicated test expression needs to be assisted by a test command to complete the test process;

Evaluate Boolean declarations for use in conditional execution

If true, then return 0

If False, 1 is returned

Test command:




Note: You must have a white space character before and after expression

Numerical test:

-GT: Is it greater than

-ge: is greater than or equal to

-eq: Is it equal to

-ne: is not equal to

-LT: Is it less than

-le: is less than or equal to

File test:

Presence Testing

-a FILE: Same-E

-E File: The existence of the test, the existence of the true, otherwise false;

Presence and category Testing

-B File: Whether it exists and is a block of device files;

-C File: Existence and character device files;

-D file: exist and are directory files;

-F file: exists and is a normal file;

-H file or-L file: exists and is a symbolic link;

-P file: exists and is a named pipe file;

-S file: exists and is a socket file;

Use the Read command to accept the input:

Use read to assign the input value to one or more shell variables:

-p Specifies the prompt to display


Read reads values from standard input, assigns a variable to each word

All remaining words are assigned to the last variable

Read-p "Enter a filename:" FILE


1, write script/root/bin/, accept a file path as a parameter, if the number of parameters is less than 1, the user is prompted "at least one parameter should be given" and immediately exit, if the number of parameters is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed


[$#-lt 1] && echo "At least give a parameter." | | Grep-c ' ^[[:space:]]*$ ' $

2, write script/bin/, judge the current user to the specified parameter file, whether unreadable and not writable


! [-r/tmp/file1-o-w/tmp/file1] && echo "' WhoAmI ' can not R and W" | | echo "' WhoAmI ' can R or W"

#root不够权限控制, use a normal user test.

This article from "Progress a little every day" blog, declined reprint!

Shell script Programming (i)

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: 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.