Getting Started with Shell programming

Source: Internet
Author: User

Getting Started with Shell programming

~/.bash_history//root file path preserving the command history

$HISTSIZE//History command variable


Sleep 100//Sleeping 100

Sleep 200//sleeping 200

CTRL + Z//pause command Run

Jobs//View tasks

FG//Recall front Desk

FG 2//Select a process to be recalled to the foreground

BG//Recall back-office operation


ENV//Display System variables

Set//Show more variables (including env variables and custom variables)

Variable cannot start with a number

Myvim= ' which vim '//anti-quote ' is the result of the reference command

c= "$a"//$a there are numbers that need double quotes "" and will be separated

Input bash enters a child shell, and if you need to use a variable from the previous shell, make a global declaration of export A=1

unset//Cancel the value of a variable


/ETC/PROFILE.D/PATH.SH//Previously defined in the/etc/profile file path is not very good, it is recommended to define under PROFILE.D

Source/etc/profile//reset takes effect,/etc/profile can automatically load all the. sh


/ETC/BASHRC//Definition PS1

Ps1=[\[email protected]\h \w]\$//define command prefixes

Ps1= ' [\[email protected]\h \w]\$ ' again cd/tmp//W to w display only last paragraph

Ps1= ' [\[email protected]\h-\t \w]\$ '//define Time


/etc/profile/umask.sh//define UMASK.SH in this path change umask value


Special symbols

*//With multiple positions

? Only one

#//Notes

\//de-symbol, remove the original effect

$//To use this variable will be added $

&//Put commands in the background

&&//write two commands together




Cut//Segment

Cut-d:-F 3,4,5/etc/passwd//To: for the separator, the 3,4,5 section is displayed

Cut-d:-F 3,6,5/etc/passwd//To: for the separator, the 3,6,5 section is displayed

Cut-c 10/etc/passwd//intercept 10th character

Cut-c 1-10/etc/passwd//intercept 1-10 characters


Sort//sorting

SORT/ETC/PASSWD//No option to sort by ASCII table

SORT-T:-k3-n/etc/passwd//To: For the separator number, take the 3rd paragraph, sorted by number

SORT-T:-k3,5-n/etc/passwd//To: For the separator number, take the 3rd to 5th paragraph, sorted by the number. Reminder: 3,5 is an interval, meaning 3 to 5. Instead of being represented as 3-5. It's different from cut.

SORT-T:-k3,5-n-r/etc/passwd//To: For the separator number, take the 3rd to 5th paragraph, sorted by the number reverse order.

Sort-u//GO repeat


Wc

-W calculates the number of words

-M calculates the number of characters

-L calculates the number of rows

Uniq to repeat (if two are not to be repeated)

Uniq-c 2.txt//view quantity in-C

Sort 2.txt | Uniq//Sort first and then repeat with Sort-u 2.txt is the same


Tee Redirection and output information displayed on the screen, need to add piping |

echo "1111" | Tee 1.txt//redirect to 1.txt and output information to the screen, overwriting the original data written in 1.txt text


TR Replacement Character

LS *.txt | Tr ' A-Z ' A-Z '//lowercase to uppercase

echo "FASDFASDFASDF" | Tr ' a ' R '//convert individual lowercase to uppercase


Split cutting Large files

Split-b 50m 1.txt; Split-l 1.txt

Split-l anaconda-ks.cfg//Split one file per 10 lines

Split-b anaconda-ks.cfg new_//100b partition a file, add new for the first definition, easy to distinguish, in addition to B, other units to add


&& Reading and

The previous command executes successfully and executes the following command

The preceding command does not execute successfully, and the following command is not executed


|| Or

The command on the right is not executed until the left command executes successfully


Regular expressions

grep filters The specified rows

grep ' root '/etc/passwd//matches to the row

grep--color ' root '-n/etc/passwd//color shows the line and line number to match

Alias CG = ' grep--color '//home directory. BASHRC define Alias

Cg-v ' root '/etc/passwd//not including the root line

Cg-c ' root '/etc/passwd//Total match occurrences

CG ' ^[0-9] '/etc/passwd//This symbol ^ is the line starting with what

CG ' [^0-9] '/etc/passwd//This symbol ^ in parentheses meaning is not the same, meaning is not 0-9, 0-9 excluded rows

CG ' ^$ '/etc/passwd//This symbol $ is blank line


Symbols for regular expressions

. Any single character

*//0 or more * preceding characters

. *//any character of any length

\? 0 or 1 preceding characters



Egrep

+ represents one or more preceding characters, excluding 0

grep--color ' r\?o ' 1.txt = = egrep--color ' r?o ' 1.txt//This symbol \? With Egrep no need to add the characters

Egrep--color ' r+o ' 1.txt//This symbol + denotes one or more preceding characters, excluding 0

Egrep--color ' root|nologin ' 1.txt//This symbol | Yes or this or that.

Egrep--color ' (RR) + ' 1.txt//This symbol () represents the whole

Egrep--color ' (RR) {1,3} ' 1.txt//specified number of times



sed function is to print, match, delete

Sed-n ' P 1.txt//print line 10th

Sed-n ' 1,10 ' P 1.txt//Specify range 1 to 10 rows

Sed-n ' 30,$ ' P 1.txt//30th line to Tail line

Sed-n '/root/' 1.txt//print the line containing the root

Sed-r-N '/(R) +/'//Plus option-r no need to add the caret

Sed ' ^$ ' d 1.txt//Remove blank line, not really deleted, just display to screen

Sed-i ' 1,19 ' d 1.txt//plus-i option is really deleted


SED replacement

Sed ' 1,10s/nologin/login/g ' 1.txt//replace Nologin directly with login

Sed ' s/nologin/login/g ' 1.txt//s replace the global nologin with login

Sed ' s#.*$ #login #g '//All lines are replaced by login

Sed ' s#.*$#&login#g '//All lines at the end plus login, need to use &

Sed ' s#[a-za-z]# #g '//Remove all characters

Sed-r ' s# (^[a-z]+) (:. *:) (. *$) #\3\2\1#g ' 1.txt//replace the preceding character with a later character

Sed-n '/root/p; /aming/p ' 1.txt = sed-n-E '/user/p '-e '/aming/p ' 1.txt//This expression can write multiple statements


Awk

Segmentation

Awk-f ': ' {print $} ' 1.txt//-f Specifies delimiter: segment display, but display will be delimited by spaces

Awk-f ': ' ofs= ': ' {print $3,$4} ' 1.txt//-f Specify delimiter: Segment display, but display will be separated by a space, adding ofs= ":" is displayed with: delimiter


The

awk '/user|boot/' 1.txt//match user or boot

Awk-f ': ' $1~/r*o/' 1.txt//in $ (the first paragraph) contains/r*o/will be matched

Awk-f ': ' $1~/r*o/{print $} ' 1.txt//in $ (the first paragraph) contains/r*o/will be matched to show only the third paragraph during the matching process

Awk-f ': ' $1~/r*o/{print $1,$3}; $1~/user/{print $1,$3} ' 1.txt//multi-segment matching

Conditional operator

Awk-f ': ' $1== ' nobody ' 1.txt//$1= ' nobody ' segment matches

Awk-f ': ' $3>=500 ' 1.txt//$3>=500 segment matches out


Internal Address variables

Awk-f ': ' nr<10 ' 1.txt//Print out the first 9 lines

Awk-f ': ' nr==10 {print $1,$7} ' 1.txt = = Awk-f ': ' {if (nr==10) print $1,$7} ' 1.txt//Print out the first 10 lines and print the specified segment

Awk-f ': ' {if (nf==7) print NF $ ' 1.txt//Match segment

Awk-f ': ' {(sum=sum+$3)}; END {print sum} ' 1.txt//sum of all segments


Getting Started with Shell programming

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.