Shell Programming Basics Summary _linux Shell

Source: Internet
Author: User
Tags character classes control characters echo command numeric

1, File type introduction
Linux systems mainly include the following seven types of files: D directory-normal file L Symbolic link s socket file B block device file c character device file p named pipe file

2, Regular expression
Regular expressions are very important in shell programming.

Extracts or filters text from a file or command output. You can use regular expressions (RE), a regular expression is a collection of special or not very special string patterns.

Basic meta-Character set:
^ matches only the beginning of the line.
$ matches only the end of a line.
* A single character followed by the *, matching 0 or more of this single character.
[] matches the characters in [], either a single character or a sequence of characters. You can use-to represent [] within the scope, as [1-5] is equivalent to [1,2,3,4,5].
Masks the special meaning of a meta character, such as $ for the character $, and does not represent a matching end of the line.
. Matches any single word character.
Pattern{n} matches the number of pattern occurrences n
Pattern{n,}m matches the number of occurrences of pattern, but with a minimum number of n
PATTERN{N,M} matches the number of pattern occurrences between N and M (N,m is 0-255)

Common examples:
To display executable files: ls–l | grep ... x...x. X
Show folders only: Ls–l | grep ^d
Match all blank lines: ^$
Match all the words: [A-Z a-z]*
Match any non-alphanumeric character: [^a-z-A-z]
Lines containing eight characters: ^........$ (8.)

For more information, consult the details of regular expressions to deepen your study.

3, Character class description
Common list of available character classes:
[: Alnum:] alpha-numeric [A-Z-0-9]
[: Alpha:] Letter [A-A-z]
[: Blank:] Space or TAB key
[: Cntrl:] any control characters
[:d Igit:] number [0-9]
[: Graph:] Any visual character (no spaces)
[: Lower:] lowercase [A-z]
[:p rint:] Non-control characters
[:p UNCT:] punctuation character
[: Space:] Spaces
[: Upper:] Capital [A-z]
[: xdigit:] hexadecimal number [0-9 a-f a-f]

You can use character classes to better accommodate non-English locale (including some of the required accent characters, and so on).

4. Quotation mark type
The shell has four types of references:
"" Double quote
' Single quotes
' Inverted quotes
Back slash

Description
"" to refer to any character or string except $, ',,, or, "" The variable can display the value of the variable normally.
"" is similar to "", except that the shell ignores any reference values.
For example: Girl= ' GIRL '
echo "The ' $GIRL ' did"
Then print: The ' girl ' did

Description
"is used to set the output of a system command to a variable, and the shell takes the contents of the ' as a system command and executes the quality.
For example, echo ' date ' prints the current system time.

Description
Used to mask the special meaning of the character:& * + ^ $ ' "| ?
For example: Expr 12 * 12 will output 144

5. Several modes of variable setting
Different modes when the variable is set:
Valiable_name=value set the actual value to the Variable_name
Valiable_name+value if Variable_name is set, resets its value
Valiable_name:?value If Variable_name is not set, the user error message is not defined first
Valiable_name?value If Variable_name is not set, the system error message is displayed
Valiable_name:=value If Variable_name is not set, its value is set
Valiable_name-value Ibid, but values are not set to Variable_name

Note: The flexible application of these models will greatly enhance the programming skills.

The

6, conditional test
Test command is used to test strings, file states and numbers, expr test and perform numeric output.
Test format: Test condition or [condition] (special attention should be paid to a space on either side of condition, or an error), the test command returns 0 to indicate success.
The following three tests are described separately:
File state test (commonly used)
-d           test whether folder
-f            test for general files
-l           test whether to link files
-r           Test whether the file is readable
-w         the test file is writable
-x            test file to perform
-s           Test whether the file is not empty

1. String test
Five formats: Test "string"
Test String_operator "String"
Test "string" String_operator "string"
[String_operator "string"]
["String" String_operator "string"]
Where the string_operator can be:
= Two string equal
!= two string unequal
-Z Empty string
-N Non-empty string

2. Numerical test
Two forms: "Number" Number_operator "number"
["Number" number_operator "number"]
Where: Number_operator can be:-eq,-ne,-gt,-lt,-ge

For example: number=130
["990" –le "995" –a "number"-GT "133"]
(Where-a indicates the result phase "and")

The expr command is generally used for integer values, but can also be used for strings.
Format: Expr srgument operator operator argument
For example: Expr 10 + 10
Expr 10 ^ 2 (square of 10)
Expr $value + 10

Incremental count--expr The most basic usage in a loop
For example: Loop=0
loop= ' expr $LOOP + 1 '

3. Pattern matching: Calculates the number of characters in a string by specifying the colon option
For example: Value=account.doc
Expr $value: ' (. *). Doc '
Output account

7. Order of execution of order

&& execute a command successfully before executing the next
|| One command fails and another command is executed
() execute a set of commands in the current shell (format: (command 1; command 2; ...) )
{} with ()
For example: Comet Mouth_end | | (echo "Hello" | mail dave; exit)
If not (), the shell executes the last command directly (exit)

In shell programming, the order in which commands are executed sometimes determines the execution result of a program, and && | Most widely used.

8, script debugging
Script debugging
The most useful debugging script tool 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 status ($) command to determine whether a command is successful, and you should be aware that immediately after you perform the command you want to test

Output $?, otherwise $? will change.

The set command can also be used to aid script testing:
Set–n Read command but not executed
Set–v Show All rows read
Set–x display all commands and their parameters
To turn off SET options, just switch to +.

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.