Shell, get started

Time:2020-2-23

Introduction

Shell is a very useful tool in Linux system. By using shell, we can improve the working efficiency of Linux system.

Shell learning

The code is all here: https://github.com/xiang2017/shell_study

variable

#!/bin/bash
Variables of variables
Echo "01" variable. Sh "

Variables of variables定义与赋值,等号两边不能用空格分开
name=hahahaha
echo $name
Echo can also use {} output: ${name}

#Some special variables
test_func() {
  echo "function name is $FUNCNAME"
}
test_func

echo $HOSTNAME
echo $HOSTTYPE
echo $MATCHTYPE
echo $LANG
echo $PWD
# echo $PATH
unset name
echo $name

#Read only variable
readonly R0=100
R0=200
Echo $? ා last instruction is wrong, so $? Is not 0

Variables of variables的作用域
Variables of variables的作用域又叫“命名空间”,相同名的变量可以在不同命名空间定义。
#In Linux system, shells with different process IDs default to different namespaces
VAR_01=100
function update() {
  #The same variable is accessed inside and outside the function
  VAR_01=200
}
update
Echo var01: $var 01

function update02() {
  #You can use the local keyword to declare local variables within a function
  local VAR_01=300
}
update02
Echo "local variables declared by local do not affect global variables, VAR < 0.01: ${var < 0.01}"

#Subshell does not inherit variables
Var ﹣ 01 of echo "echo subshell is \ $var ﹣ 01" > tmp.sh
bash ./tmp.sh

#Export variable (environment variable), which can be inherited by the sub shell, which is equivalent to copying the exported variable when the sub shell starts
export VAR_01
bash ./tmp.sh

#Modifying var? 01 in a subshell does not affect

RM. / tmp.sh delete tmp.sh

Escape and reference

#!/bin/bash
To escape from justice
#As in other programming languages, escape characters are used\
Echo \ 3535; use escape to output comment symbols\#
Dollar=123
echo $Dollar is $Dollar
echo 8 \* 8 = 64

Citation
#There are four quotation marks in the shell, namely double quotation mark, single quotation mark, back quotation mark and escape character

#"" double quotation mark: partial reference, variable can be interpreted
echo "$Dollar is $Dollar"
#Looks the same without double quotes, but it's different for output spaces
VAR="A     B      C"
Echo without quotes only outputs one for consecutive spaces: $var
Echo "quoted will output all spaces: $var"

#'' single quotation mark: full reference, output content only according to literal meaning, and escape character cannot be used
Echo '$Dollar in single quotes or $Dollar. '
Echo 'escape character output in single quotes \, single quotes output only content as literal'
Echo 'escape character cannot be used, single quote cannot be output within single quote'

#'` back quote: Command substitution, which assigns the standard output of a command to a variable as a value
#Command substitution can also be in the form of $(command)
LS=`ls`
echo "=== LS ==="
echo $LS
echo "=== LS ==="
LSA=$(ls -a)
echo $LSA
#$() supports nesting
$(echo $(ls) > tmp.sh)
TMP=$(cat tmp.sh)
echo === tmp ===
echo $TMP
echo === tmp ===
rm tmp.sh

operator

#!/bin/bash
Operator operator
#Shell operators mainly include:
#Comparison operator (integer comparison), string operator (string test), file operation operator (for file test), logical operator, arithmetic operator, bit operator, auto increase and auto decrease, etc

#Arithmetic operators: addition, subtraction, multiplication, division, remainder power, addition, subtraction, multiplication, elementary, remainder, etc
A=1
B=2
Let "C = $a + $B" ා the let keyword is required to perform the operation
echo $C

#Bitwise operator: shift left and right bitwise and bitwise OR bitwise non bitwise exclusive or
var1=1
var2=5

let "var = $var1<<2"
echo $var
let "var = $var1&$var2"
echo $var
#Bitwise non is - ($var + 1)
let "var = ~8"
echo $var

#Like other languages, it can be divided into pre and post
var1=1
echo "var1 is $var1"
let "var2=++var1"
Echo "var2 pre auto increment VAR1, $var2"
var1=1
let "var2=var1++"
Echo "var2 post auto increment VAR1, $var2"

#Other arithmetic operations
#Use $[] to do operations: similar to $(()), $[] can be used for simple arithmetic operations
echo '$[1+1]' is $[1+1]
echo '$[5 ** 2]' is $[5 ** 2]

#Use expr for operation: use expr to separate operands and operators with spaces, otherwise it will be treated as a string
expr 1+1
expr 1 + 1
Expr 2 \ * 2 ා escape required for special character operator

#Arithmetic extension: $((arithmetic expression))
echo $((2*(1+1)))

#Using BC for operations
#The operations described above can only be based on integers. If you want to calculate high-precision decimals, you can use the BC tool under Linux.
#BC is a high-precision computing language, which supports sequential execution, judgment, loop, function, etc. here is a simple example
NUM1=1.2
NUM2=2.3
SUM=$(echo "$NUM1+$NUM2" | bc)
echo $SUM
#You can also enter BC directly at the command line, and enter the BC command line mode

Special characters

#!/bin/bash
#Special characters

Wildcard wildcard character
Wildcard wildcard character用于模式匹配,常见的通配符有 *、? 和用 [] 括起来的字符序列。
#For example: a * can match any length of string starting with a, but cannot contain a dot and slash
#So a * can't match abc.txt
#? can match any single character
#[] means that any one of them can be matched, for example [a B C] can match a or B or C
#Start and end can be represented by - in []. For example, [A-Z] matches all lowercase letters
#*? In [], it means common characters, without universal matching effect

Quotation mark
#02 "escape and reference. Sh, mainly including single quotation mark, double quotation mark and back quotation mark

#Annotation symbols

Curly braces
Curly braces {} 在 Shell 中的用法很多
#1. Variable extension ${PWD}
#2. Wildcard extension
#3. Statement block
Wildcard wildcard character扩展的例子:
touch file_{A,B}
ls . | grep file
rm file_A
rm file_B

Others
#Location parameters
#$0: script name itself
#$1, $2... First parameter of script, second parameter
#Total $ා variables
#$* [email protected] show all parameters
#$? Return value of the previous command's exit
Echo $? ා exit normally, result is 0
rm qweqweqweqwe
When echo $? ා, the result is not 0
#$! ID number of the last background process

test

#!/bin/bash
#Testing: specific commands are often required to be executed according to the actual situation during program operation,
#For example, to determine whether a file exists, if not, you may need to create the file first
# ls tmp.sh
# echo $?

#Test structure
#1. Test expression uses the test instruction
#2. [expression] uses []

#Document test
# 1. test file_operator FILE
# 2. [file_operator FILE]
test -e tmp.sh
Echo $? ා does not exist, last instruction result was 1
[ -e tmp.sh ]
echo $?

#Document test符,文件不存在时,均返回假
#- B file returns true when the file exists and is a block file, otherwise false
#- C file returns true when the file exists and is a device file, otherwise false
#- D file test whether the file is a directory
#- e file test whether the file or directory exists
#- f file test whether the file is a normal file
#- x file determines whether the file is an executable
#- W file determines that the file is writable
#- R file determines that the file is readable
#- L file to determine whether it is a linked file
#- P file to determine whether it is a pipeline file
#- s file determines that the file exists and the size is not 0
#- s file determines whether it is a socket file
#- G file determines whether the file has sgid set
#- U file determines whether the file has suid set
#- K file determines whether the file has the sticky property set
#- G file determines that the file belongs to a valid user group
#- O file determines that the file belongs to a valid user
#Returns true when file1 - NT File2 file1 is newer than File2
#File1 - ot File2 file1 returns true when File2 is older

#String test
#It mainly includes equal to, not equal to, greater than, less than and whether it is empty
#Return true when - Z string is empty
Echo "string test"
[ -z "" ]
Echo '[- Z ""]' $? ා result 0, true

#- N string returns true when it is not empty
[ -n "aaa" ]
echo '[ -n "aaa" ]' $?
[ "string1" = "string2" ]
echo '[ "string1" = "string2" ]' $?
[ "string1" != "string2" ]
echo '[ "string1" != "string2" ]' $?
[ "string1" > "string2" ]
echo '[ "string1" > "string2" ]' $?
[ "string1" < "string2" ]
echo '[ "string1" < "string2" ]' $?

#Integer comparison
#- EQ means equal
-gt
-lt
-ge =
-le =
-ne!
[ 1 -eq 2 ]
echo '[ 1 -eq 2 ]' $?
[ 1 -gt 2 ]
echo '[ 1 -gt 2 ]' $?
[ 1 -lt 2 ]
echo '[ 1 -lt 2 ]' $?
[ 1 -ge 2 ]
echo '[ 1 -ge 2 ]' $?
[ 1 -le 2 ]
echo '[ 1 -le 2 ]' $?
[ 1 -ne 2 ]
echo '[ 1 -ne 2 ]' $?

#Logical tester and logical operator
#! expression negate
#Expression - A expression is true, the result is true        
#Expression - O expression only one is true, the result is true
touch tmp.sh
[ ! -e tmp.sh ]
echo '[ ! -e tmp.sh ]' $?

[ -e tmp.sh -a -e tmp1.sh ]
echo '[ -e tmp.sh -a -e tmp1.sh ]' $?

[ -e tmp.sh -o -e tmp1.sh ]
echo '[ -e tmp.sh -o -e tmp1.sh ]' $?

#- A - O can be replaced by & & and|, but there will be differences in writing
[ -e tmp.sh ] && [ -e tmp1.sh ]
echo '[ -e tmp.sh ] && [ -e tmp1.sh ]' $?

[ -e tmp.sh ] || [ -e tmp1.sh ]
echo '[ -e tmp.sh ] || [ -e tmp1.sh ]' $?

rm tmp.sh
rm string2

judge

#!/bin/bash
#Bash's judgment and loop are similar to other languages, with if else elif case

#If judgment structure
# if expression; then
#    command
# elif expression; then
#    command
# else
#    command
# fi
if [ ! -e tmp.sh ];
then
  Echo "tmp.sh does not exist, create it"
  touch tmp.sh

  if [ -e tmp.sh ]; then
    Echo "TMP. Sh created"
  else
    Echo "tmp.sh creation failed"
  fi
else
  Echo "tmp.sh exists, delete it"
  rm tmp.sh
fi

#Case judgment structure
# case VAR in
# var1) command ;;
# var2) command ;;
# ...
# *) command ;;
# esac
Read - P "please enter the number:" num
case $NUM in
1) Echo "input is 1";;
2) Echo "input is 2";;
*) echo "input as other";;
esac

rm tmp.sh

loop

#!/bin/bash
Loop system
#There are mainly for, while, until, and select loops in shell

For cycle
#For loop with list:
# for VAR in (list)
# do
#   command
# done
for NUMBER in 1 2 3 4 5
do
  echo $NUMBER
done

fruits="apple banana orange"
for FRUIT in ${fruits}
do
  echo $FRUIT
done

Loop system数字时可以使用 {a..b} 表示从 a 循环到 b
for N in {2..10}
do
  echo $N
done
#Where {2.. 10} can be replaced by the SEQ command
echo "echo with seq:"
for N in $(seq 2 10)
do
  echo $N
done
#The SEQ command can add "step size"
for N in $(seq 1 2 20)
do
  echo $N
done

#As you can see, the content after for in can be the standard output of any command
#For example, we can output all files with SH in the current directory
for VAR in $(ls | grep sh)
do
  echo $VAR
done

#If there is no in after for, it is equivalent to [email protected]
#You can run B a sh 07 loop. Sh a B C try it
for VAR
do
  echo $VAR
done

#For loop of class C
# for ((exp1; exp2; exp3))
# do
#   command
# done
for ((i=0, j=100; i < 10; i ++))
do
  echo $i $j
done


#While loop
#The syntax is as follows:
# while expression
# do
#   command
# done
#While ((1)) will loop indefinitely
COUNT=0
while [ $COUNT -lt 5 ]
do
  echo $COUNT
  let "COUNT++"
done

#While reading files by line
echo "john  30  boy
sue   20  girl" > tmp.txt
while read LINE
do
  NAME=`echo $LINE | awk '{print $1}'`
  AGE=`echo $LINE | awk '{print $2}'`
  SEX=`echo $LINE | awk '{print $3}'`
  echo $NAME $AGE $SEX
Do < tmp.txt input redirection
rm tmp.txt


#Until loop
#Until is similar to while. The difference is that if it is judged as no, it will continue to loop. If it is judged as true, it will continue to loop
#Until ((0)) will loop indefinitely
COUNT=0
until [ $COUNT -gt 5 ]
do
  echo $COUNT
  let "COUNT++"
done


#Select cycle
#Select is a kind of menu type loop. Its syntax structure is similar to for. The value of each loop is selected by the user
echo "choose your menu:"
select MENU in "apple" "banana" "orange" "exit"
do
  echo "you choose $MENU"
  if [[ $MENU = "exit" ]]
  then
    break
  else
    echo "choose again"
  fi
done

Loop system控制,break continue,与其他编程语言一致

function

#!/bin/bash
Gamma function

Gamma function的定义
# function FUNCTION_NAME() {
#   command
# }
#Omit function keyword
# FUNCTION_NAME() {
#   command
# }            
function func1 {
  echo 1 
}
func2() {
  echo 2 
}

Gamma function调用
func1
func2

Gamma function返回值
func3 () {
  Echo 'please enter the return value of the function:'
  read N
  return $N
}
func3
Echo "the return value of the previous function is" $? # (use $? To get the return value of the previous instruction

Gamma function参数
#Consistent with parameter usage of script
func4 () {
  Echo "first parameter $1"
  Echo "second argument $2"
  Echo "all parameters [email protected]"
  Echo "number of parameters $×"
}
func4 a b c

#Use set to specify the script (or function) parameter value of the location
func5() {
  set q w e
  Echo "Parameter1 $1"
  Echo "all parameters: [email protected]"
}
func5

#Move position parameter: in the shell, you can use the shift command to move the parameter one bit to the left
func6() {
  while [ $# -gt 0 ]
  do
    echo current \$1 is $1
    shift
  done
}
func6 q w e r t

#Implement a POW function
pow() {
  let "r=$1**$2"
  return $r

}
pow 2 5
echo $?

redirect

#!/bin/bash
Reorientation
Reorientation是指将原本由标准输入输出的内容,改为输入输出的其他文件或设备

#When the system starts a process, three files are opened for the process:
#Standard input (stdin), standard output (stdout), standard error (stderr)
#Identified with file identifiers 0, 1 and 2 respectively
#If you want to open other input and output for the process, you need to start identification from certificate 3
#By default, standard input is keyboard, standard output and standard error are display


#Common IO redirection symbols
#> standard output overrides redirection. Redirecting the command's standard output to another file will directly overwrite the original file content
#> > append redirection to standard output. Redirect the standard output of the command to another file. The file will not be overwritten, but will be appended after the file
#> & identify output redirection, say output of one identity is redirected to input of another identity
#< standard input redirection, naming will read input from the specified file, not from the keyboard
#| pipeline, reading output from one command as input to another


#Output redirection
#Redirect the contents of the original standard output to the screen to the tmp.txt file
echo "result1" > tmp.txt
cat tmp.txt

echo "result2" > tmp.txt
cat tmp.txt

#Output append
Echo "output append:"
echo "result3" >> tmp.txt
echo "result3" >> tmp.txt
echo "result3" >> tmp.txt
cat tmp.txt
rm tmp.txt

#Identify output redirection
Echo "standard error not redirected, output directly to page"
#Make a nonexistent order
adhfafahdfakdf > tmp.txt
echo "tmp.txt:" `cat tmp.txt`
rm tmp.txt
Echo "redirect standard error to standard output, output to file"
asiiaodfuoaf > tmp.txt 2>&1
echo "tmp.txt:" `cat tmp.txt`

#Standard input redirection
Echo "standard input redirection:"
while read Line
do
  echo $Line
done < tmp.txt

Channel pipe
#Get the name of the. Sh file
ls | grep .sh | cut -f1 -d'.'

#Use Exec
#Exec is a built-in command of shell. When executing this command, the system will not start a new shell, but will replace the current shell process with the executed command
#Therefore, after exec command is executed, the shell process will actively exit
#For example, if exec LS is executed, other subsequent commands will not be executed. You can also open the shell directly and execute exec LS
#In addition, exec can be used for I / O redirection.
#Exec < file takes the contents of the file file as exec's standard input
#Exec > file file file as standard output
#Exec 3 < file specifies the file identifier
#Exec 3 < & - close file identifier
#Exec 3 > file writes the contents of the written file identifier to the specified file (output redirection)
#Exec 4 < & 3 creates file identifier 4, which is a copy of 3 (similar to ID output redirection 2 > & 1)
#Note: different shell environments may be different. For example, I can't use exec redirection normally under Zsh of MAC

# Here Document
#Here doc, also known as here doc, is used to enter text by line in a command or script.
#Format is command < < delimiter
#Delimiter is the separator used to mark the end

Example:
#You can type sort < end on the command line
#You can try cat > TMP. TXT < end at the command line
cat << EOF > tmp.txt
1
2
3
EOF
cat tmp.txt

rm tmp.txt

array

#!/bin/bash
Array of vectors

#Bash only supports one-dimensional arrays

#Defining arrays
declare -a mArray
mArray[0]="nihao"
mArray[1]=2

#When defining, assign values. The elements of the array are separated by spaces. Other characters will be treated as values, such as "PHP", which will be treated as PHP,
declare -a mArray=("php" "python" 123)

Array of vectors取值,需要用 ${数组名[索引]} 语法
echo ${mArray[0]}
echo ${mArray[1]}
echo ${mArray[2]}
#Use @ * to index all elements
#@ gets element values separated by spaces
#* get the entire string
echo ${mArray[@]}
echo ${mArray[*]}

Array of vectors长度
Echo "the array length is ${ාාmaray [@]}"
Echo "the array length is ${ාාmaray [*]}"

Array of vectors截取
#You can get the subarray. The following example is to get the elements of the first and second subscripts of the array
echo ${mArray[@]: 1:2}
#You can get several characters of an element in the array. The following example is three characters starting from 0 to get the second element in the array
echo ${mArray[1]: 0:3}

#Merge arrays
Front=("javascript" "typescript")   Array of vectors声明也可以忽略 declear -a
Conn=(${mArray[@]} ${Front[@]})
echo ${Conn[@]}
Echo ${ාconn [@]}ාmerge to get the length of the array

#Replace element
mArray=(${mArray[@] /123/"java"})
echo ${mArray[@]}

#Cancel array or element
unset mArray[1]
Echo "after the element with subscript 1 is cancelled, the array is: ${maray [@]}, and the array length is ${ා maray [@]}"
#It should be noted that the element at position 1 of the array becomes empty, rather than the element after it moves forward
Echo "array 1 element is ${maray [1]}, 2 element is ${maray [2]}"

Character processing

#!/bin/bash
#Character processing

Channel pipe
#Read output from one command as input to another
An example of an example
# ls | grep .sh | cut -f1 -d'.'

# grep
#Grep is a line based text search tool. The common parameters of this command are:
#Grep [- IVNC] 'characters to match' filename
#- I case insensitive
#- C counts the number of rows containing matches
#- N output line number
#- V reverse match
#Where 'characters to match' supports regular expression pattern
Grep - in 'func' 01 "variable.sh

# sort
#Sort can sort unordered data
#Sort [- ntkr] filename
#- N sort by number
#- t specifies the separator
#- K specifies the column
#- R reverse sort

An example of an example 使用空格分开每行,按第二列进行排序
echo "3 1 3
1 2 4
5 3 2
1 2 4
5 3 4
2 3 4" | sort -t ' ' -k 2

# uniq
#Using uniq to remove duplicate content
echo "123
123
ab
ab" | uniq

#Cut cut text
#Cut - f column specified - D 'separator'
#The specified columns can be separated by commas, or ranges can be used
echo "jhon 10 boy class1
lili 12 girl class2" | cut -f2-4 -d ' '

#TR do text conversion
#TR 'original character', 'target character', where the original character corresponds to the target character one by one
Head - N 5 01 variable. Sh | tr '[A-Z]' [A-Z] '

#Paste text merge
#Paste merges the text in rows.
# paste -d
echo "1
2
3" > tmp1.txt
echo "a
b
c" > tmp2.txt
paste -d: tmp1.txt tmp2.txt > tmp.txt
cat tmp.txt

#Split split large file
# split -l lines file dist_file
An example of an example
Split - L 5 01 variable. Sh split file
ls | grep split_file
rm split_file*

#SED and awk
# ...
#If existing tools don't meet your string processing needs, take a look at the SED and awk commands.

rm tmp*

Example – operation database

#!/bin/bash
USER=root
PASSWORD=root

#Use - e to execute
databases=`mysql -u$USER -p$PASSWORD -e"show databases"`

for db in $databases
do
  echo "Tables in $db:"
  #Use here doc to execute code block
  mysql -u$USER -p$PASSWORD << EOF
use $db; 
show tables;
EOF
  #You can also use input redirection
  # mysql -u$USER 0pPASSWORD < select.sql
done

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]