Shell script entry notes

Source: Internet
Author: User
1. condition test

Two formats: test condition or [condition] (Note that spaces are added on both sides)
Condition can be a logical operation of multiple sub-conditions. The logical operator numbers include:

-A logic and

-O logic or

! Logic No

&, |


2. File Test

File testing uses a conditional test to test the status of a file or directory, such as whether the file exists or is readable. The specific test parameters are as follows:

-D directory

-F normal file (regular file)

-L Symbolic Link

-R readable (readable files and directories)

-Block B dedicated files

-E file exists

-G: If the set-group-ID bit of the file is set, the result is true.

-S file length is greater than 0, not empty

-Z file length = 0

-W writable (Writable Files And Directories)

-The U file has SUID settings

-X executable (File executable and directory browsed)

-C character dedicated File

-L Symbolic Link

For example, to test whether the file file.txt exists, you can test it as follows:

$ Test-e file.txt or [-e file.txt]


3. String Testing

The string test can test whether the string is null and whether the two strings are in phase. The specific test parameters are as follows:

= The two strings are equal

! = Two strings

-Z blank string [zero]

-N non-empty string [nozero]


4. Numerical Test

The value test is used to test the size and phase of two parameters. The format is "Number" numberic_operator "Number" or ["Number" numberic_operator "Number"]. The value of numberic_operator is as follows:

-Equal (equal)

-NE (not equal)

-Gt a> B (greater)

-Lt a <B (less)

-Le A <= B (less, equal)

-Ge a> = B (greater, equal)

(Question: It seems that a variable cannot be compared with a numerical constant. You need to assign the value to the variable and then compare the two variables for further research !)


5. If Structure

If condition 1
Then
Command 1
Elif condition 2
Then
Command 2
Else
Command 3
Fi


6. Case Structure

Case variable in
Pattern [| pattern]...) # Use "|" to separate matching Modes
Command ....
;;
Mode 2)
Command .....
;;
*) # Match all
.......
;;
Esac


7. For Structure

For variable name in list
Do
Command 1
Command 2
Done

Example:

Used for numerical sequence:
For Loop in 1 2 3 4 5
For I in {1 .. 10}
For (I = 1; I <= 10; I ++ ))

Used for string sequence:
For Loop in "orange red blue gray"

Command Execution result:
For Loop in 'LS'

Infinite Loop:
For files in *


8. Until Structure

Until Condition
Do
Command .....
Done


9. While Structure

While Condition
Do
Command 1
Command 2
Done


10. Number of bash groups (array)

Generally, variables like a = "a B C Def" only replace $ A with a single string,
But to a = (a B C DEF), it is to define $ A as the number of groups...
The following method can be used to replace the number of bash groups:
$ {A [@]} or $ {A [*]} can obtain a B c def (number of all groups)
$ {A [0]} returns a (number of the first group), and $ {A [1]} returns the number of the second group...
You can get 4 for $ {# A [@]} or $ {# A [*]} (number of all groups)
$ {# A [0]} can get 1 (that is, the length of the first number of groups (), $ {# A [3]} can get 3 (length of the fourth number (DEF)
A [3] = xyz is to redefine the fourth number of groups as XYZ...


11. Three important symbols

Hard quote: ''(single quotes). All the Meta in hard quotes are disabled.
Soft quote: "" (double quotation marks). In Soft quoe, most of the Meta will be disabled, but some will be retained (for example, $ ).
Escape: \ (backslash). A single Meta is disabled only after escape (Escape Character.

For example, the echo '$0 outside function is' $0, the first $0 won't be unlocked, and the character "$0" is output directly, and the second $0 will be unlocked, output the path of the current shell script;

If you run echo "$0 outside function is" $0, both $0 will be unlocked, so the expected result cannot be reached.

(Note: When writing a shell script in Vim, the color prompt of VIM can be used to check whether the symbol is unlocked, that is, it is processed by regular strings or meta)


[Other key rules]

  • (): Place the commands in it in the nestedsubshell for execution, or use it for calculation or command replacement. (Variable modification is temporary)
  • {}: Place the commands in the non-namedfunction for execution, or use them in the defined range of variable replacement. (Variable modification is valid for this shell)
  • $ {} Or '': used to replace the command, that is, execute the command line in the brackets, and then return the command line execution result.
  • $ (): Used for integer calculation. For example:
    $ A = 5; B = 7; C = 2
    $ Echo $ (a + B * C ))
  • (): You can redefine variable values or perform conditional testing. For example:
    A = 5; (A ++) You can redefine $ A as 6
    A = 5; B = 7; (a <B) returns 0 (true.
  • There are only two return values for commands, functions, or shell scripts: 0 is true (true), and non-0 is false (false)
  • $ #: Number of parameters added to Shell
  • $: Shell PID (processid)
  • $! : PID of the background process last run by Shell
  • $? : End code of the last running command (return value)
  • $ *: List of all parameters. For example, when "$ *" is included in... $ N "to output all parameters.
  • $ @: List of all parameters. For example, when "$ @" is included in "... All parameters are output in the form of "$ N.








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.