Regardless of front-end or back-end, on the basis of learning Linux, you should also learn to write some simple shells. If you want to do something manually, it is very tiring and time-consuming. If you know how to use Shell script to handle it, it is very simple. This article covers some basic knowledge of Shell. This will continue to be updated.

Variables of the shell’s basic syntax

Shell variable naming
Variables can consist only of uppercase and lowercase letters, digits, and underscores. Variable names cannot start with a number. Variables can store numeric types or string types. Variable assignment must not have Spaces on either side of the equal sign. String variables can be enclosed in single or double quotation marks.Copy the code

Define variables:

➜ ~ name ="xiaoka"
➜ ~ age = 2Copy the code

Invalid definition

[root@iz2ze0ajic0vbv28hcdctpz hello]# 1212=sfds
-bash: 1212= SFDS: command was not found[root@iz2ze0ajic0vbv28hcdctpz hello]# ?aasd=hehe
-bash: ? Aasd = heHE: Command not foundCopy the code
Shell variable call

Variables are called using the $symbol or $symbol with curly braces. In general, use curly braces. For example:

[root@iz2ze0ajic0vbv28hcdctpz hello]# apple=hongfushi
[root@iz2ze0ajic0vbv28hcdctpz hello]# echo $applepen

[root@iz2ze0ajic0vbv28hcdctpz hello]# echo ${apple}pen
hongfushipen
Copy the code

Without curly braces, applepen is read as a variable, so parsing is ambiguous.

Shell variable classification
  • Environment variables: Save the parameters used when the operating system is running.
  • Positional variables: Used when passing script parameters.
  • Predefined variables: Similar to environment variables, except that they cannot be redefined.
  • Custom variables: User-defined variables can be used in user-written scripts.

Custom variables: Define variables database:

[root@iz2ze0ajic0vbv28hcdctpz hello]# database=mysql

[root@iz2ze0ajic0vbv28hcdctpz hello]# echo $database
mysql
[root@iz2ze0ajic0vbv28hcdctpz hello]# echo ${database}
mysql Copy the code

Environment variables: View global environment variables:

[root@iz2ze0ajic0vbv28hcdctpz ~]# printenv
XDG_SESSION_ID=2044
HOSTNAME=iz2ze0ajic0vbv28hcdctpz
TERM=xterm-256color
SHELL=/bin/bash
HISTSIZE=1000 SSH_CLIENT = 113.44.42.209 22 56074SSH_TTY=/dev/pts/1 USER=root LS_COLORS=rs=0:di=38; 5; 27:ln=38; 5; 51:mh=44; 38; 5; 15:pi=40; 38; 5; 11:so=38; 5; 13:do= 38; 5; 5:bd=48; 5; 232; 38; 5; 11:cd= 48; 5; 232; 38; 5; 3:or=48; 5; 232; 38; 5; 9:mi=05; 48. 5; 232; 38; 5; 15:su=48; 5; 196; 38; 5; 15:sg=48; 5; 11. 38; 5; 16:ca=48; 5; 196; 38; 5; 226:tw=48; 5; 10; 38; 5; 16:ow=48; 5; 10; 38; 5; 21:st=48; 5; 21. 38; 5; 15:ex=38; 5; 34:*.tar=38; 5; 9:*.tgz=38; 5; 9:*.arc=38; 5; 9:*.arj=38; 5; 9:*.taz=38; 5; 9:*.lha=38; 5; 9:*.lz4=38; 5; 9:*.lzh=38; 5; 9:*.lzma=38; 5; 9:*.tlz=38; 5; 9:*.txz=38; 5; 9:*.tzo=38; 5; 9:*.t7z=38; 5; 9:*.zip=38; 5; 9:*.z=38; 5; 9:*.Z=38; 5; 9:*.dz=38; 5; 9:*.gz=38; 5; 9:*.lrz=38; 5; 9:*.lz=38; 5; 9:*.lzo=38; 5; 9:*.xz=38; 5; 9:*.bz2=38; 5; 9:*.bz=38; 5; 9:*.tbz=38; 5; 9:*.tbz2=38; 5; 9:*.tz=38; 5; 9:*.deb=38; 5; 9:*.rpm=38; 5; 9:*.jar=38; 5; 9:*.war=38; 5; 9:*.ear=38; 5; 9:*.sar=38; 5; 9:*.rar=38; 5; 9:*.alz=38; 5; 9:*.ace=38; 5; 9:*.zoo=38; 5; 9:*.cpio=38; 5; 9:*.7z=38; 5; 9:*.rz=38; 5; 9:*.cab=38; 5; 9:*.jpg=38; 5; 13:*.jpeg=38; 5; 13:*.gif=38; 5; 13:*.bmp=38; 5; 13:*.pbm=38; 5; 13:*.pgm=38; 5; 13:*.ppm=38; 5; 13:*.tga=38; 5; 13:*.xbm=38; 5; 13:*.xpm=38; 5; 13:*.tif=38; 5; 13:*.tiff=38; 5; 13:*.png=38; 5; 13:*.svg=38; 5; 13:*.svgz=38; 5; 13:*.mng=38; 5; 13:*.pcx=38; 5; 13:*.mov=38; 5; 13:*.mpg=38; 5; 13:*.mpeg=38; 5; 13:*.m2v=38; 5; 13:*.mkv=38; 5; 13:*.webm=38; 5; 13:*.ogm=38; 5; 13:*.mp4=38; 5; 13:*.m4v=38; 5; 13:*.mp4v=38; 5; 13:*.vob=38; 5; 13:*.qt=38; 5; 13:*.nuv=38; 5; 13:*.wmv=38; 5; 13:*.asf=38; 5; 13:*.rm=38; 5; 13:*.rmvb=38; 5; 13:*.flc=38; 5; 13:*.avi=38; 5; 13:*.fli=38; 5; 13:*.flv=38; 5; 13:*.gl=38; 5; 13:*.dl=38; 5; 13:*.xcf=38; 5; 13:*.xwd=38; 5; 13:*.yuv=38; 5; 13:*.cgm=38; 5; 13:*.emf=38; 5; 13:*.axv=38; 5; 13:*.anx=38; 5; 13:*.ogv=38; 5; 13:*.ogx=38; 5; 13:*.aac=38; 5; 45:*.au=38; 5; 45:*.flac=38; 5; 45:*.mid=38; 5; 45:*.midi=38; 5; 45:*.mka=38; 5; 45:*.mp3=38; 5; 45:*.mpc=38; 5; 45:*.ogg=38; 5; 45:*.ra=38; 5; 45:*.wav=38; 5; 45:*.axa=38; 5; 45:*.oga=38; 5; 45:*.spx=38; 5; 45:*.xspf=38; 5; 45:MAVEN_HOME=/usr/local/ apache maven -- 3.6.0MAIL=/var/spool/mail/root PATH=/usr/local/ apache maven - 3.6.0 / bin: / usr /local/ jdk1.8.0 _191 / bin: / usr /local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin PWD=/root JAVA_HOME=/usr/local/ jdk1.8.0 _191LANG=zh_CN.UTF-8 HISTCONTROL=ignoredups SHLVL=1 HOME=/root LOGNAME=root CLASSPATH=.:/lib/dt.jar:/lib/tools.jar SSH_CONNECTION = 113.44.42.209 172.17.187.244 22 56074LC_CTYPE=zh_CN.UTF-8 LESSOPEN=||/usr/bin/lesspipe.sh %s XDG_RUNTIME_DIR=/run/user/0 _=/usr/bin/printenv Copy the code

Define an environment variable

[root@iz2ze0ajic0vbv28hcdctpz ~]# export kaka="kaka"
[root@iz2ze0ajic0vbv28hcdctpz ~]# env |grep kaka
kaka=kaka
Copy the code

Deleting environment variables

[root@iz2ze0ajic0vbv28hcdctpz ~]# unset kaka
[root@iz2ze0ajic0vbv28hcdctpz ~]# env |grep kaka
Copy the code

If you use them for a long time, you can write them in the configuration file. /etc/profile or.bash_profile of the user’s home directory

Position variables: Let’s define a shell.

[root@iz2ze0ajic0vbv28hcdctpz ~]# cat test.sh
#! /bin/bash
echo "Execute file:$0"
echo "First parameter:The $1"
echo "Second parameter:$2" Copy the code

View the result.

[root@iz2ze0ajic0vbv28hcdctpz ~]# chmod +x test.sh
[root@iz2ze0ajic0vbv28hcdctpz ~]# ./test.sh 1 2
Run the./test.sh fileThe first parameter: 1The second parameter: 2Copy the code

Predefined variables: Commonly used predefined variables are as follows.


#! /bin/bash
echo "Execute file:$0"
echo "First parameter:The $1"
echo "Second parameter:$2"
echo All parameters: $* echo "Pid of current script: $" echo "Number of script parameters:$#" Copy the code

Viewing the Execution Result

[root@iz2ze0ajic0vbv28hcdctpz ~]# ./test.sh 1 2
Run the./test.sh fileThe first parameter: 1The second parameter: 2All parameters: 1 2Pid of the current script: 30170Number of script parameters: 2Copy the code

View the result of the previous command.

[root@iz2ze0ajic0vbv28hcdctpz hello]# ls
test
[root@iz2ze0ajic0vbv28hcdctpz hello]# echo $?
0
Copy the code

The shell operator.

The shell is a language, and it’s a little bit like any other language operator, but here we go. This section describes operators.

Operator type
  • Arithmetic operator
  • Relational operator
  • Boolean operator
  • Logical operator
  • String operator
  • File test operator
Arithmetic operator

Command: expr

Function: Evaluates an expression

Syntax: expr expression

Rules:

  • There must be Spaces on both sides of the operation symbol

  • When encountering special symbols such as asterisks, you need to add a backslash

  • Spaces and special strings need to be enclosed in quotes

    Operation :(write a shell script related to the operation first)

#! /bin/bash
i=1
j=2
addition=`expr $i + $j`
subtraction=`expr $i - $j`
multiplication=`expr $jA \ *$j` division=`expr $j / $i` mod=`expr $j % $i` echo "addition=${addition} , subtraction=${subtraction} , multiplication=${multiplication} , division=${division} , mod=${mod}"  if [ $i= =$j ] then  echo "I is equal to the j" fi  if [ $i! =$j ] then  echo "I is not equal to j" fi Copy the code

View the results:

➜ ~. / operation. Shaddition=3 , subtraction=-1 , multiplication=4 , division=2 , mod=0
I is not equal to jCopy the code

Relational operator


Action :(scripting)

#! /bin/bash
num1=5
num2=8
if [ $num1 -eq $num2 ]
then
 echo "$num1Whether is equal to the$num2Num1 = num2" else  echo "$num1Whether is equal to the$num2Num1 does not equal num2" fi if [ $num1 -ne $num2 ] then  echo "$num1Is it not equal to$num2Num1 does not equal num2" else  echo "$num1Is it not equal to$num2Num1 = num2" fi if [ $num1 -gt $num2 ] then  echo "$num1If more than$num2Num1 > num2" else  echo "$num1If more than$num2Num1 not greater than num2" fi if [ $num1 -lt $num2 ] then  echo "$num1Is less than$num2Num1 < num2" else  echo "$num1Is less than$num2: num1 not less than num2" fi if [ $num1 -ge $num2 ] then  echo "$num1Whether greater than or equal to$num2: num1 is greater than or equal to num2" else  echo "$num1Whether greater than or equal to$num2Num1 < num2" fi if [ $num1 -le $num2 ] then  echo "$num1Whether less than or equal to$num2: num1 is less than or equal to num2" else  echo "$num1Whether less than or equal to$num2Num1 > num2" fi Copy the code

Operation results:

➜ ~. / function. ShWhether 5 equals 8: num1 does not equal num2Does 5 not equal 8? Num1 does not equal num2Whether 5 is greater than 8: NUM1 is not greater than NUM2Whether 5 is less than 8: num1 is less than num2Whether 5 is greater than or equal to 8: num1 is smaller than num2Whether 5 is less than or equal to 8: num1 is less than or equal to num2Copy the code
Boolean operator

Scripting:

#! /bin/bash
num1=3
num2=23
if [ $num1! =$num2 ]
then
 echo "$num1! =$num2Num1 does not equal num2" else  echo "$num1! =$num2Num1 = num2" fi if [ $num1 -lt 25 -a $num2 -gt 15 ] then  echo "$num1Less than 25 and$num2Greater than 15: return true" else  echo "$num1Less than 25 and$num2Greater than 15: return false" fi if [ $num1 -lt 25 -o $num2 -gt 25 ] then  echo "$num1Less than 25 or$num2Greater than 25: return true" else  echo "$num1Less than 25 or$num2Greater than 25: return false" fi Copy the code

View the results:

➜ ~. / bool. Sh3! = 23: num1 does not equal num23 is less than 25 and 23 is greater than 15true
3 less than 25 or 23 greater than 25: returnstrue
Copy the code
String operator

Scripting:

#! /bin/bash
apple="apple"
pen="pen"

if [ $apple = $pen ]
then  echo "$apple = $pen: Apple equals pen" else  echo "$apple = $penApple does not equal pen." fi if [ $apple! =$pen ] then  echo "$apple! =$penApple does not equal pen." else  echo "$apple! =$pen: Apple equals pen" fi if [ -z $apple ] then  echo "-z $apple: Contains 0 characters. else  echo "-z $apple: Contains a maximum of 0 characters. fi if [ -n "$apple" ] then  echo "-n $apple: Contains a maximum of 0 characters. else  echo "-n $apple: Contains 0 characters. fi if [ $apple ] then  echo "$apple: string is not empty" else  echo "$apple: string is empty" fi Copy the code

Execution Result:

[root@iz2ze0ajic0vbv28hcdctpz ~]# ./string.sh
Apple = pen: Apple does not equal penapple ! = pen: Apple does not equal pen-z apple: the value cannot contain 0 characters-n apple: The value cannot contain 0 charactersApple: the string is not emptyCopy the code
Logical operator

Scripting:

#! /bin/bash
num1=5
num2=8
if [[ $num1 -lt 10 && $num2 -gt 5 ]]
then
 echo "Return true" else  echo "False" fi  if [[ $num1 -lt 10 || $num2 -gt 5 ]] then  echo "Return true" else  echo "False" fi Copy the code

Execution Result:

[root@iz2ze0ajic0vbv28hcdctpz ~]# ./logic.sh
returntrue
returntrue
Copy the code
File test operator

Scripting:

~ cat testfile.sh
#! /bin/bash
file="/root/testfile.sh"

if [ -r $file ] then  echo "This file is readable" else  echo "This file is unreadable" fi if [ -x $file ] then  echo "This file is executable" else  echo "This file is not executable" fi if [ -w $file ] then  echo "This file is writable" else  echo "This file is not writable" fi  if [ -f $file ] then  echo "This file is a common file." else  echo "This file is not a common file." fi  if [ -s $file ] then  echo "This file is empty" else  echo "This file is not empty" fi if [ -d $file ] then  echo "This file is a directory" else  echo "This file is not a directory" fi Copy the code

Execute the script:

➜ ~. / testfile. ShThe file is readableThe file is executableThe file is writableThis file is a normal fileThe file is emptyThe file is not a directoryCopy the code

Shell condition judgment & flow control

In fact, the essence of flow control is similar, but there are a few differences in syntax. In this section, we will make a tape.

Single branch if

Process diagram:


For example:

if cat if-one.sh
#! /bin/bash
a=10

if [ a > 5 ]
then  echo "A greater than 5" fi Copy the code

Execution Result:

if. /if-one.sh
A greater than 5Copy the code
Double branch if

Process diagram:


For example:

if cat if-double.sh
#! /bin/bash
age=18
if [ $age -ge 18 ]; then
  echo "Adult"
else  echo "Minor" fi Copy the code

Execution Result:

if. /if-double.sh
adultCopy the code
Multi-branch if statement

Process diagram:


For example:

if cat if-multi.sh
#! /bin/bash
money=10
if [ $money -lt 1 ]
then
 echo "Pipikachu with no dollar decided to eat dirt at home." elif [ $money -ge 1 -a $money -lt 5 ] then  echo "Boss, I want white Rabbit candy." elif [ $money -ge 5 -a $money -le 10 ] then  echo "Boss, I want chocolate." else  echo "Boss, bring me a drumstick." fi Copy the code

Execution Result:

if. /if-multi.sh
Boss, I want chocolateCopy the code

This article has updated some shell basics. If you want to learn it, don’t copy it. Continuously updated… ⛽ ️

This article is formatted using MDNICE