1. An overview of the
1.1 Shell parsers provided by Linux include:
[root@hadoop1 /]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
Copy the code
1.2 Relationship between bash and sh
[root @ hadoop1 /] # ll/bin / | grep bash - rwxr-xr-x mto 1 root root on August 8, 2019, 964600 bash LRWXRWXRWX 1 root root 4 April 27, 2020 sh -> bashCopy the code
1.3 Centos’s default parser is bash
[root@hadoop1 /]# echo $SHELL
/bin/bash
Copy the code
2. Getting started with scripts
2.1 Script Format
Script to #! Start /bin/bash (specify parser)
#! /bin/bashXXXXXXXXXX Script contentCopy the code
2.2 the helloworld
#! /bin/bash
echo 'helloworld'
Copy the code
2.3 Executing Scripts
Bash execute
[root@hadoop1 shell]# bash hello.sh
helloworld
Copy the code
Sh to perform
[root@hadoop1 shell]# sh hello.sh
helloworld
Copy the code
Execute directly (need execute permission)
[root@hadoop1 shell]# chmod 744 hello.sh
[root@hadoop1 shell]# ./hello.sh
helloworld
Copy the code
3. The variable
3.1 Predefined variables
1. Common system variables
HOME, HOME, HOME, PWD, SHELL, SHELL, SHELL, USER, etc
2. Case practice
[root@hadoop1 shell]# echo $shell /bin/bash set [root@hadoop1 shell]# set BASH=/usr/bin/bash BASH_ALIASES=()Copy the code
3.2 User-defined variables
Basic grammar
1. Define variable: variable = value 2. Undo variable: unset variable 3. Declare static variables: readonly variables (note: cannot be unset) 4. Promoted to global environment variables: export variable name (available for other Shell programs)Copy the code
2. Variable definition rules
1. The variable name can contain letters, digits, and underscores (_), but cannot start with a digit. It is recommended to capitalize environment variable names. 3. In bash, the default variable type is string, so you cannot directly perform numerical operation. 4. If the variable value contains Spaces, enclose it with double or single quotation marks.Copy the code
[root@hadoop1 shell]# aaa=123 [root@hadoop1 shell]# echo $aaa 123 [root@hadoop1 shell]# unset aaa [root@hadoop1 shell]# echo $aaa [root@hadoop1 shell]# bbb=234 [root@hadoop1 shell]# readonly bbb [root@hadoop1 shell]# echo $bbb 234 [root@hadoop1 shell]# unset bash: unset: BBB Variable [root@hadoop1 shell]# aa=" large data "[root@hadoop1 shell]# export aa [root@hadoop1 shell]# echo aa aaCopy the code
3.3 Special Variables
1. $n
Function description: N for the digital, 0 on behalf of the name of the script, 0 on behalf of the name of the script, 0 on behalf of the name of the script, 1-9 on behalf of the first to the ninth parameters, more than ten parameters, need to use braces contains more than ten parameters, such as 9 on behalf of the first to the ninth parameters, more than ten parameters, need to use braces contains more than ten parameters, such as 9 on behalf of the first to the ninth parameters, For more than 10 parameters, use curly braces, such as {10}.
#! /bin/bash
echo $0 The $1 $2
Copy the code
[root@hadoop1 shell]# sh bianliang.sh 1 2
bianliang.sh 1 2
Copy the code
2. $#
Function Description: Get the number of all input parameters, usually used in a loop
#! /bin/bash
echo $#
Copy the code
[root@hadoop1 shell]# sh bianliang.sh 1 2
2
Copy the code
3.
@
- ∗ (The ∗ variable represents all of the parameters on the command line, * (the ∗ variable represents all of the parameters on the command line, * treats all of the parameters as a whole)
- @ (this variable also represents all the arguments in the command line, but @ treats each argument differently)
#! /bin/bash echo $* echo $@Copy the code
The difference can be found in recycling
[root@hadoop1 shell]# sh bianliang.sh 1 2
1 2
1 2
Copy the code
4. $?
Function: Return status of the last command executed. If the value of this variable is 0, prove that the previous command executed correctly; If the value of this variable is non-zero (which number is up to the command itself), then the previous command was executed incorrectly
[root@hadoop1 shell]# ll total amount 8-rw-r --r-- 1 root root 53 April 9 19:19 bianliang. Sh-rwxr --r-- 1 root root 31 April 9 18:39 hello.sh [root@hadoop1 shell]# echo $? 0 [root@hadoop1 shell]# LLLLL bash: LLLLL: no command [root@hadoop1 shell]# echo $? 127Copy the code
4. Operator
- $((expression))
- $[expression]
[root@hadoop1 shell]# echo "$((2+3))"
5
[root@hadoop1 shell]# echo "$[3+3]"
6
Copy the code
5. Conditional judgment
-
The basic grammar
- test condition
- [condition] [condition] [condition]
-
Judge conditions
A comparison between two integers
symbol | role |
---|---|
= = | String comparison |
-lt | Less than |
-le | Less than or equal to |
-eq | Is equal to theta equal. |
-gt | Greater than |
-ge | Greater than or equal to (greater than equal) |
-ne | Is Not equal to |
Judge by file permission
symbol | role |
---|---|
-r | Have read permission (read) |
-w | Have write permission (write) |
-x | Have permission to execute (execute) |
Judge by file type
symbol | role |
---|---|
-f | The file exists and is a regular file. |
-e | Existence of documents |
-d | The file exists and is a directory |
[root@hadoop1 shell]# test 1 -lt 2 [root@hadoop1 shell]# echo $? 0 [root@hadoop1 shell]# test 1 -gt 2 [root@hadoop1 shell]# echo $? 1 [root@hadoop1 shell]# ll total amount 8-rw-r --r-- 1 root root 53 4月 9 19:19 bianliang. Sh-rwxr --r-- 1 root root 31 4月 9 18:39 hello.sh [root@hadoop1 shell]# [ -x bianliang.sh ] [root@hadoop1 shell]# echo $? 1 [root@hadoop1 shell]# [ -x hello.sh ] [root@hadoop1 shell]# echo $? 0 [root@hadoop1 shell]# [ -f hello.sh ] [root@hadoop1 shell]# echo $? 0 [root@hadoop1 shell]# [ -d hello.sh ] [root@hadoop1 shell]# echo $? 1Copy the code
6. Process control
6.1 if judgment
Basic grammar:
if [ condition ]; then
# if body
elif [ condition ]; then
# else if body
else
# else body
fi
Copy the code
Steps:
#! /bin/bash
if [ The $1 -eq 1 ]; then
echo "The $1= 1"
elif [ The $1 -lt 1 ]; then
echo "The $1"1"
else
echo "The $1> 1. ""
fi
Copy the code
Execution result:
[root@hadoop1 shell]# sh if.sh 1
1=1
[root@hadoop1 shell]# sh if.sh 2
2>1
[root@hadoop1 shell]# sh if.sh 0
0<1
Copy the code
6.2 a case statement
Basic grammar:
case "${item}" in
1)
echo "item = 1";; 2 | 3)echo "item = 2 or item = 3"
;;
*)
echo "default (none of above)"
;;
esac
Copy the code
Steps:
#! /bin/bash
case "The ${1}" in
1)
echo "param = 1";; 2 | 3)echo "param = 2 or param = 3"
;;
*)
echo "default (none of above)"
;;
esac
Copy the code
Execution result:
[root@hadoop1 shell]# sh case.sh 1
param = 1
[root@hadoop1 shell]# sh case.sh 2
param = 2 or param = 3
[root@hadoop1 shell]# sh case.sh 3
param = 2 or param = 3
[root@hadoop1 shell]# sh case.sh 4
default (none of above)
Copy the code
6.3 a for loop
Basic grammar:
for((i=0; i<n; i++));do
echo "${i}"
done
for item in{a.. z};do
echo "${item}"
done
Copy the code
Steps:
#! /bin/bash
for((i=0; i<The $1; i++));do
echo "${i}"
done
for item in{1, 2, 3};do
echo "${item}"
done
Copy the code
Execution result:
[root@hadoop1 shell]# sh for.sh 3
0
1
2
1
2
3
Copy the code
6.4 the while loop
Basic grammar:
while [ condition ]; do
# body
done
Copy the code
Steps:
#! /bin/bash
i=0
while [ $i -lt The $1 ]; do
# body
echo "${i}"
i=$[$i+ 1]done
Copy the code
Execution result:
[root@hadoop1 shell]# sh while.sh 2
0
1
Copy the code
7. Read The console input
Basic grammar:
- Read (option)(parameter)
Options:
- -p: indicates the prompt for reading values.
- -t: specifies the waiting time (seconds) for reading the value.
Parameters:
- Variable: The name of the variable that specifies the read value
[root@hadoop1 shell]# echo $name root [root@hadoop1 shell]# echo $name root [root@hadoop1 shell]# echo $name rootCopy the code
8 function
8.1 System Functions
basename
Basic grammar: Basename [string/pathname] [suffix] The basename command deletes all prefixes including the last character (‘/’) and displays options: Suffix is a suffix, and if suffix is specified, basename will remove the suffix in pathname or string.
[root@hadoop1 shell]# basename /opt/1.txt
1.txt
[root@hadoop1 shell]# basename /opt/test/
test
Copy the code
dirname
Basic syntax: dirname Absolute file path Function Description: remove the file name (non-directory part) from the given file name containing the absolute path, and then return the remaining path (directory part)
[root@hadoop1 shell]# dirname /opt/1.txt
/opt
[root@hadoop1 shell]# dirname /opt/test/
/opt
Copy the code
8.2 Custom Functions
Basic grammar:
[ function ] funname[()]{
Action;
[returnint; ] }function funname(){
Action;
return int;
}
funname(){
Action;
return int;
}
Copy the code
Code:
#! /bin/bash function funname(){echo "2"} funnameCopy the code
Execution result:
[root@hadoop1 shell]# sh function.sh "2"Copy the code
9. The Shell tool
9.1 the cut
Basic usage cut [option argument] filename Description: The default delimiter is a TAB
Options Parameter Description
Option parameters | function |
---|---|
-f | Column number, which column to extract |
-d | Delimiter that separates columns by the specified delimiter |
-c | Specifies a specific character |
-f 1,7 indicates the first and seventh columns [root@hadoop1 test]# cut /etc/passwd -d ":" -f 1,7 root:/bin/bash bin:/sbin/nologin daemon:/sbin/nologin - f 3 - after the third column are [root @ hadoop1 test] # cut/etc/passwd ":" d - 3 - f 0-0: root: / root: / bin/bash 1-1: bin: / bin: / sbin/nologin 2:2:daemon:/sbin:/sbin/nologiCopy the code
9.2 the awk
Basic usage awk [Option parameter] ‘Pattern1 {action1} Pattern2 {action2}… ‘Filename pattern: Indicates what AWK looks for in the data, the matching pattern. Action: A series of commands that are executed when a match is found
Options Parameter Description
Option parameters | function |
---|---|
-F | Specifies the input file separator |
-v | Assign a user-defined variable |
Awk’s built-in variables
variable | instructions |
---|---|
FILENAME | The file name |
NR | Number of records read (lines) |
NF | Number of fields in the browsing record (number of columns after cutting) |
[root@hadoop1 test]# awk -f ":" '{print $1" "$3}' /etc/passwd root 0 bin 1 $3+1 [root@hadoop1 test]# awk -f ":" '{print $1" "$3+1}' /etc/passwd root 1 bin 2 -v I =2 $3+ I [root@hadoop1 test]# awk -v I =2 -f ":" '{print $1" [root@hadoop1 test]# awk -v I =2 -f ":" '/ etc/passwd root 2 bin 3 /^r/{print $1" " [root@hadoop1 test]# awk -v I =2 -f ":" 'BEGIN{print "BEGIN "} {print $1" "$3+ I} END{print "END "}' /etc/passwd begin root 2 bin 3 END "NR " "NF}' /etc/passwd /etc/passwd 1 7 /etc/passwd 2 7Copy the code
9.3 the sort
Basic syntax sort(option)(argument)
options | instructions |
---|---|
-n | Sort by size |
-r | In reverse order |
-t | Sets the delimiter character used when sorting |
-k | Specifies the columns to be sorted |
: Divided according to the third column inverted [root @ hadoop1 test] # sort -t: "" three - NRK/etc/passwd test123: x: 1003-1001: : / home/test123: / bin/bash test2:x:1002:1002::/home/test2:/bin/bash test1:x:1001:1001::/home/test1:/bin/bashCopy the code
10. Getting started with regular expressions
A regular expression uses a single string to describe and match a series of strings that match a syntactic rule. In many text editors, regular expressions are used to retrieve and replace text that conforms to a pattern. In Linux, grep, sed, awk, and other commands support pattern matching using regular expressions.
10.1 General Matching
A string does not contain special characters of regular expression match itself, for example: [atguigu @ hadoop102 datas] $cat/etc/passwd | grep atguigu will match all contain atguigu line
10.2 Common Special Characters
- Special characters: ^
^ match the beginning of a line, for example: [root @ hadoop1 shell] # cat/etc/passwd | grep ^ ro root: x: 0-0: root: / root: / bin/bash will match out all the lines begin with roCopy the code
- Special characters: $
$matches the end of a line, For example [root @ hadoop1 shell] # cat/etc/passwd | grep t $t root: x: 0-0: root: / root: / bin/bash Shutdown: x: lost: shutdown: / sbin, / sbin/shutdown will match all to t at the end of the line of thinking: ^ $matches?Copy the code
- Special characters:.
Matching an arbitrary character, such as [root @ hadoop1 shell]. # cat for sh | grep i. + for ((I = 0; i<$1; i++)); Do matches all rows that contain i++ and so onCopy the code
- Special characters: *
* don't use alone, he and the first character on the left, on the means to match one character 0 or more times, for example [root @ hadoop1 shell]. # cat for sh | grep for I * o ((I = 0; i<$1; i++)); Do echo "${I}" done to match IO, iooo, ioooooo, etc.Copy the code
- Special characters: []
[] means to match one character at a time, within the scope of a certain, for example [6] -- -- -- -- -- - match 6 or 8 [a-z] -- -- -- -- -- - match between an a to z characters [a-z] * -- -- -- -- - string match any letters (a, c, e - f] - match between a - c or e - f any characterCopy the code
- Special characters: \
\ indicates an escape and is not used alone. Because all special characters have their own particular matching pattern, we run into difficulties when we want to match a particular character itself (for example, I want to find all lines that contain '$'). At this point we use escape characters with special characters to represent the special character itself, for exampleCopy the code
Note: A direct matching character, if you escape it and you put a single quote on it, will match all lines that contain a and a and b.