First part Shell Programming 1 basics

Source: Internet
Author: User


Path to the LS Etc/init.d/shell script

ls/usr/local/apache2/
Ls/usr/local/apache2/bin/apachectl


1. Shell Features
Order history!! !$!n! character
Tab key to complete file path or command
Alias a= "B" Unalias A
Wildcard * Matches 0 or more characters? Match one character
Input/output redirection, >>, < reverse redirect, 2>, 2>> error redirection
Pipe symbol |
Job Control CTRL + Z, jobs view background Stop command, FG Recovery background Stop command, BG n n indicates to view the first few running commands in the background

Sleep & put in the background to perform


ls aming >1.log 2>err.log = ls aming >1.log 2>&1

Mail-s "ADHOFDJF" [email protected] </etc/passwd
Wc-l </etc/passwd
39

Cat 1.log | Wc-l


2. Variables

System variable names are uppercase, echo can view variable names
Env can list all environment variables for the current user and user-defined global variables
The SET command can list all variables to include system and custom global variables as well as current shell custom variables
The custom variable rules are set under Linux: (1) The format is "A=b", where a is the variable name, B is the contents of the variable, there is no space on either side of the equal sign, and (2) the variable name can only consist of English, numeric and underscore, and cannot start with a number; (3) A single quotation mark is required, (4) if the contents of the variable need to be used to run the results of other commands can use anti-quotation marks; (5) variable content can accumulate the contents of other variables, need double quotation marks;

All users of the system use variables: Export myname=aming global variable, add/etc/profile and Source/etc/profile Permanent
A user of the system uses the variable: Export myname=aming to the. BASHRC source in the current user home directory. BASHRC
Export myname=aming global variable, export without any option representation, declare all environment variables and user-defined variables
User-defined variables, which can be set using the unset variable name

A=1
Echo $a

Unset A to erase the value of a


Echo $HOME
Echo $LANG

filename=/etc/passwd
Echo $filename

Com= ' Ls/root '
Echo $com

RPM-QF ' which vim ' symbol represents the result of the quote command


[Email protected]/]# a=1
[[email protected]/]# b= $a: 123
[Email protected]/]# echo $b
1:123


3. Configuration files for system and personal environment variables
/etc/profile PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, histsize, umask, etc.
/ETC/BASHRC $PS 1 Umask later if you set umask modify/etc/profile do not change this file
. bash_profile user's own environment variables
. BASHRC the file is executed when the user logs on and every time the new shell is opened
. bash_history Record Command history with
. bash_logout: When you exit the shell, the file is executed.


[Email protected] ~]# echo $PS 1
[\[email protected]\h \w]\$
[[email protected] ~]# output is the meaning of this


Ps1= "[\[email protected]\h \w \t]\\$" Add a Time


Global configuration

/etc/profile
/etc/profile.d/*.sh
/etc/bashrc

4. Special symbols in the shell
* Match symbol, 0 or more arbitrary characters
? Match symbol, 1 arbitrary characters
# Follow the instructions so that the contents of the back lose their original meaning
\ de-semantic characters, reverting special characters to normal characters
| Drop the result of the preceding command to the command following the symbol, typically for document operations such as Cat, less, head, tail, grep, cut, sort, WC, uniq, tee, tr, split, SED, awk, etc.

$ reference variable, and!$
; Semicolon, multiple commands to write a row, separate commands
~ User Home Directory
& Put the command on the last side of the command to run it in the background
>>, 2>, 2>> [ERROR] redirect, append [ERROR] redirect
[] brackets, the middle is a combination of characters, representing any of the intermediate characters in the LS 1[23a].txt


5. Common commands:
1) cut
syntax: cut-d ' delimited character ' [-CF] n here n is a positive integer
-d specifies the delimiter, enclosed in single quotation marks,-f specifies the paragraph cut-d ': '-F 1 /etc/passwd |head-n 5.
cut-d:-f1/etc/passwd |head-n 5
-c There is only one number behind to intercept the first few characters head-n2/etc/passwd|cut-c2
-c followed by a number area, which means intercept from a few to several head- N2/etc/passwd|cut-c2-5

[[email protected] ~]# head/etc/passwd |cut-d:-f3
0
1
2
3
4
5
6
7
8
10


[Email protected] ~]# HEAD/ETC/PASSWD|CUT-C2
O
I
A
D
P
Y
H
A
A
U

Cut and sort are used together

2) sort
Syntax: sort [-t delimiter] [-KN1,N2] [-nru] (N1<N2)
No option, backward from the first character, followed by ASCII values in ascending order SORT/ETC/PASSWD
-N sort from small to large, using pure numbers
-t specifies the delimiter after-kn1,n2, which is ordered in the specified interval, followed by a number to sort the nth character after-K
SORT-T:-k3-n/etc/passwd
-R means sort in descending order Sort-t:-k3,5-r/etc/passwd k indicates period
-U de-cut-d:-f4/etc/passwd |sort-n-U

[[email protected] ~]# cut-d:-f3/etc/passwd |sort-n
0
1
2
3
4
5
6
7
8
10
11
12
13
14
69
74
81
89
99
499

Sort-n-U 1.txt |xargs

3) WC
Number of lines, characters, and words used to count documents
Without any options, the number of lines, words, and characters will be displayed.
-L count rows
-M statistic character number
-W Statistic Word number

4) Uniq
Uniq to repeat, the most common is a-C used to count the number of repetitions, to go to the first order sort Testb.txt |uniq-c

Uniq 1.txt |wc-l

Sort-n 1.txt |uniq Existing sort

Sort-n 1.txt |uniq-c |sort-n Multiple pipes, C represents the number of repetitions displayed


5) Tee
followed by the file name, similar to a, the specific gravity of a function, the file is written in the following file, while also displayed on the screen
|tee ==> + screen

Tcpdump-nn grabbed the bag
Tcpdump-nn > 2.log

Wc-l 2.log

Tcpdump-nn |tee 2.log is not only displayed on the screen, but also directed to the 2.log inside, the dynamic

6) TR is used to replace characters
The most common is the case conversion: head-2/etc/passwd |tr ' [A-z] ' [A-z] '
TR replacing a character is also possible with grep ' root '/etc/passwd |tr ' r ' R '

7) split cut large files with
-b: Split units by size to byte Split-b50 1.txt

-l: delimited by rows, Split-l10 file

[[Email protected] ~] #split-b5 1.txt
[[email protected] ~]# ls
1.txt Install.log Xaa xac xae
Anaconda-ks.cfg install.log.syslog Xab xad xaf
[Email protected] ~]# DU-SB x*
5 XAA
5 Xab
5 Xac
5 Xad
5 Xae
3 Xaf

RM-RF x*

6. && | |
Command1; Command2 the previous command is complete executes the following command
Command1 && Command2 Only the previous command execution succeeds will execute the following command
Command1 | | Command2 only the previous command failed to execute the following command.

First part Shell Programming 1 basics

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.