Little exercise: Write a script
Determine if a user's default shell is bash on the system
If there is one, it shows how many such users will otherwise show no such user
#!/bin/bash
#
grep "bash$"/etc/passwd &>/devnull
Code=$?
If [$CODE-eq 0]; Then
usernu= ' grep ' bash$ '/etc/passwd | Wc-l '
echo "The number of Bash shell user is $USERNU" English not well don't know right hehe
Else
echo "The system is not a user of bash shell." English is not very good don't know right hehe
Fi
Script Execution Results
[Email protected] ~]#./first.sh
The number of Bash shell user is 8
Exercise two:
To see if the system has a user's default shell is bash
There is no indication that one of them is displayed on the system.
scripting, similar to the previous script
#!/bin/bash
#
grep "bash$"/etc/passwd &>/devnull
Code=$?
If [$CODE-eq 0]; Then
usernu= ' grep ' bash$ '/etc/passwd | head-n1 | cut-d:-f1 '
echo "$USERNU is one of bash shell users."
Else
echo "The system is not a user of bash shell."
Fi
Script Execution Results:
[Email protected] ~]#
[Email protected] ~]#./first.sh
Root is one of the bash shell users.
[Email protected] ~]#
How to perform arithmetic operations in the shell (note: The default shell treats variables as characters and does not perform arithmetic operations)
Cases:
[Email protected] ~]# a=3
[Email protected] ~]# b=6
[Email protected] ~]# c= $A + $B
[Email protected] ~]# echo $C
3+6
[Email protected] ~]#
If you want it to do arithmetic operations:
The first way: use let plus the variable to be operated on
Cases:
[Email protected] ~]# a=3
[Email protected] ~]# b=6
[[email protected] ~]# let c= $A + $B
[Email protected] ~]# echo $C
9
[Email protected] ~]#
Second way: Use $[arithmetic expression]
Cases:
[Email protected] ~]# a=25
[Email protected] ~]# b=88
[Email protected] ~]# c=$[$A + $B]
[Email protected] ~]# echo $C
113
[Email protected] ~]#
Third Way: Using $ ((arithmetic expression))
Cases:
[Email protected] ~]# a=222
[Email protected] ~]# b=381
[[email protected] ~]# c=$ (($A + $B))
[Email protected] ~]# echo $C
603
[Email protected] ~]#
Fourth way: Use expr arithmetic expressions Note that there are spaces between the operands in the expression, and that you want to use a command reference
Cases:
[Email protected] ~]# a=3244
[Email protected] ~]# b=38234
[[email protected] ~]# c= ' expr $A + $B ' note to use a space to separate the HA
[Email protected] ~]# echo $C
41478
[Email protected] ~]#
Bash Scripting 3 conditional judgments and arithmetic operations (notes)