Powerful query character matching commands and shell scripts in Linux

Source: Internet
Author: User
Tags egrep

Variable:

Local variable: valid only for the current

Environment variable: valid for both the current shell and sub-shell

Export A = 3 define a as a variable and assign 3 to

A = 3

Export a reference does not need to be added &

Export also views Environment Variables

[Root @ server56 ~] # Export

Declare-x cvs_rsh = "ssh"

Declare-x g_broken_filenames = "1"

Declare-x histsize = "1000"

Declare-x home = "/root"

Declare-x hostname = "server56.example.com"

Declare-x inputrc = "/etc/inputrc"

Declare-x lang = "zh_cn.gbk"

Declare-x lessopen = "|/usr/bin/lesspipe. sh % s"

Declare-x LOGNAME = "root"

Declare-x ls_colors = "NO = 00: fi = 00: di = 00; 34: Ln = 00; 36: Pi = 40; 33: So = 00; 35: BD = 40; 33; 01: Cd = 40; 33; 01: OR = 01; 05; 37; 41: MI = 01; 05; 37; 41: Ex = 00; 32 :*. cmd = 00; 32 :*. EXE = 00; 32 :*. COM = 00; 32 :*. BTM = 00; 32 :*. bat = 00; 32 :*. SH = 00; 32 :*. CSH = 00; 32 :*. tar = 00; 31 :*. tgz = 00; 31 :*. ARJ = 00; 31 :*. taz = 00; 31 :*. lzh = 00; 31 :*. zip = 00; 31 :*. z = 00; 31 :*. z = 00; 31 :*. gz = 00; 31 :*. bz2 = 00; 31 :*. bz = 00; 31 :*. TZ = 00; 31 :*. rpm = 00; 31 :*. cpio = 00; 31 :*. JPG = 00; 35 :*. GIF = 00; 35 :*. BMP = 00; 35 :*. xbm = 00; 35 :*. XPM = 00; 35 :*. PNG = 00; 35 :*. TIF = 00; 35 :"

Declare-x mail = "/var/spool/mail/root"

Declare-x oldpwd

Declare-x Path = "/usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin: /usr/sbin:/usr/bin:/root/bin"

Declare-x Pwd = "/root"

Declare-x shell = "/bin/bash"

Declare-x shlvl = "1"

Declare-x ssh_askpass = "/usr/libexec/OpenSSH/gnome-ssh-askpass"

Declare-x ssh_client = "192.168.0.211 49242 22"

Declare-x ssh_connection = "192.168.0.211 49242 192.168.0.156 22"

Declare-x ssh_tty = "/dev/pts/0"

Declare-x term = "xterm"

Declare-x user = "root"

Display: You can define which Desktop you are on

Histsize: Number of historical commands defined

Home: current home directory

PS1 base name

Env: displays the current variable.

Mainly used in characteristic Environments

 

Special variable: Bash

$? : Whether the previous command is successfully executed. 0 indicates successful, and 1-indicates different errors.

Location variable: $1: directly pass a variable to the script

$2, $3, more than 10, it is recommended to add {}

Arithmetic Operation echo "$ [$ A + $ B]"

"$ ($ A + $ B ))"

Bash command alias:

Alias is only valid for the moment

When the following command has spaces, it must be enclosed by quotation marks.

Alias cdnet = 'CD/etc/sysconfig/network-scripts'

Unalias Revocation

Global:/etc/profile,/etc/profile. d/*,/etc/bashrc

Local :~ /. Bash_profile ,~ /. Bashrc ,~ /. Bash_logout

Profile class:

It is usually used to set environment variables and run some commands to be executed during user logon.

Bashrc class:

Set the alias and local variables, only valid for the current Shell

Shell:

Interactive login shell: user login required <input type = "text"

Step 1: Read/etc/profile -->/etc/profile. d/> * --> ~ /. Bash --> ~ /. Bashrc -->/etc/bashrc

Non-interactive login shell: script shell that can be run without user logon

Log on first ~ /. Bashrc -->/etc/profile. d /*

Define alias declaration Variables

Source/etc/profile repeat this file

./Etc/profile re-reading this file is not recommended

\ Avoid Intention

Input/Output redirection

The default value is standard input/output.

Standard output device:/dev/stdout, 1 monitor,>

Error output:/dev/stderr, 2, monitor, 2>

Standard Input Device:/dev/stdin, 0 indicates standard input, keyboard, <

Merge output streams &>

> Overwrite output redirection> append output redirection 2> error message append redirection &> not append

Set-C force append to prohibit Overwrite

> | Force Overwrite

Ls/var>/tmp/var. out 2>/tmp/var. Err

Output stream

2> & 1 = &>

Pipeline: | used to connect commands. The output result of the previous command is treated as the input result of the next command.

Command | command

Tee separates input streams

Ls/etc | tee/tmp/LS. Out | less

The output of multiple commands must be saved to the same file.

Custom output redirection

0, 1, 2

Exec 3>/tmp/myout. Out defines a fixed File

Ls> & 3 overwrite fixed files

Exec 3> &-Undo

The input usage is the same as above.

&>/Dev/null: sets unwanted files to a data black hole.

>/Dev/null

Program execution flow:

Sequential execution

Select Branch

Loop execution: 1 +... + 100

For I in 1 2 3 4 5; do

Statement1

Statement2

Done

1 2 3 4 5 = 'seq 1 10'

= $ Seq 1 10 $

= {1 .. 10}

Text processing command:

The cut-D Delimiter is:

Cut-D:-F1 remove the first segment

Ifs: built-in line break

Ifs = $ '\ N'

Grep:

Grep, egrep, fgrep

Grep [Options] "pattern [mode]" File

Regular Expression: Basic regular expression, extended Regular Expression

Metacharacter re

Grep-I is case insensitive

-V is opposite to the default action. The matching result is not displayed.

-N: The matched row number is displayed in the text.

-A [after] n: the matching n rows are displayed.

-B [before] n

-C [] n context, displaying n rows online

-- Color: highlight matched items.

-E = egrep

^: Locates the first line of the line and matches the rivet.

$: Locate the end of a row

. Match a single character

*: Match the previous character 0 or any times

[]: Match any one of them

[X-y]

\ <

\>

\(..\)

$? : Execution result of the previous command

0: Execution successful

1-255: Failed

Short-circuit OPERATOR:

&&:

Command & command: execution after successful execution

|: After execution fails

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.