Shell Programming---File testing and special variables

Source: Internet
Author: User
Tags arithmetic first string modifier

I. Arithmetic operations

1. Let arithmetic operation expression

Let c= $A + $B

2. $[arithmetic operation expression]

c=$[$A + $B]

3, $ (arithmetic operation expression)

c=$ (($A + $B))

4, expr arithmetic expression, the expression in the operands and operators to have a space between, and to use the command reference

c= ' expr $A + $B '

5. Ending the script early

Exit Status Return code

6. File Testing

-e file: Test files exist

-F File: Test files are normal files

-D FILE: Tests whether the specified path is a directory

-R File: Tests whether the current user has Read permission 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

[-e/etc/inittab]

[-x/etc/rc.d/rc.sysinit]

7. Test if the script has syntax errors

Bash-n Script

Bash-x Script: Stepping

Types of 8.bash variables

Local variables (local variables)

Environment variables

Positional variables:

$, $, ...

Shift

Special variables:

$?: Execution status return code of the command

$#: Number of parameters

$*: Parameter list

[email protected]: parameter list

9.sed command

1. Mode space

By default, the original file is not edited, only the data in the pattern space is processed, and then, after the processing is finished, the pattern space is printed to the screen (the original file is read into the schema space line-by-row into the pattern spaces and pattern matching, the processing of non-conformance is discarded)

2.sed [options] ' addresscommand ' file ...

-N: Silent 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/path/to/sed_script

Sed-f/path/to/scripts File

-r: Indicates the use of extended regular expressions

Address:

1, Startline,endline

Like 1,100.

$: Last line

2,/regexp/

/^root/

3,/pattern1/,/pattern2/

The first line that is matched by the pattern1 begins at the end of the line that is first matched to the pattern2, and all the rows in the middle

4, LineNumber

The specified row

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/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern

Add modifier

G: Global Substitution

I: Ignore character case

s///: s###, [email protected]@@

\ (\), \1, \2

L.. E:like-->liker

love-->lover

Like-->like

Love-->love

&: Reference pattern matches entire string

Sed ' 1,2d '/etc/passwd delete the specified line in the file

Sed exercises:

1. Delete the blank characters from the beginning of the/etc/grub.conf file

# sed-r ' [email protected]^[[:spapce:]][email protected]@g '/etc/grub.conf

2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5

# sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/etc/inittab

3. Delete blank lines in the/etc/inittab file

# sed '/^$/d '/etc/inittab

4. Delete the # number at the beginning of the/etc/inittab file

# sed ' [email protected]^#@@g '/etc/inittab

5. Delete the # and the trailing whitespace characters from the beginning of a file, but require a white-space character after the # # number

# sed-r ' [email protected]^#[[:space:]][email protected]@g '/etc/inittab

6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #

# sed-r ' [email protected]^[[:space:]]+#@@g '/etc/inittab

7. Take out a directory name for a file path

# echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

Base name:

# echo "/etc/rc.d/" | Sed-r ' [Email protected]^/.*/([^/]+)/[email Protected]\[email protected] '






This article is from "Luo Chen's blog" blog, please be sure to keep this source http://luochen2015.blog.51cto.com/9772274/1636971

Shell Programming---File testing and special variables

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.