Shell Learning Notes---transcription

Source: Internet
Author: User
Tags first string

SHELL notes

1. View the size of a file or directory ls du

2. View status Stat file name

3, the system itself, usually uppercase

4. System variables are viewed by set or declare instructions

5. UDV variable (user defined variable)
User creation and maintenance, recommended capitalization (why?) )
Reason: Avoid conflicts and confusion because commands are all lowercase in a Linux system

6. All variable definitions in the Shell are strings.
Variables are case-sensitive, such as: Foo and foo are two different variables
A variable must begin with a letter or underscore, not a special character such as ",", ":", "\".

7, 1) Definition of variable
Variable name = value (there is no space between the variable name and the value "=") Example: Lang=cn and lagn= en LANG =en
2) declaration with a space variable
Need to add quotation marks-"" Example: My=23 67
3) Declare a read-only variable readonly My_udv=guozi
4) Modifying variables
Modify Environment Variables Example: path= $PATH:/opt/bin Modify user-defined variables example: src= "Guozi"

8, the use of variables
"$" symbol before variable
echo $HOME or Echo ${home} echo $USER or Echo ${user}
Thinking: Why use variables? Why add a delimiter: {}?

Because of this, the variable name can continue to receive content after

9. REDIRECT Output

Direct input: ls–l > Lsoutput.txt append: Ls–l/home >> lsoutput.txt
Output to empty device:/dev/null
Where does the ">" Delegate redirect to
"1" means stdout standard output, system default value is 1
">/dev/null" is equivalent to "1>/dev/null"
"2" indicates stderr standard error
"&" means equivalent, 2>&1, 2 output redirect equals 1
Example: Killall ser >/dev/null 2>&1

10. REDIRECT Input
Interactive input in Shell terminal
Format: Cat >> 2.txt <<eof [content] EOF
Reading content from a file assigns a value to a variable
Format: Read variable < filename

11. display text or variable values
Format: Echo [options] [characters, variables ...]
Options:
-N non-newline output echo defaults to a line break for the output
-E enhanced. Enable escape character resolution
\a Bell \b Backspace \c remove new line from trailer
\ n a new line \ r enter \ t horizontal tab
\ \ Slash \0nnn octal \0xhh hex Output
Example: Echo-e "Welcome to Guozi ' s \n\t\\ Shell Programming"

12. Output colored characters
Foreground color: 10-digit 3 indication range: 30-37 (3x)
Background color: 10-digit 4 indication range: 40-47 (4x)
Single digit indicates color
30 (Black) 31 (red) 32 (green) 33 (yellow)
34 (blue) 35 (magenta) 36 (cyan) 37 (white)
Example: Echo-e "Network restarting ... [\033[32;40m OK \033[0m] "

13. Calculation method
There are three types of formats that can be mathematically calculated.
1.1. The first type of--expr
Format: expr operand 1 operator operand 2
Example:
1 Expr 1 + 2 tmp=$ (expr 1 + 2)
2 Expr 1 + 4/3 tmp=$ (expr 1 + 4/3)
3 Expr 1 + 2 * 3 (?)
Features: relatively strict format requirements

1.2. The second type-()
Format: $ (operand 1 operator operand 2))
Example: tmp=$ ((1 + 3))
Features: Relatively loose format requirements
1.3. The third type of--let
Format: let [variable] [operator] operand 1 operator operand 2 "
Example:
1 Let "1+2" 2 Let "tmp2=1+2"
Features: Loose format requirements, flexible calculation

14, 1, what is called reference
Feedback on the results of shell scripts, programs, terminal commands, variables, strings, and more.
2. Types of references
"Double quotation mark ' anti-quote ' single quote \ Backslash
3. Necessity of reference
Objective: To prevent unnecessary misunderstandings.
Example:
1 echo Hello World * with echo "Hello World *"
2 echo "Hello" world "Welcome"
3 echo "Hello World"

3.1. Double quotes
Use double quotation marks to reference any character or string except the character $, ', \ \.
Example:
1 echo "Welcome to #$%^ to Shell programming"
2 echo "Welcome to $ABC Programming"
3.2. Single quotation marks
Single quotes are similar to double quotes, but the shell ignores any reference values.
Example:
echo "The ' $BOY ' is years old"
3.3. Anti-Quote
Anti-quotes are used to output system commands to variables, for example:
1 echo ' Hello ' (wrong x)
2 echo ' Date ' (to √)
3.4. Backslashes
If the next character has a special meaning, the backslash prevents the shell from misunderstanding its meaning
The following characters contain special meanings: & * + ^ $ ' "|?.

1. Symbols for matching files
Definition: A symbol that matches a string type is called a meta character.
1.1. * Matching Files
* matches any string in the file name, including an empty string.
Example: ls/etc/*.conf ls/etc/cg*.co*f
1.2.? Matching files
? Matches any single character in the file name.
Example: LS/ETC/PPTP?. Conf ls/etc/?? St.conf
1.3.[] Matching files
? [...] Matches any one of the characters contained in [].
Messy match: ls/etc/[wersdfp]syslog.conf
Range matching: LS [0-9][a-za-z].txt
? [!...] Match [] Non-exclamation! Any one of the following characters.
Just the opposite of the previous rule.
Example: LS [!0-9]*.txt matches all files with a suffix named txt that begin with a non-digit.

2.1. Execution result of the command
View command execution return value: Echo $? Success: return 0 failure: return non 0
2. Command execution control
2.1. Use && Symbols
Format: Command 1 && command 2 && ...
Description: If command 1 executes successfully, then execute command 2 ...
Example: Cp/home/file.txt/opt && echo "Copy file Success"
When compiling the source code:./configure && make && make install
Use: Debug for execution of scripts.
2.2. Use | | Symbol
Format: Command 1 | | Command 2 | | ...
Description: If | | The command on the left failed, then execute the command on the right
Example: CP hello.txt Copy.txt | | echo "Copy file Failed"
Use: Debug for execution of scripts.
3. Combined use of commands
Format 1: ";" More than two commands at a time
Format 2: (Command 1; command 2; .... ) is executed in the current shell.
Format 3:{command 1; command 2; ...} Execute in child shell
Example:
1) ls/usr; cd; echo "OK"
2) (LS; cat hello.txt; cd) | | echo "Success"
3) {ls; cat hello.txt; cd}| | echo "Success"

3. What metacharacters are regular
[ ] \ ^ $ . | ? * + ( )
3. Basic meta-character set and its meaning
^ Match beginning of Line
$ matches end of line only
* A single character immediately following *, matching 0 or more characters
[] only matches [] inside characters. Can be a single character, or it can be a word
Sequence of characters. You can use '-' to denote a range of character sequences within [], such as
Use [1-5] instead of [1 2 3 4 5] \
\ is used to mask the special meaning of a meta-character. Because sometimes some metacharacters have special meanings in the shell. \ can make it lose its proper meaning. \ r \ n
? Tells the engine to match a leading character 0 or one time
+ tells the engine to match leading characters 1 or more times
. Match only any single character
p a t t e r n \ {n \} is used only to match the number of occurrences of the preceding p a t T e R N. n is the number of times
P a t e r n \ {n,\} only has the same meaning, but the minimum number of times is n
p a t t e R n \ {n,m \} only has the same meaning, but p a t T e r n occurs between N and M

4. Match start and end character formatting: grep {^string | string $} filename

5. Example: grep ^[a-z]tr filename grep tr$ filename

6. Match entire word format: grep ' \bword\b ' filename

7..  ? + and * format of use: grep {...... *} filename

8.. Match a single character to match a space * match any character or string repeat multiple times

9.. + character must appear 1 times. Characters appear 0 or 1 times

10. Note: grep does not support +? Metacharacters must be added to the-e option

One. Regular expression of Perl-P

12.. * Greedy match. *? Lazy Matching

Tags for grep

14. Format: grep ' \ (str\) \ (\) \ (\) [other]\1 ' filename

15. Example: Grep-n ' \ (tom\) \ (guozi\). \1 ' A3.txt–color

Pattern Search in grep

17. Format 1:grep ' [str]\{1\} ' filename

18. Format 2:grep ' [str]\{1,\} ' filename format 3:grep ' [str]\{1,3} ' filename

Basic syntax for SED commands
1.1 sed command format sed [options] {sed-commands} {Input-file}
1.2 sed p command with-n option (print,--quiet--silent)
Print only the first line: Sed-n ' 1 P ' example.txt
Print the first line to the third row sed-n ' 1,3 P ' example.txt
Print Jason's line sed-n '/jason/p ' example.txt
Print Jason to Jane's line sed-n '/jason/,/jane/p ' example.txt
Print lines beginning with 103 sed-n '/^103/p ' example.txt
Print the line ending with Manager sed-n '/manager$/p ' example.txt
Starts with a number and repeats three times Sed-n '/^[0-9]\{3\}/p ' example.txt
Print once every other line Sed-n ' P ' example.txt
1.3 sed d command (delete)
Delete 103 string where the line sed '/103/d ' example.txt
Because the D command is identical to the syntax of the P command, there are no more examples here.
1.4 More SED options
-F Read command from File
Create a new cmd.sed command file with the following file contents:/jane/p
In Terminal input command: sed-f cmd.sed example.txt
You can write multiple commands in a command file
-e option to execute multiple commands
Print the line where Jason and Jane are Sed-n-e '/jane/p '-e '/jason/p ' example.txt

20. Replace the command format sed ' [address-range|pattern-range] s/originalstring/replacement-string/[substitute-flags] ' inputfile
Address-range: The address list that represents where execution begins, such as: 1,3 represents the first row to the 3rd row.
Pattern-range: A list of styles that represents the beginning of which style matches the string. such as:/jane/, which represents the line from which the string Jane is contained, begins execution. [] are optional. S: Indicates a replacement command to execute (substitute)
Originalstring: A string that needs to be replaced
Replacement-string: the replaced string.
Substitute-flags: Optional replacement marker.
2 Preparation Create a new Example.txt text and enter the following: 101,john doe,ceo 102,jason smith,it Manager 103,raj reddy,sysadmin 104,anand Ram,develo Per 105,jane miller,sales Manager

3 Address-range and Pattern-range
A) Replace the first line of John with the Guozi string sed ' 1 s/john/guozi/' example.txt
b) Replace the Manager of the second line to line fifth with Guozi sed ' 2,5 s/manager/guozi/' example.txt
c) Replace John, the CEO of the string business, with the XXX sed '/john/s/ceo/xxx/' example.txt
d) Replace the Developer string with XXX from line fourth and the line containing the string Ram
Sed ' 4,/ram/s/developer/xxx/' example.txt
e) If there are no Address-range and Pattern-range, then the first matching string encountered in each row will be replaced.
Sed ' s/1/aaa/' example.txt
4 Substitute-flags
3.1 Global flag G (GLOBE)
The global flag replaces any strings that meet the conditions that are encountered
Sed ' s/1/aaa/g ' example.txt
3.2 Digital Signage
Replace the 2nd matching string in the matching string with the one we want.
Sed ' S/1/AAA/2 ' example.txt
3.3 See also P (print) logo sed-n ' s/1/aaa/p ' example.txt
3.4 Write Flag W (write) Each line touches the first string John replaces with a Guozi string and writes to Output.txt
Sed ' s/john/guozi/w output.txt ' example.txt to specific gravity orientation
The redirect will put all the contents of the output to the terminal into the text, and the W switch will only
The lines we have modified are written to the file.
3.5 Sensitive flag I (Ignore flag)
Ignores case, replaces all lines containing the Jason string in the text with the AAAA string
Sed ' s/jason/aaaa/gi ' example.txt
3.6 Federated Use flags can use multiple flags to replace and save a string
Sed ' s/jason/aaaa/gipw output2.txt ' example.txt
5 Replace command delimiter
Format 1:sed ' s///' example.txt
Format 2:sed ' [email protected]@@ ' example.txt useful: use a reasonable delimiter to easily read our program code.
6 powerful &--style matching
Replace the origin-string with the replace-string
Sed ' s/john/[&]/' example.txt
Requirements: Add a layer {} to the outside of the three-digit number

22.1 Replace text content directly
Useful-I option-strongly requires this option to be remembered!
2 Previous class homework instruction
Job: Truncate the preceding three digits of the text, and add braces
101,john Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
The final input effect is:
{101}
{102}
{103}
{104}
{105}
Solution one: sed ' s/\ (^[0-9]*\),. */{\1}/g ' Example.txt
Solution two: sed ' s/\ ([^,]*\). */{\1}/' Example.txt
3 other examples
A) Intercept network card IP address
Solution One: Use the Cut command
Ifconfig eth0 | grep "inet addr" | Cut-d:-F 2 | Cut-d ""-F 1
Solution Two: Use grep command
After class everyone think about it.
Solution three: sed command
1) ifconfig eth0 | Sed-e '/inet addr/!d '-e ' s/[^0-9]*//'-E
' s/. *//'
2) ifconfig eth0 | Sed-e '/inet addr/!d; s/[^0-9]*//; s/. *//'
3) ifconfig eth0 | Sed-e '/inet addr/!d;
S/\ ([^0-9]*\) \ (\ ([0-9]\{1,3\}\.\) \{3\}[0-9]\{1,3\}\). */\2/'
Solution Four: awk command
For the time being no example.
b) Remove comments and Blank lines
Sed ' s/^#.*//g ' my.cnf sed-i-e ' s/^#.*//g; /^$/d ' my.cnf

23.1. Append rows with a command
Format: sed ' [address] a the-line-to-append ' input-file
Example:
1) Append a line after the 2nd line of the text
Sed ' 2 a 203,jack johnson,engineer ' Employee.txt
2) append a line at the end of the text
Sed ' $ A 106,jack johnson,engineer ' Employee.txt
3) Add multiple lines at text match
Sed '/jason/a\
203,jack Johnson,engineer\
204,mark smith,sales Engineer ' Employee.txt
2. Inserting rows using the I command
Format: sed ' [address] I the-line-to-insert ' input-file
The usage is similar to the a command, but the difference is that before all rows of the address are matched
3. Modify rows using the C command
Format: sed ' [address] c The-line-to-insert ' Input-file
Different in meaning, similar in usage
4. Use the l command to print hidden character formatting: sed–n l Input-file
5. Use the = command to print the line number format: sed = input-file
The line number where the string is printed sed '/string/= ' input-file
6. Use the y command for character conversions
Format: sed ' y/original-string/replace-string/' input-file
Example: sed ' y/abcde/abcde/' employee.txt
Can be used to encrypt strings
7. Use the Q command to terminate execution
Example: sed ' 5 Q ' input-file terminates after execution of line 5th
Sed '/root/q ' input-file exit after matching to root line

24.4. awk command format
Format 1:awk-fs ' begin{}/pattern/{action} end{} '
Input-file
Description:-f Specifies the delimiter
1) Example: Split by comma and print a section
Awk-f, ' {print $1,$2,$3} ' Employee.txt
2) Example: awk-f: '/mail/{print '} '/etc/passwd
5. Awk built-in variables
1) awk Divider FS
awk ' BEGIN {fs= ', '; \
Print "-------------\nname\ttitle\n-------------"} \
{print $, "\ T", $;} \
END {print "-------------"} ' Employee.txt
2) Specify multiple delimited fields
Format: awk ' BEGIN {fs= ' [,:%] '} {action} Input-file
awk ' BEGIN {fs= ' [,:%] '} {print $, $ employee.txt} '
3) Output delimiter OFS
Awk-f ', ' {print $, ': ', $ employee.txt} '
Equivalent to Awk-f ', ' BEGIN {ofs= ': '} {print $, $ $} '
Employee.txt
4) Specify the separator and wrap the line RS
Specify a split character to divide one line into multiple lines input example: 103 Raj Reddy Sysadmin-104 Anand Ram Developer-105 Jane Miller Sales Manager output: John doe:ceo Jas On Smith:it manager Raj reddy:sysadmin Anand ram:developer Jane miller:sales Manager
awk ' BEGIN {fs= ' \ n '; Rs= "-\n"; Ofs= ":"} \
{print $, $ tmp.txt} '
5) Output delimiter ors
awk ' BEGIN {fs=, '; Ofs= "\ n"; ors= "\ n---\ n"} {print
$1,$2,$3} ' Employee.txt

1) in detail print and printf
Variables in print do not need to be quoted, strings need to be quoted
Example: Date | awk ' {print ' Month: ' $ $ ' \nyear: ', ' $ '
2) printf is basically consistent with C language
Escape character
-%c character%s string%d decimal integer%f floating-point type
Program Name Status version user
COMMAND STATUS VERSION USER
3) OFMT variable control output
Date | awk ' {ofmt= '%.2f "; print 1.3456,3.45} '
3. Example Application
1) Divide the following string into multiple lines, separated by:
101,john Doe:102,jason Smith:103,raj Reddy:104,anand
Ram:105,jane Miller
Implementation: awk ' begin{rs= ': "}{print $} ' Example1.txt
2) Windows and Linux file format conversion
awk ' begin{rs= ' \ n '; ors= "\ r \ n"}{print} ' README.txt >readme. WIN

2) NR built-in variable
Line number for record processing
awk '/jason/{print NR, $ A} ' Test.txt
Note: The NR variable does not reset the counter
4. FILENAME built-in variables
Used to flag the currently processed file name when working with multiple files at the same time
Attention:
1) Display file name when working with text
2) Show "-" when receiving content from pipeline input stream
5. FNR built-in variables
Similar to the NR built-in variable, but resets the counter.

Explore the order of execution of the following three environment configuration files

1,/etc/profile

Under the premise of root user login

2,/ROOT/.BASHRC (equivalent to ~/.BASHRC)

3,/root/bash_profile (equivalent to ~/.bash_profile)

The experimental results show that the order of execution is:

/etc/profile

~/.bashrc

~/.bash_profile

Log in as a normal user and find that the environment variables in/etc/profile are still loaded without executing/ROOT/.BASHRC and/root/.bash_profile in the/root directory

This note/etc/profile is in effect for the global, that is, all users are in effect.

To summarize, the order in which the environment variables load the configuration files is:

Load the Global environment variable configuration file first, and then load the private environment variable profile.

Basic format of the command
Find Pathname-options [-print-exec-ok]
The Pathname directory path.
-print output to standard output.
-exec executes this parameter on the matching file with the S H L command given in the form of the command
' Command {} \; ' Note the spaces between {} and \;
-ok and-e x e C have the same effect, except that the shell command given by the parameter is executed in a more secure mode, and a prompt is given before each command is executed to determine whether or not to execute.
3 Find other useful options
-name finds files by file name.
-perm to find files according to file permissions.
-prune using this option allows the f i n d command not to be found in the currently specified directory, and if the-depth option is used at the same time, the-prune option will be ignored by the Find command.
-user Search for files according to the owner of the file.
-group finds files according to the group to which the files belong.
-mtime-n +n Search for files according to the time the file was changed, similar to the-ctime-atime
-newer file1! File2 find change time than file F i l e 1 new but newer than file F i l e 2 old file.
-type find a file of a certain type, such as:
B-block device files.  D-Directory. C-character device file.
P-Pipeline file.   L-Symbolic link file. F-Normal file.
-size N[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth when looking for a file, first find the file in the current directory, and then look in its subdirectories.
4 Find and Xargs are similar to the-exec command, except that it is connected to the pipeline, and only some of the parameters can be transmitted at once, to avoid the hint of error reporting when large parameter files are transferred.

5 Aliases for commands

You can use the alias command to rename complex variables and parameters, such as:

Alias showipt= ' IPTABLES-NL '

Alias application: It can be used in a number of command parameters more, and often used in the place, the variable and parameter alias substitution.

Alias findtxt= ' find/root/-type f-name *.txt '

Use the Stty command plus parameter [-]echo to Output "no" echo

Cal [-SMJY13] [[[Day] month] year]

[Optional between] If there is only one number, then the year, if there are two digits, indicates the month and year, if there are three, then the day, month, and year

Question: What should I do if I want to format the time: 2014-08-21 21:25:00?

Use the following command to achieve your goal:

Date "+%y-%m-%d%h:%m:%s"

Question: How do I set the system time?

Date-s "2014-08-21 21:28:00"

Question: How do I calculate timestamps?

Date-d "2014-08-21 21:30:00" +%s

Question: Why do you want to calculate timestamps?

In order to calculate the time difference.

Question: How do I draw a date based on a timestamp?

Take time stamp 1408681800 as an example

Date-d @1408681800 "+%y-%m-%d%h:%m:%s"

Locate and find

The locate command is found based on the Whatis command database.

Find files named Flood.c in the home directory

find/home/-name FLOOD.C

The find command is searched recursively in the home directory, and it will find the file that matches the file name rule in the home directory.

Find files in the home directory that are not case-sensitive and file names FLOOD.C.

Find/home-iname FLOOD.C

-i:case-insensitive not sensitive

Based on user lookup:

find/home/sh/-user Guozi

Based on group lookup:

find/home/sh/-group Guest

1. Find files with user Guozi and group guest

Find/home/sh-user Guozi-group Guest

Find/home/sh-user Guozi [-A]-group guest

2, find the user is Guozi, or the group is the guest file

Find/home/sh-user Guozi-o-group Guest

3, find the user is Guozi, or the group is guest, and the file name is Guozi.guest.txt file

Find/home/sh-user guozi-o guest-a-name guozi.guest.txt

4. Find files with file name not Guozi.guozi.txt

Find/home/sh! -name Guozi.guozi.txt

Shell Learning Notes---transcription

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.