Shell programming Basics

Source: Internet
Author: User
Tags character classes echo command

1. Introduction to file types
Linux mainly includes the following seven file types: d directory-common file l symbol link s socket file B device file c character device file p name pipe File

2. Regular Expression
Regular Expressions are very important in shell programming.

When extracting or filtering text from a file or command output. You can use a regular expression (RE), which is a set of special or not special string patterns.

Basic metacharacters:
^ Matches only the beginning of the row.
$ Matches only the end of the row.
* A single character followed by * matches 0 or more.
[] Matches the characters in []. It can be a single character or a character sequence. You can use-to represent the range in []. For example, [1-5] is equivalent to [1, 2, 4, 5].
Block the special meaning of a metacharacter. For example, $ indicates the character $, but not the end of the matched line.
. Match any single character.
Occurrence times of pattern {n} matching pattern n
Pattern {n,} m matches the occurrence times of pattern, but indicates that the minimum number of occurrences is n.
Pattern {n, m} matches pattern between n and m (n, m is 0-255)

Common examples:
Display executable files: ls-l | grep... X.. x.. x
Show only folders: ls-l | grep ^ d
Match All empty rows: ^ $
Match All words: [A-Z a-z] *
Match any non-letter character: [^ A-Z a-z]
The row contains eight characters: ^ ........ $(8 .)

For more information, see more information about regular expressions.

3. character class description
List of common available character classes:
[: Alnum:] letter number [a-z A-Z 0-9]
[: Alpha:] [a-z A-Z]
[: Blank:] space or tabulation key
[: Cntrl:] Any control character
[: Digit:] Number [0-9]
[: Graph:] Any visual character (no space)
[: Lower:] lower case [a-z]
[: Print:] uncontrolled characters
[: Punct:] punctuation
[: Space:] space
[: Upper:] capital [A-Z]
[: Xdigit:] hexadecimal number [0-9 a-f A-F]

The use of character classes can better adapt to non-English locale (including some required accent characters, etc ).

4. quotation marks
Shell has four types of reference:
Double quotation marks
''Single quotes
''Anti-quotation marks
Backslash

Note:
"" Can reference any character or string except $, ', and. Variables in "" can normally DISPLAY variable values.
''Is similar to" ". The difference is that shell ignores any reference values.
Example: GIRL = 'girl'
Echo "The '$ GIRL' did well"
Print: The 'girl 'did well

Note:
'''Is used to set the output of system commands to variables. shell uses the content in ''as a system command and executes the quality.
For example, echo 'date' prints the current system time.

Note:
Characters Used to block special meanings: & * + ^ $ '|?
For example, expr 12*12 will output 144

5. Several Modes of variable settings
Different modes for variable setting:
Valiable_name = value: Set the actual value to variable_name
Valiable_name + value if variable_name is set, reset its value
Valiable_name :? If variable_name is not set for value, the undefined user error message is displayed first.
Valiable_name? If variable_name is not set for value, the system error message is displayed.
Valiable_name: = value if variable_name is not set, set its value
The valiable_name-value is the same as above, but it is not set to variable_name

Note: the flexible application of the above modes will greatly improve programming skills.

6. Conditional Test
The test command is used to test the string, File status, and number, and expr to test and execute numerical output.
Test format: test condition or [condition] (Note that there must be a space on both sides of condition; otherwise, an error will be reported). If the test command returns 0, the result is successful.
Three tests of test are described as follows:
File status test (commonly used)
-D: test whether the folder is used.
-F: test whether a common file is used.
-L test whether a file is linked
-R test file readable
-W test file writable
-X test file executable
-S test file is not empty

1. String Testing
Five formats: test "string"
Test string_operator "string"
Test "string" string_operator "string"
[String_operator "string"]
["String" string_operator "string"]
The string_operator can be:
= The two strings are equal
! = Two strings
-Z empty string
-N non-empty string

2. Numerical Test
Two formats: "number" number_operator "number"
["Number" number_operator "number"]
Number_operator can be-eq,-ne,-gt,-lt, and-ge.

For example: NUMBER = 130
["990"-le "995"-a "NUMBER"-gt "133"]
(-A indicates that the result is "and ")

The expr command is generally used as an integer, but can also be used as a string.
Format: expr srgument operator argument
Example: expr 10 + 10
Expr 10 ^ 2 (10 square)
Expr $ value + 10

Incremental count -- the most basic usage of expr in a loop
Example: LOOP = 0
LOOP = 'expr $ LOOP + 1'

3. Pattern Matching: Use the specified colon option to calculate the number of characters in the string
Example: valueappsaccount.doc
Expr $ value: '(. * mongo.doc'
Output account

7. Command Execution Sequence

& After successfully executing a command, execute the next
| One command fails to be executed before another command is executed
() Execute a group of commands in the current shell (Format: (command 1; command 2 ;......))
{} Same ()
Example: comet mouth_end | (echo "hello" | mail dave; exit)
If no () exists, shell will directly execute the last command (exit)

In shell programming, the command execution sequence sometimes determines the execution result of a program, and & | is the most widely used.

8. Script debugging
Script debugging
The most useful tool for script debugging is the echo command, which can print information about variables or operations at any time to help locate errors.
You can also use the print final state ($ ?) Command to determine whether the command is successful. Note that the command to be tested should be executed immediately

Output $ ?, Otherwise, $? Will change.

The Set command can also be used to assist in script testing:
Set-n read command but not executed
Set-v: Display All rows read
Set-x displays all commands and Their Parameters
To disable the set option, you only need to replace-with +.

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.