Shell Programming Basics

Source: Internet
Author: User

1. Execution style of the program

Program Programming Style:
Past: Command-centric, data-serving instruction.
Object type: Data-centric, instruction serves data.
Program programming has the following characteristics:
1) Sequential execution: refers to the program in the process of execution, the command will be executed from top to bottom, the above instruction is unsuccessful, will also execute the following program.
2) Select Execute: Refers to in the past programming, will have the following judgment statement, only then satisfies the condition, will execute the judgment statement the procedure.
3) Loop execution: refers to the circular statement, there will be a cyclic price adjustment, when the conditions are met, the program will always be in the loop loop execution.

2. Programming language classification:

3. The basic format of the past-programming Linux:

1) The first line must have #!/bin/bash, #!/bin/python and other interpreters, in the system to execute the program the first time the meeting to see the first line, if not, such as is currently under bash, will directly use Bash to execute the program, if not in bash format written program, Error will occur
2) version Information
3) Date
4) Create a person
5) Email and other information,
Except for the first one, the other information, dispensable

4. Variables

Command rules for variables:
1) cannot use reserved words in the program
2) Use only numbers, letters and underscores, numbers cannot begin
3) See the meaning of the name
4) Uniform naming rules: Hump naming Method (capitalize the first letter of the word)
The effective range of several variables:
Local variable: The active scope is the current shell program and will not take effect on other shells
Environment variable: The effective scope is the current shell program, and its child shell will also take effect
Local variables: Valid for a specific piece of code, typically in a function
Position variable: $1,$2.1 represents the first parameter passed in, and so on
Special variables: $?: Represents the result of returning the execution of the previous command, or True if 0, or False if 0. $*: Represents all parameters passed to the script, all of which are combined into a single string. [Email protected]: pass to all parameters, each of which is a separate string. $#: The number of arguments passed to the script

5. Logical operation

With: true and True = True, true and False = False, false and True = False, false and false = False All is true, only for true
Or: true and True = True, true and False = True, false and True = True, true and False = False all False, only for false
Non -:! Take the inverse value, if true, the result is false, if it is false, the result is true
Short Circuit with:
The first one is false, the result must be false, the second program is not executed
The first one is true, the second one needs arithmetic, if it is true, if it is false, the result is false.
Short Circuit or:
The first one is true, then the result must be true, not executing the second program
The first is false, you need to operate the second program, if true, the result is true, if false, the result is false
XOR: Two values of XOR, same as false, different for true

6. Condition Testing

Determine if the condition is satisfied: If true, returns 0. If False, returns 1
Test command:
Test condition

[[email protected] ~]# Test 10-eq 9 && Eecho "true" | | echo "false"

Determine if 10 equals 9
[Condition] where the "[" number is an internal command, must be paired with "]", the intermediate and judging conditions

[[Email protected] ~]# [10-eq 9]&& Eecho "true" | | echo "false"

[[condition]] condition can be regular expression

[[Email protected] ~]# [["Root" =~ R.*t]] && echo "Zhen" | | echo "Jia"

Numeric comparison
-gt: is greater than
-ge: is greater than or equal to
-eq: Equals
-ne: is not equal to
-lt: is less than
-le: is less than or equal to
string comparison
= = =
' > ': Whether the ASCII code is greater than, the single quotation mark here is translated to >
<: is less than
! =: is not equal to
=~: The left string matches to the right, and the general right side is extended regular expression
- Z: is empty, empty is true, not empty is false
-N: Not empty, empty is false, NULL is true
file existence test
-e: File existence test, existence is true, otherwise false
-A: Same as e
File existence and category
-B: exists and is a block device file
-C: exists and is a character device file
-D: Exists and is a directory file
-F: exists and is a normal file
-H or-L: exists and is a symbol connection file
-P: exists and is a pipe file
-S: exists and is a socket file
file permission Test
-r: Exists and is readable
-W: exists and writable
-X: Exists and can be executed
file special permission test
-U: Exists and has suid
-G: Exists and has gid
-K: exists and has kticky
file size test
-S: Exists and is empty
Whether the file is open
-T: is open and related to a terminal
-N: Whether the file has been modified since the last time it was read
-o: Is the file owner of the current active user
-G: Whether the current active user is a group of files
Binocular Test (whether two files are one)
File1-ef file2 See if two files are an inode number
File1-nt file2 file1 mtime New and File2 is true, otherwise false
File1-ot File2 File1 's mtime old and file2 are true, otherwise false

7. The first Hello World shell script

Shell Programming Basics

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.