Chapter 6 Introduction to Shell script programming

Keywords: Linux Operation & Maintenance architecture DevOps Cloud Native

2.7.11 script security and set

set command: can be used to customize the shell environment

$- variable

h: hashall, after the option is turned on, the Shell will hash the path where the command is located to avoid querying every time. Turn off the H option through set +h
i: Interactive comments, including this option, indicates that the current shell is an interactive shell. In the so-called interactive shell, the I option is turned off in the script
m: monitor, open the monitoring mode, and you can control the process stop, continue, background or foreground execution through Job control
B: braceexpand, brace extension
H: When the history and H options are turned on, you can expand the commands in the history list through! Exclamation point to complete, for example, "!" returns the last historical command, "! n" returns the nth historical command

example:

[root@rocky8 bin]# echo $-
himBHs
#When the h option is turned on, the shell will hash the path where the command is located to avoid querying every time

[root@rocky8 bin]# set +h #Off h
[root@rocky8 bin]# echo $-
imBHs
[root@rocky8 bin]# hash
-bash: hash: hashing disabled

[root@rocky8 bin]# set -h #Open h
[root@rocky8 bin]# echo $-
himBHs
[root@rocky8 bin]# hash
hits	command
   2	/usr/bin/grep
   1	/usr/sbin/ping
   2	/usr/bin/ls

#B {} curly bracket extension
[root@rocky8 bin]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@rocky8 bin]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@rocky8 bin]# set +B
[root@rocky8 bin]# echo $-
himHs
[root@rocky8 bin]# echo {1..10}
{1..10}
[root@rocky8 bin]# set -B
[root@rocky8 bin]# echo $-
himBHs
[root@rocky8 bin]# echo {1..10}
1 2 3 4 5 6 7 8 9 10

Script security with set command

-u displays an error message when expanding a variable that is not set, which is equivalent to set -o nounset

-e if a command returns a non-zero exit status value (failure), it exits, which is equivalent to set -o errexit

-o option display, turn options on or off

Display options: set -o

Open option: set -o option

Off option: set +o option

-x when executing the command, print the command and its parameters, similar to bash -x

example:

[root@rocky8 bin]# DIR=/data
[root@rocky8 bin]# rm -rf $DiR/*
#Define a variable. When we execute the command, if we input the variable name incorrectly, it is equivalent to executing an empty variable. The command rm -rf $DiR / * is equivalent to rm -rf /, which deletes the root

[root@rocky8 bin]# vim set.sh
[root@rocky8 bin]# cat set.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      set.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
set -u      #If the value of the set -u variable is empty, an error is reported                                                                                                   
DIR=/data
rm -rf $Dir/*   #If you input a variable incorrectly

[root@rocky8 bin]# bash set.sh
set.sh: line 14: Dir: unbound variable
#Tip Dir is an undefined variable, so it won't let you execute it

[root@rocky8 bin]# vim set2.sh
[root@rocky8 bin]# cat set2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      set2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
set -e
cmd
hostname

[root@rocky8 bin]# bash set2.sh 
set2.sh: line 13: cmd: command not found
#Once an error command is found in set -e, it will directly report an error and will not execute the following commands

#In production, it is recommended that you enable set ue in the script to prevent misoperation and unpredictable results

example:

[root@rocky8 bin]# set -o
allexport      	off
braceexpand    	on
emacs          	on
errexit        	off
errtrace       	off
functrace      	off
hashall        	on
histexpand     	on
history        	on
ignoreeof      	off
interactive-comments	on
keyword        	off
monitor        	on
noclobber      	off
noexec         	off
noglob         	off
nolog          	off
notify         	off
nounset        	off
onecmd         	off
physical       	off
pipefail       	off
posix          	off
privileged     	off
verbose        	off
vi             	off
xtrace         	off

2.8 format output printf

format

printf "Specified format" "Text1" "Text2 "

Common format substitution

Substitution characterfunction
%scharacter string
%d,%iDecimal integer
%fFloating point format
%cASCII character, that is, the first character to display the corresponding parameter
%bWhen the corresponding parameter contains escape characters, you can use this replacement character to replace, and the corresponding escape characters will be escaped
%oOctal value
%uUnsigned decimal value
%xHex value (a-f)
%XHex value (A-F)
%%Represents the% itself

explain:

%#The number in s represents the width of the output character in this replacement character. If it is insufficient to fill in spaces, it is right aligned by default,% - 10s represents 10 characters wide, and - represents left aligned
%03d Represents a 3-bit width,If it is insufficient, fill in the front with 0,Out of digit as is output
%.2f 2 in represents the number of decimal places displayed after the decimal point

Common escape characters

Escape characterfunction
\aWarning character, usually ASCII BEL character
\bback off
\fPage change
\nLine feed
\renter
\tHorizontal tab
\vvertical tab
\Represents the \ itself

example:

[root@rocky8 bin]# printf "%s\n" 1 2 3 4
1
2
3
4
[root@rocky8 bin]# printf "%s %s \n" 1 2 3 4
1 2 
3 4

[root@rocky8 bin]# printf "%f\n" 1 2 3 4
1.000000
2.000000
3.000000
4.000000

#. 2f means to keep two decimal places
[root@rocky8 bin]# printf "%.2f\n" 1 2 3 4
1.00
2.00
3.00
4.00

[root@rocky8 bin]# printf "(%s)" 1 2 3 4;echo
(1)(2)(3)(4)
[root@rocky8 bin]# printf "(%s) " 1 2 3 4;echo ""
(1) (2) (3) (4)
[root@rocky8 bin]# printf "(%s)\n" 1 2 3 4
(1)
(2)
(3)
(4)
[root@rocky8 bin]# printf "%s %s\n" 1 2 3 4
1 2
3 4
[root@rocky8 bin]# printf "%s %s %s\n" 1 2 3 4
1 2 3
4  

#%-10s means 10 characters wide, left justified
[root@rocky8 bin]#  Printf "% - 10s% - 10s% - 4S% s \ n" name gender age weight Xiaoming male 20 70 Xiaohong female 18 50
 full name     Gender     Age and weight 
Xiao Ming     male        20   70 
Xiao Hong     female        18   50 

#Convert decimal 17 to hexadecimal
[root@rocky8 bin]# printf "%X" 17
11[root@rocky8 bin]#

#Convert hexadecimal C to decimal
[root@rocky8 bin]# printf "%d\n" 0xC
12

[root@rocky8 bin]# VAR="welcome to linux study";printf "\033[1;31m%s\033[0m\n" $VAR
welcome
to
linux
study

[root@rocky8 bin]# VAR="welcome to linux study";printf "\033[1;31m%s\033[0m\n" "$VAR"
welcome to linux study

2.9 arithmetic operation

Shell allows the evaluation of arithmetic expressions in some cases, such as let and declare built-in commands, (()) Compound commands and arithmetic extensions. Evaluation takes place as a fixed width integer without checking for overflow, although dividing by 0 is trapped and marked as an error. Operators and their precedence, relevance and values are the same as those in C. The following list of operators is grouped into equal priority operator levels. Levels take precedence in descending order.

Note: bash only supports integers, not decimals

[root@rocky8 bin]# man bash
ARITHMETIC EVALUATION
       The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands,  the
       (( compound command, and Arithmetic Expansion).  Evaluation is done in fixed-width integers with no check for overflow, though divi‐
       sion by 0 is trapped and flagged as an error.  The operators and their precedence, associativity, and values are the same as in  the
       C  language.   The following list of operators is grouped into levels of equal-precedence operators.  The levels are listed in order
       of decreasing precedence.

       id++ id--
              variable post-increment and post-decrement
       ++id --id
              variable pre-increment and pre-decrement
       - +    unary minus and plus
       ! ~    logical and bitwise negation
       **     exponentiation #Power
       * / %  multiplication, division, remainder #%Indicates modulus, i.e. remainder, for example: 9% 4 = 1, 5% 3 = 2
       + -    addition, subtraction
       << >>  left and right bitwise shifts
       <= >= < >
              comparison
       == !=  equality and inequality
       &      bitwise AND
       ^      bitwise exclusive OR
       |      bitwise OR
       &&     logical AND
       ||     logical OR
       expr?expr:expr
              conditional operator
       = *= /= %= += -= <<= >>= &= ^= |=
              assignment
       expr1 , expr2
              comma

       Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated.  Within an expression,
       shell variables may also be referenced by name without using the parameter expansion syntax.  A shell variable that is null or unset
       evaluates to 0 when referenced by name without using the parameter expansion syntax.  The value of a variable  is  evaluated  as  an
       arithmetic  expression  when  it  is  referenced,  or when a variable which has been given the integer attribute using declare -i is
       assigned a value.  A null value evaluates to 0.  A shell variable need not have its integer attribute turned on to  be  used  in  an
       expression.

       Constants  with  a leading 0 are interpreted as octal numbers.  A leading 0x or 0X denotes hexadecimal.  Otherwise, numbers take the
       form [base#]n, where the optional base is a decimal number between 2 and 64 representing the arithmetic base, and n is a  number  in
       that  base.   If base# is omitted, then base 10 is used.  When specifying n, the digits greater than 9 are represented by the lower‐
       case letters, the uppercase letters, @, and _, in that order.  If base is less than or equal to 36, lowercase and uppercase  letters
       may be used interchangeably to represent numbers between 10 and 35.

       Operators  are evaluated in order of precedence.  Sub-expressions in parentheses are evaluated first and may override the precedence
       rules above.

Multiplication symbols need to be escaped in some scenarios

Implement arithmetic operations:

(1) let var=Arithmetic expression
(2) ((var=Arithmetic expression)) Equivalent to the above
(3) var=$[Arithmetic expression]
(4) var=$((Arithmetic expression))
(5) var=$(expr arg1 arg2 arg3 ...)
(6) declare -i var = numerical value
(7) echo 'Arithmetic expression' | bc

Built in random number generator variables:

$RANDOM Value range: 0-32767

example:

[root@rocky8 bin]# x=10
[root@rocky8 bin]# y=20
[root@rocky8 bin]# z=$x+$y
[root@rocky8 bin]# echo $z
10+20
#By default, the shell does not operate on variable values

[root@rocky8 bin]# let --help
let: let arg [arg ...]
    Evaluate arithmetic expressions.
    
    Evaluate each ARG as an arithmetic expression.  Evaluation is done in
    fixed-width integers with no check for overflow, though division by 0
    is trapped and flagged as an error.  The following list of operators is
    grouped into levels of equal-precedence operators.  The levels are listed
    in order of decreasing precedence.
    
    	id++, id--	variable post-increment, post-decrement
    	++id, --id	variable pre-increment, pre-decrement
    	-, +		unary minus, plus
    	!, ~		logical and bitwise negation
    	**		exponentiation
    	*, /, %		multiplication, division, remainder
    	+, -		addition, subtraction
    	<<, >>		left and right bitwise shifts
    	<=, >=, <, >	comparison
    	==, !=		equality, inequality
    	&		bitwise AND
    	^		bitwise XOR
    	|		bitwise OR
    	&&		logical AND
    	||		logical OR
    	expr ? expr : expr
    			conditional operator
    	=, *=, /=, %=,
    	+=, -=, <<=, >>=,
    	&=, ^=, |=	assignment
    
    Shell variables are allowed as operands.  The name of the variable
    is replaced by its value (coerced to a fixed-width integer) within
    an expression.  The variable need not have its integer attribute
    turned on to be used in an expression.
    
    Operators are evaluated in order of precedence.  Sub-expressions in
    parentheses are evaluated first and may override the precedence
    rules above.
    
    Exit Status:
    If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.

[root@rocky8 bin]# echo $x
10
[root@rocky8 bin]# echo $y
20
[root@rocky8 bin]# let x+y
[root@rocky8 bin]# let z=x+y
[root@rocky8 bin]# echo $z
30

[root@rocky8 bin]# let z=x*y
[root@rocky8 bin]# echo $z
200

[root@rocky8 bin]# i=10
[root@rocky8 bin]# let i++ #i + + is i=i+1
[root@rocky8 bin]# echo $i
11

[root@rocky8 bin]# j=10
[root@rocky8 bin]# let ++j
[root@rocky8 bin]# echo $j
11
[root@rocky8 bin]# exit
logout

[root@rocky8 ~]# i=10
[root@rocky8 ~]# let j=i++ #First assign the value of i to j, and then i++
[root@rocky8 ~]# echo $j
10
[root@rocky8 ~]# echo $i
11

[root@rocky8 ~]# unset i j
[root@rocky8 ~]# i=10
[root@rocky8 ~]# let j=++i #First add up the value of i, and then assign it to j
[root@rocky8 ~]# echo $j
11
[root@rocky8 ~]# echo $i
11

[root@rocky8 ~]# let i+=2 #i+=2 is i=i+2
[root@rocky8 ~]# echo $i
13

[root@rocky8 ~]# ((++i))
[root@rocky8 ~]# echo $i
14
#(()) two parentheses also support numeric operations

[root@rocky8 ~]# expr 1 + 2
3
[root@rocky8 ~]# expr 1 * 2
expr: syntax error: unexpected argument 'anaconda-ks.cfg' #The multiplication sign needs to be escaped
[root@rocky8 ~]# expr 1 \* 2
2

example:

#Generate random numbers between 0 and 49
[root@rocky8 bin]# echo $[$RANDOM%50]
10
#Generate random numbers between 0 and 70
[root@rocky8 ~]# echo $[RANDOM%70+1]
68
[root@rocky8 ~]# echo $[RANDOM%70+1]
39
[root@rocky8 ~]# echo $[RANDOM%70+1]
8

[root@rocky8 ~]# echo $[RANDOM%7+31]
37
[root@rocky8 ~]# echo $[RANDOM%7+31]
31
[root@rocky8 ~]# echo $[RANDOM%7+31]
35


[root@rocky8 bin]# echo -e "\033[1;$[RANDOM%7+31]mhello\033[0m"
hello

example:

[root@rocky8 bin]# vim backup.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      backup.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
N=$[RANDOM%7+31]
COLOR="\E[1;${N}m"
COLOR_END='\E[0m'
SRC=/etc
DEST=/data

echo -e ${COLOR}Starting backup...${COLOR_END}
sleep 2
cp -av $SRC $SRC$DEST`date +%F_%H-%M-%S`
echo -e ${COLOR}Backup is finished${COLOR_END}

example:

[root@rocky8 bin]# N=$[RANDOM%7+31]
[root@rocky8 bin]# echo -e "\033[$N;1mtest\033[0m"
test
[root@rocky8 bin]# echo -e "\033[$N;1mtest\033[0m"
test
[root@rocky8 bin]# N=$[RANDOM%7+31]
[root@rocky8 bin]# echo -e "\033[$N;1mtest\033[0m"
test
[root@rocky8 bin]# N=$[RANDOM%7+31]
[root@rocky8 bin]# echo -e "\033[$N;1mtest\033[0m"
test

#Random font color
[root@rocky8 bin]# echo -e "\033[1;$[RANDOM%7+31]mhello\033[0m"
hello


Enhanced assignment:

+= i+=10 amount to i=i+10
-= i-=j amount to i=i-j
*=
/=
%=
++ i++,++i amount to i=i+1
-- i--,--i amount to i=i-1

Format:

let varOPERvalue

example:

[root@rocky8 bin]# let i=10*2
[root@rocky8 bin]# echo $i
20
[root@rocky8 bin]# ((j=i+10))
[root@rocky8 bin]# echo $j
30

example:

#Self assignment after adding 3
let count+=3

[root@rocky8 bin]# i=10
[root@rocky8 bin]# let i+=20
[root@rocky8 bin]# echo $i
30
[root@rocky8 bin]# j=20
[root@rocky8 bin]# let i*=j
[root@rocky8 bin]# echo $i
600

example:

#Self increasing and self decreasing
let var+=1
let var++
let var-=1
let var--

[root@rocky8 bin]# unset i j ; i=1; let j=i++; echo "i=$i,j=$j"
i=2,j=1
[root@rocky8 bin]# unset i j ; i=1; let j=++i; echo "i=$i,j=$j"
i=2,j=2

example:

[root@rocky8 bin]# expr 2 * 3
expr: syntax error: unexpected argument 'arg.sh'
[root@rocky8 bin]# expr 2 \* 3
6

example:

[root@rocky8 bin]# echo "scale=3;20/3"|bc
6.666

example:

[root@rocky8 bin]# i=10
[root@rocky8 bin]# j=20
[root@rocky8 bin]# declare -i result=i*j
[root@rocky8 bin]# echo $result
200

Example: now there are pheasant rabbits in the same cage. There are 35 heads on the top and 94 feet on the bottom. How about pheasant rabbits?

[root@rocky8 bin]# vim chook_rabbit.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      chook_rabbit.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
HEAD=$1
FOOT=$2

((RABBIT=(FOOT-HEAD-HEAD)/2))
((CHOOK=HEAD-RABBIT))
#RABBIT=$(((FOOT-HEAD-HEAD)/2))
#CHOOK=$[ HEAD-RABBIT ]

echo RABBIT:$RABBIT
echo CHOOK:$CHOOK

[root@rocky8 bin]# bash chook_rabbit.sh 30 80
RABBIT:10
CHOOK:20

2.10 logic operation

true, false

1 true
0 false

example:

[root@rocky8 bin]# echo $?
0
[root@rocky8 bin]# xxx
-bash: xxx: command not found
[root@rocky8 bin]# echo $?
127
#Command execution success is true, and execution failure is false.

[root@rocky8 bin]# true
[root@rocky8 bin]# echo $?
0
[root@rocky8 bin]# false
[root@rocky8 bin]# echo $?
1

[root@rocky8 bin]# type true
true is a shell builtin
[root@rocky8 bin]# help true
true: true
    Return a successful result.
    
    Exit Status:
    Always succeeds. #Forever true
[root@rocky8 bin]# type false
false is a shell builtin
[root@rocky8 bin]# help false
false: false
    Return an unsuccessful result.
    
    Exit Status:
    Always fails. #Forever false

And &: sum with 0, the result is 0, and sum with 1. The result retains the original value. Only if it is true, it is true. As long as one is false, the result is false

1 And 1 = 1
1 And 0 = 0
0 And 1 = 0
0 And 0 = 0

example:

[root@rocky8 bin]# echo $[8&4]
0

Or |: and 1 phase or result is 1, and 0 phase or, the result retains the original value. As long as one is true, the result is true. Only if both are false, the result is false

1 Or 1 = 1
1 Or 0 = 1
0 Or 1 = 1
0 Or 0 = 0

example:

[root@rocky8 bin]# echo $[8|4]
12

Non:!

! 1 = 0 ! true
! 0 = 1 ! false

XOR:^

Two values of XOR, the same is false and the different is true. Two numbers X and Y XOR get the result Z, Z, and then XOR with any one of them will get another value y

1 ^ 1 = 0
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1

example:

[root@rocky8 bin]# type
[root@rocky8 bin]# echo $?
0
[root@rocky8 bin]# false
[root@rocky8 bin]# echo $?
1
[root@rocky8 bin]# ! type
[root@rocky8 bin]# echo $?
1
[root@rocky8 bin]# ! false
[root@rocky8 bin]# echo $?
0

[root@rocky8 bin]# x=10;y=20;temp=$x;x=$y;y=$temp;echo x=$x,y=$y
x=20,y=10
[root@rocky8 bin]# x=10;y=20;x=$[x^y];y=$[x^y];x=$[x^y];echo x=$x,y=$y
x=20,y=10

Short circuit operation

  • Short circuit and&&
CMD1 Short circuit and CMD2
 first CMD1 The result is true (1),the second CMD2 You must participate in the operation to get the final result
 first CMD1 The result is false (0),The total result must be 0, so no execution is required CMD2
  • Short circuit or||
CMD1 Short circuit or CMD2
 first CMD1 The result is true (1),The total result must be 1, so no execution is required CMD2
 first CMD1 The result is false (0),the second CMD2 Must participate in the operation,To get the final result

Posted by phynias on Fri, 22 Oct 2021 17:11:27 -0700