Shell script (1) Basic knowledge and basic knowledge of shell scripts
Shell
I. Introduction
1. Role: interaction with the kernel
2. Language type: interpreted language, used for Kernel Interpretation
3. The first line of content in the script:
#! /Bin/bash: This is the first line of the shell script. It indicates that the interpreter bash is used to explain the following content.
#! /Bin/sh this can also be the first line of the shell script, indicating to use the sh interpreter to explain the following content
#! /Usr/bin/env bash env indicates that the relative path is used and the location of the bash interpreter is automatically located. It can solve the problem that bash of different versions may not always be in the/usr/bin/bash path.
4. to make the script easier for others to read, the author, creation time, email address, version, description, and other content are usually added at the beginning. You can add the information to the script by setting the shortcut key as follows:
Vim/etc/vimrc
Map <F4> ms: call WESTOS () <cr>'s
Function WESTOS ()
Call append (0, "######################################")
Call append (1, "# Author: vaon #")
Call append (2, "# Create Date:". strftime ("% Y-% m-% d ")."#")
Call append (3, "# Email: vaoner@163.com #")
Call append (4, "# Version: 1.0 #")
Call append (5, "# Description :#")
Call append (6, "######################################")
Call append (7 ,"")
Call append (8 ,"#! /Usr/bin/env bash ")
Endfunction
# Map indicates shadow --> call (CALL) The WESTOS function when pressing F4.
#0 indicates the first line, and 1 indicates the second line...
# The Yellow strftime is a built-in function, indicating that the current time is displayed. It is a separator, indicating that this function does not affect the content of the other two segments.
# The following describes how to set a function:
Fucton function name ()
Content
Endfunction
Create a script and use the F4. The following content is automatically added at the beginning of the script:
Ii. Basic Knowledge
1. Variable declaration $ {a} B
Set a = 1
Echo $ a will show 1,
But echo $ AB won't show 1b, because the system regards AB as a variable.
You can echo $ {a} B and 1b will appear.
2. Escape, weak reference, and strong reference
1) \ escape
Echo ### does not show results, but with echo \#\#\# added, it will appear ###
2) "" indicates a weak reference and can be output ###
3) ''indicates a strong reference and can be output ###
4) differences between strong references and weak references
For example, $! \ ''These strong references print them as common content to the screen.
Weak references treat them as system variables and execute the content in the variables.
For example, in a weak reference, 'hostname' indicates that the hostname command is executed.
In a strong reference, the 'hostname' content is output.
3. diff, patch
1) diff
[1 ].
[Root @ server mnt] # cat file1
Abcd
1234
[Root @ server mnt] # cat file2
Abcd
[Root @ server mnt] # diff file1 file2
2d1
<1234
# It indicates that file1 deletes 1234, which is the same as file2.
[2]. Compare Directories
Diff-r directory 1 directory 2
2) patch
Create a hello file:
Vim hello
Westos
Create the hello. new file:
Vim hello. new
Westos
456
Install the automatic update Toolkit:
Yum install patch-y
Compare the hello. new file with the hello file and make the extra content into the hello. path patch package:
Diff-u hello. new> hello. path
Use the path patch package to update the hello file:
Patch hello. path
View the hello file, which has been updated like hello. new:
Cat hello
Westos
456
Note: If the-B parameter is added after the patch, the original file is backed up as hello. orig at the same time.
4. grep
First, create a file:
[Root @ shell mnt] # vim/mnt/passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Hello
Mail: x: 8: 12: mail:/var/spool/mail:/sbin/nologin
Games: x: 12: 100: games:/usr/games:/sbin/nologin
Test: ROOT: test
Test: root
Test: root: test
Test: roottest: test
1) Find the row containing the root sub-segment
[Root @ shell mnt] # grep root/mnt/passwd
Root: x: 0: 0: root:/bin/bash
Test: root
Test: root: test
Test: roottest: test
2) Search for rows containing the root sub-segment, case insensitive
[Root @ shell mnt] # grep-I root/mnt/passwd
Root: x: 0: 0: root:/bin/bash
Test: ROOT: test
Test: root
Test: root: test
Test: roottest: test
3) as long as
[Root @ shell mnt] # grep-I ^ root/mnt/passwd
Root: x: 0: 0: root:/bin/bash
4) as long as the root end
[Root @ shell mnt] # grep-I root $/mnt/passwd
Test: root
5) do not start or end with root. -E indicates that the following two conditions are filtered at the same time, and-v indicates that the rows are not filtered.
[Root @ shell mnt] # grep-I root/mnt/passwd | grep-I-E "^ root | root $"-v
Test: ROOT: test
Test: root: test
Test: roottest: test
6) indicates that as long as the row contains only the root sub-segment and there are no letters before and after the root, such as the roottest line.
[Root @ shell mnt] # grep-I "\ <root \>"/mnt/passwd | grep-I-E "^ root | root $"-v
Test: ROOT: test
Test: root: test
Problem:
View All users who can log on to the system
Answer:
[Root @ shell mnt] # grep "\ <bash \>"/etc/passwd
Root: x: 0: 0: root:/bin/bash
It can be seen that only root users can log on
7) grep-n indicates displaying the row number of the searched string
[Root @ shell mnt] # grep-n hello/mnt/passwd
4: hello
8) gep-n2 indicates that the searched row and its two rows above and below are displayed, and their respective row numbers in the file are displayed.
Note: if there is a space between-n and 2, the file name will be displayed at the beginning of each line in addition to the content shown above.
[Root @ shell mnt] # grep-n2 hello/mnt/passwd
2-bin: x: 1: 1: bin:/sbin/nologin
3-daemon: x: 2: 2: daemon:/sbin/nologin
4: hello
5-mail: x: 8: 12: mail:/var/spool/mail:/sbin/nologin
6-games: x: 12: 100: games:/usr/games:/sbin/nologin
9) grep-B2 indicates that the searched rows and the content of the last two rows are displayed.
[Root @ shell mnt] # grep-B2 hello/mnt/passwd
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Hello
10) grep-A2 indicates the next two rows.
[Root @ shell mnt] # grep-A2 hello/mnt/passwd
Hello
Mail: x: 8: 12: mail:/var/spool/mail:/sbin/nologin
Games: x: 12: 100: games:/usr/games:/sbin/nologin
11) searches for files with root characters in the/mnt directory.
[Root @ shell mnt] # grep-r hello/mnt/
/Mnt/passwd: hello
5. cut
Usage: specifies the number of columns of the cut-d separator-f.
-C: specified character
-D specifies the delimiter
-F specifies the column number
Cut-d:-f 2/mnt/psswd # Use the separator: to display the second column of the passwd file
Cut-d:-f 7th/mnt/psswd #, which indicates the second and seventh columns. If it is-, it indicates the 2 to columns.
Cut-c 3/mnt/passwd # Show the characters in the third column, without Separators
Cut-c 1, 3/mnt/passwd # Show the characters in the first and third columns without Separators
Question:
Display the ip address of the eth0 Nic
Answer:
[Root @ shell mnt] # ifconfig eth0 | grep "\ <inet \>" | cut-d ""-f 10
172.25.254.1
6. awk
-V indicates to assign content to the variable:
[Root @ shell mnt] # name = hello
[Root @ shell mnt] # awk-F:-v name = $ name' BEGIN {print name} {print $1} END {print "end"} '/mnt/passwd
Hello
Root
Bin
Daemon
Hello
Mail
Games
Test
Test
Test
Test
End
7. sed
-I # This parameter indicates that the file is written.
Sed's/old/new/G' passwd # Replace all old characters in the passwd file with new
Sed's/old/new/G' passwd # Replace all old characters in the passwd file with new and write them to the passwd file
Sed '2, 5S/old/new/G' passwd # Replace 2 to 5 rows
Sed '/adm/,/TPD/s/old/new/G' passwd # replace old with new in the content from the adm character to the content of the EM character
Sed-e's/old/new/G'-e's/old1/new1/G' passwd # replace old with new and replace old1 with new1
Sed 5d passwd # Delete 5th rows
Sed 5 p passwd # copy 5th rows
Sed-n 5 p passwd # show only 5th rows
-P # indicates that the sed action is directly written in a file.-f filename indicates that the sed action in filename can be executed.
Vim file
S/sbin/westos/g
S/nologin/linux/g
Sed-f file passwd
Question:
Change httpd service port to 8080
Answer:
############################
# Author: vaon #
# Create Date: 2017-12-16 #
# Email: vaoner@163.com #
############################
#! /Bin/bash
PORT = 'grep ^ Listen/etc/httpd/conf/httpd. conf'
Sed "s/$ PORT/Listen $1/g"-I/etc/httpd/conf/httpd. conf
Systemctl restart httpd
8. Set Variables
1) Temporary settings (Environment level)
It takes effect in the current environment, the current environment is disabled, and the variable is invalid.
Let's take a look at an example:
[Root @ shell mnt] # a = 1
[Root @ shell mnt] # echo $
1
[Root @ shell mnt] # vim test. sh
#! /Bin/bash
Echo $
[Root @ shell mnt] # sh test. sh
[Root @ shell mnt] #
# Because a = 1 is the value assigned to the current shell, when the script is executed, the script uses a sub-shell, which is not set to a = 1 in this sub-shell, the result is empty.
[Root @ shell mnt] # export a = 1
[Root @ shell mnt] # sh test. sh
1
# You can temporarily set a = 1 to all shells. Once logged out, this temporary setting will be lost.
2) permanent settings
[1]. System Level
Effective for all users
[Root @ shell mnt] # vim/etc/profile
Export a = 1
[Root @ shell mnt] # source/etc/profile
[Root @ shell mnt] # logout
Connection to 172.25.254.1 closed.
[Root @ station Desktop] # ssh root@172.25.254.1
Root@172.25.254.1's password:
Last login: Sat Dec 16 09:12:16 2017 from 172.25.254.10
[Root @ shell ~] # Cd/mnt/
[Root @ shell mnt] # sh test. sh
1
# In this way, all the shells can use the = 1 variable. After logging out of the system, a = 1 still exists.
[2]. user-level
It takes effect only for users who have configured it, and cannot be used by other users.
Vim/root/. bash_profile
Export a = 1
Source/root/. bash_profile
Now a = 1 takes effect only for root users
9. Add the script to the system command.
[Root @ shell mnt] # vim/etc/profile
Export PATH = $ PATH:/mnt
Note: system-level or user-level settings are also available here. System-level settings are used here. If only root is allowed, you can set the/root/. bash_profile file.
[Root @ shell mnt] # source/etc/profile
[Root @ shell mnt] # chmod + x test. sh
Then exit the/mnt directory and run the test. sh script as a system command. You can also find that:
[Root @ shell mnt] # cd
[Root @ shell ~] # Test. sh
1
10. Command alias
1) use aliases
[Root @ shell ~] # Vim/etc/bashrc
Alias kan = 'cat' # indicates that the 'cat' command is equivalent to the 'cat' command.
[Root @ shell ~] # Source/etc/bashrc
View the alias list:
[Root @ shell ~] # Alias
...
Alias kan = 'cat'
...
Executing the word kan is equivalent to executing the cat command.
[Root @ shell ~] # Kan/mnt/test. sh
######################################
# Author: vaon #
# Create Date: 2017-12-16 #
# Email: vaoner@163.com #
######################################
#! /Bin/bash
Echo $
2) delete an alias
Delete alias kan = 'cat' in/etc/bashrc'
Then, delete the kan information:
Unalias kan
Now, check the alias list. There is no information about kan in it.
11. * difference between @ and @
Vim test. sh
#! /Bin/bash
Echo '$1 Is' $1
Echo '$2 Is' $2
Echo '$3 Is' $3
Echo '$ * Is' $ *
Echo '$ # Is' $ #
Echo '$ @ Is' $ @
[Root @ shell mnt] # sh test. sh 1 2 3
$1 Is 1 # display 1st characters
$2 Is 2 # display 1st characters
$3 Is 3 # display 1st characters
$ * Is 1 2 3 # Show All characters
$ # Is 3 # display the last character
$ @ Is 1 2 3 # Show All characters
* And @ indicate all content, but in fact all content is included in. @ Is that every data is enclosed by quotation marks.
$ * "1 2 3"
& @ "1" "2" "3"
12. define functions in shell scripts
READ ()
{
Echo hello word
}
13. Variable comparison
1) test
2) []
[Root @ shell mnt] # a = 1
[Root @ shell mnt] # B = 1
[Root @ shell mnt] # test "$ a" = "$ B" & echo yes | echo no
Yes
[Root @ shell mnt] # ["$ a" = "$ B"] & echo yes | echo no
Yes
3) [condition 1-a condition 2] indicates that both conditions 1 and 2 meet
4) [condition 1-o condition 2] indicates that either of the conditions 1 and 2 meet
5) [condition 1-eq condition 2] equals
6) [condition 1-ne condition 2] is not equal
7) [condition 1-gt condition 2] greater
8) [condition 1-ge condition 2] greater than or equal
9) [condition 1-lt condition 2] is smaller
10) [condition 1-le condition 2] less than or equal
[Root @ shell mnt] # ["$ a"-eq "$ B"] & echo yes | echo no
Yes
11) [-n] variable content is not empty
12) the content of the [-z] variable is null.
[Root @ shell mnt] # a = 8
[Root @ shell mnt] # [-n "$ a"] & echo yes | echo no
Yes
[Root @ shell mnt] # [-z "$ a"] & echo yes | echo no
No
13) [-B] whether it is a block Device File
14) [-c] whether it is a character Device File
[Root @ shell mnt] # [-B/dev/sda] & echo yes | echo no
Yes
[Root @ shell mnt] # [-c/dev/vfio] & echo yes | echo no
Yes
15) [-f] whether it is a regular file
16) whether [-S] is a socket File
17) [-L] whether it is a linked file
18) [-d] whether it is a directory
19) [-e] whether the file exists
20) [-r] whether it is readable
21) [-w] is writable?
22) [-x] Is it executable?
18) [filename1-nt filename2] True if filename1 is newer than filename2
19) [filename1-ot filename2] True if filename1 is earlier than filename2
[Root @ shell mnt] # touch file
[Root @ shell mnt] # [/mnt/check_ip-ot file] & echo yes | echo no
Yes
Question:
Display input file types
Answer:
#! /Bin/bash
[-E $1] & (
[-F $1] & echo "$1 Is File"
[-D $1] & echo "$1 Is Directory"
[-B $1] & echo "$1 Is Block File"
[-L $1] & echo "$1 Is Link"
[-S $1] & echo "$1 Is Socket"
)
14. Arithmetic Operations
15. Statements
Case statement:
Concurrent conditions are matched and all conditions are configured at the same time, which is more efficient than if statements.
If statements are row-by-row matched, the first line does not match and then matches the next line, which is inefficient.
Exit to exit the script. Add the number 1 | 0 to indicate that $? The value is 1 | 0.
Break exit Loop