The function of the shell
- Automatic completion
yum -y install bash-completion
Copy the code
- View History Command
history View all history commands
history -n # check the last n commands
history -d n Delete n command record! nRun the NTH command! -nRun the NTH command
Copy the code
- The alias feature
alias h20 = 'head -20' Create an alias, then use h20 instead
unalias h20 # delete alias
Make the alias permanent
vim ~/.bashrc
alias h ='cd /' # add alias
# and quit
source ~/.bashrc # refresh file
Copy the code
Bash initialization
Loading of environment variables
-
/etc/profile:
-
/etc/bashrc:
-
~/.profile:
-
~/.bash_login
-
~/.bash_profile
-
~/.bashrc
-
~/.bash_logout
Load order
For GUI login: 1 =>3
After GUI login: Open the terminal: 2=> 5
2 => 1=>5
Su to switch the user: 2=>1=>5
Common commands
command & Let the job run in the background
ctrl + z Switch the current job to background
jobs # Check background job status
fg %n Change the background running job n to the foreground
bg %n Let the specified job n run in the background
Copy the code
redirect
File descriptor
0: standard input file
1: standard output
2: standard error output file
Output redirection:
command >file Write the result to the file as an overwrite (default: 1)
command >>file Write the result to the file appending
command 2>>file # append command error messages to files
command >file 2>&1 Write both the correct result and the error message to the same file
command >>file 2>&1 # Append correct and incorrect results to the same file
Copy the code
The standard standard is the set of right and wrong,
&1 stands for standard correct channel,
The &2 stands for standard error channel,
/dev/null is equivalent to the recycle bin in Linux. All unnecessary information can be redirected to this directory. The garbage can not be recovered.
Input redirection:
command <file Take the contents of file as input to command
command <<END # Reads data from standard input until you encounter the delimiter ENDBefore stopping (delimiter can be customized)Case #
while read str
do
echo $str
done < readme.txt # implement readme. TXT statements line by line.
mysqladmin -uroot -p 123 test <test.sql Write the contents of the test.sql file to test
Copy the code
tee
It is mainly used to display the previous results to the front end, and can also be written to a file
cat /etc/profile | tee test.txt /etc/profile/test.txt/test.txt /etc/profile/test.txt
Copy the code
Command sequence
The realization of logic, &&, | |
&& : Indicates that the statement following the symbol is executed only after the command before the symbol is successfully executed
systemctl mysqld start && mysql -uroot -p xxx Mysql > log in to mysql
Copy the code
| | : symbol in front of the command executed success will not sign the back of the statement
sl || yum install sl If the sl command is not used successfully, the sl command will be installed
Copy the code
; No logical judgment, just connecting two commands
Normal usage in programming
Wildcards (slightly different from re)
* :# match 0 or more arbitrary characters (remember mysql fuzzy query %?
ls /etc/*.conf # match all files that end in.conf below /etc/conf? :Matches an arbitrary characterls /etc/??? .conf# match all files under /etc/conf that end in. Conf and have a filename length of 3
[list]: Matches any single character in the list
ls /etc/[list].conf # match any file under /etc/conf that ends with a.conf file name in the list
[!list]: Matches all but any single character in list
ls /etc/[!list].conf # match any file under /etc/conf that ends in. Conf and is not in the list
[c1-c2]: # Matches any single character between C1 and c2
ls /etc/[c1-c2].conf # match any file under /etc/conf that ends in.conf and has a file name in c1-c2{string1,string2,...... } :# match string1, string... Any character in the.
ls /etc/{"1"."2"."3"}.conf # match any of the files under /etc/conf that end with.conf and are not in {"1","2","3"}
Copy the code
Shell specification
#1. The beginning of the interpreter must be executed through '#! ` specified
cat /etc/Shells # Default supported interpreters to view:
/bin/sh
/bin/bash
/user/bin/sh
/user/bin/bash
#2. Use of comments
`# ': # single-line comment` : < <! Content! ` :# multiline comments
In fact, it is similar to Java specification, which is not mentioned here.
Copy the code
Script Execution Check
sh -n: # do not execute script, only query syntax problem
sh -v: Output the contents of the script to the screen before executing the script
sh -x: Print the content of the script used to the screen
Copy the code
Script execution mode:
sh == bash # No permissions required. / :Add permission
/usr/local/test.sh: This can also be executed as a path, which also requires permissions:
Copy the code
read
- Read the variable name
- Read -p “Prompt” variable name
- Read -t n -p” Prompt “The variable name #-t indicates the number of seconds in which a timeout occurs without input
- Read-n N Variable name # indicates that n input characters are read
Reference variables
"": indicates a weak reference. The value of the reference will be the value of the variabletest="hello shell"
echo "${test}"The result: Hello shell' ': indicates a strong reference. The value of the reference is a string in the middle of the valuetest="hello shell"
echo '${test}'The result is:${test}
Copy the code
Variable operation
Integer arithmetic
1. Use expr expr m + n# add
expr m - n # reducing
expr m / n # in addition to
expr m \* n # take2. Use $((formula)) or $[formula]echo $(($a + $b))
echo $[$a + $b] 3. Uselet
let sum=2+3; echo $sum
let i++; echo $i
Copy the code
Decimal arithmetic
echo "Talent" |bc # 0
echo "2 * 4" |bc # 8
echo "scale=2; 6/4. "" #1.50 Specifies exact calculation to two decimal places ()
awk 'BEGIN(print 1/2)' # 0.5
echo "Print 5.0/2 | python" # 2
Copy the code
-
The number of decimal points in a product defaults to the largest number of decimal points in the multiplier (no scale argument is specified).
-
If the scale parameter is not specified in the division, the result has no decimal places, and the decimal number is displayed entirely according to the scale value. If the decimal number of the calculated result is less than the scale value, 0 will be added at the end
Greedy matching and non-greedy matching
A % is a non-greedy match, that is, the shortest match. % from right to left for non-greedy matching. Matches the shortest string of specified characters to the right of the % delimiter, and then deletes it.
v=www.baidu.com
echo ${v%.*} #www.baidu
Copy the code
Two %% is greedy match
v=www.baidu.com
echo ${v%%.*} # WWW. Is the longest string of delimiters
Copy the code
# matches from the left in reverse order of %
v=www.baidu.com
echo ${v#*.} #baidu.com
echo ${v##*.} #com
Copy the code
String interception can also be done like this:
${value: offset:length} The specified length is truncated from offset
Copy the code
Variable substitution
Using the /
url="www.baidu.com"
echo ${url/baidu/sougou} #www.sougou.com(only one will be replaced, which is a non-greedy match, i.e. the shortest match)
echo ${url//w/W} #WWW.baidu.com(multiple matches, greedy match)
Copy the code
${value:-word}: # Return the contents of Word if value is undefined or empty, otherwise return the variable value
${value:+word}: Word is used only when value has been assigned
${value:=word}: # if value is undefined or empty, return the value of Word and assign Word to value, otherwise return the value of the variable
Copy the code