Shell programming Details (II.)

Source: Internet
Author: User
Tags line editor

File test:

-e file: Test files exist

-F File: Test files are normal files

-D File: Tests whether the specified path is a path

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

-W File: Tests whether the current user has write permission to the specified file

-X file: Tests whether the current user has execute permissions on the specified file


Test the shell for syntax errors

Bash-n xx.sh: Be able to see if the script syntax is wrong

Bash-x xx.sh: Single-step, for script debugging


To define a script exit status code:

Exit: Exit Script

Exit n:0 indicates normal exit, 1-255 indicates error exit

If the script does not explicitly define the exit status code, then the exit code of the last command executed is the exit status code of the script


Special variables:

$?: previous command exit status

$#: Number of parameters

$*: Parameter list

[email protected]: parameter list


Position variable: Shift


SED usage

Sed:stream Editor, line editor, default does not edit the original file, only the data processing in the pattern space

sed [option] ' Addressconmand ' file ...

-N: Static mode, no longer displays the contents of the mode space by default

-I: Modify the original file directly

-E script-e script: Multiple scripts can be executed at the same time

-F Sedfile: Multiple scripts can be placed in one

Sed-f sedfile File

-r: Indicates the use of regular expressions

Adress:

1, Startline,endline

For example: 1,100

$: Indicates the last line

2,/regexp/

For example:/^root/

3,/pattern1/,/pattern2/

The first time the line is pattern1 matched to the beginning of the line to the first Cup pattern2 matches to the end of the row, all the rows in the middle

4, linenumber the specified line

5, Startline,+n

From startline onwards, n rows Backward

Command:

D: Delete rows that match the criteria

P: Show rows that match the criteria

A \string: Appends a new line after the specified line, with the contents of string

\ n: can be used for line wrapping

I \string: Adds a new row before the specified line, with the contents of string

R file: Adds the contents of the specified file to the qualifying line

W File: Save the row in the range specified by the address to the specified file

s/pattern/string/: Find and replace, by default only the first match in each line is matched by the pattern to the string

Add modifier:

G: Global Substitution

I: Ignore case

s///: can also use S###,[email protected]@@

&: Reference pattern matches entire string

For example:

L.. E:like->liker

Love->lover

Sed ' s#\ (L.. e\) #&r#g ' xx.txt or sed ' s#\ (L. e\) #\1r#g ' Xx.txt


This article is from the "Forget the Past" blog, please be sure to keep this source http://xujingbo.blog.51cto.com/4633099/1826847

Shell programming Details (II.)

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.