Shell script Programming

Source: Internet
Author: User
Tags arithmetic

BASH: Weakly typed language

Variable type:

Environment variables, local variables (local variables), positional variables, special variables


1. Local variables: Only valid in the current process, parent-child shell is not universal

Declarative method: Name = value

Reference method: $ (Name) parentheses can be omitted

Local variables: Scope is the current code snippet

Declaration method: Local Name = value

2. Environment variable: scope is the current shell process and its child processes

Declarative method: Name= Value

Reference method: Export Name (the user-defined variable, is a local variable, export can change the local variable into an environment variable, so that the child process can reference)

3. Special variables:

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

Note: After the execution of the program there are two kinds of return values, one is the result of program execution, and the other is the return code of the program State (0-255).

0: Correct execution

1-255: Error execution

1,2,127: System Reservation

Undo Variable: Unset Name

View environment variables: env, PRINTENV, export


Script:

Command stack, according to the actual needs, combined with the command flow mechanism, the realization of the source program. The Linux kernel can only execute Elf-formatted files, but the script we write is a stack of commands, the script format is binary ASCII so in the first line of the script file, we need to tell the kernel that we need to invoke a script interpreter in order to execute the script properly.

Magic number: (shebang) #!/bin/bash


Three kinds of tests common in scripts:

1. Condition Testing

2. Character test

3. File Testing

1. Conditional test: According to the return value of the command to determine whether the execution of the command is successful, and then combine the logical symbol to complete the condition judgment

1) The expression of the condition test:

[Expression]: [] must have a widening number between the expression

[[Expression]]: [[]] must have a widening number between the expression

Test expression


2. Integer test:

-eq:equal

-ne:not equal

-gt:great Then

-lt:less Then

-ge:great equal

-le:less equal


3. Logical Relationship:

With: &&

Or: | |

Non -:!


4) If statement of a single branch

if judgment condition; Then

Statement1

Statement2

...

Fi


5) Multi-branch

if judgment condition; Then

Statement1

Statement2

...

ElseIf statement

Fi


6) Arithmetic operations

Ⅰlet Arithmetic Expression Example: let c= $a + $b

ⅱ$[Arithmetic Expression] Example: c= $[$a + $b]

ⅲ$ (arithmetic expression) Example: C= $ (($a + $b))

ⅳexpr arithmetic expressions Note that there are spaces between the operands and operators in the expression, and that you want to use the command to reference the back quotation marks

Example: c = ' expr $a + $b '


3. File Testing

If the-e file:exist file is present [-e] Note: [] also requires a space

-F file:file is normal file

-D file:directory is a catalog file

-R File:read Test whether the current user has read access to the specified file

-W File:write

-X File: Execute

Define script exit Status code: Exit if no exit status code is defined, the exit code for the last command is the exit status code for the script

Test the script for syntax errors:

Bash-n file

Bash-x File Single Step execution



Positional variables: used when the script needs to accept external parameters, $ $ $ for the first argument is the second argument ...

When you execute the script, the variables are followed by the script, separated by a space.


Special variables:

$? :

$#: Parameter list

[email protected]: parameter list

Shift: Rotation, when there are too many arguments, we can use shift to kick out one or more parameters, Shift | Shift 2


Shell script Programming

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.