Exercise: Write a script
Determine if there is a user's default shell for bash on the current system;
If so, the number of such users is displayed, otherwise, no such user is shown;
#!/bin/bash#grep '\<bash$'/etc/passwd&>/dev/NULLRETVAL=$?if[$RETVAL-eq0]; ThenUSERS=`grep"\<bash$ "/etc/passwd|WC-l 'Echo "hava $USERS USERS Use default bash"Else Echo "no users using bash by default"
Fi
Exercise: Write a script
Determine if there is a user's default shell for bash on the current system;
If so, displays one of the user names; otherwise, no such user is displayed;
#!/bin/bash#grep '\<bash$'/etc/passwd&>/dev/NULLRETVAL=$?if[$RETVAL-eq0]; ThenUSER=`grep"\<bash$ "/etc/passwd|Head-1|Cut-D:-F1 'Echo "user $USER Use default bash"Else Echo "no users using bash by default"fi
Exercise: Write a script
Given a file, such as/etc/inittab
Determine if there is a blank line in this file;
If so, the number of blank lines is displayed, otherwise, no blank lines are displayed.
#!/bin/Bashgrep "^$"/etc/inittab &>/dev/NULLRETVAL=$?if[$RETVAT-eq0]; Then Echo "No Blank lines"ElseNUM=`grep "^$"/etc/inittab |WC-l 'Echo "There are $num blank lines"
Fi
Exercise: Write a script
Given a user, determine if the UID is the same as the GID
If it does, the user is displayed as "good guy", otherwise the user is shown as "bad guy".
#!/bin/bash#username="user1"USERID= 'ID -u $USERNAME ' GROUPID= 'ID -g $USERNAME 'ifthenecho'good guy"elseecho"bad guy"fi
#!/bin/Bash#username=User1userid=`grep "^ $USERNAME \>"/etc/passwd|Cut-D:-f3 ' GROUPID=`grep "^ $USERNAME \>"/etc/passwd|Cut-D:-F4 'if[Userid-eq GROUPID]; ThenEcho "good guy."Else Echo " bad guy"fi
How to perform arithmetic operations in the shell
A=3
B=6
1. Let arithmetic expressions
Let c= $A + $B
2. $[Arithmetic expression]
c=$[$A + $B]
3. $ (())
c=$ (($A + $B))
4. Expr arithmetic expression, with spaces between the operands and operators in the expression, and using the command reference
c= ' expr $A + $B '
Exit: Exit Script
Exit #
The error code can be 1-255, or it can be omitted.
Exercise: Write a script
Given a user, get their password warning period;
And then determine whether the user password period is less than the warning period;
Tip: The calculation method, the maximum period of use minus the number of days that have been used is the remaining period of use;
If it is less than, "Warning" is displayed; otherwise, "OK" is displayed.
Rounding: Discard all content after the decimal point #!/bin/bash#
#!/bin/bash
USER=Root
Id-u $USER &>/dev/null
retval=$?
If [$RETVAL-eq 0]; Thenwarndate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f6 ' Latestdate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f3 ' MaxDate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f5 ' MILLS=`Date+%s ' nowdateused=`Expr$MILLS/86400' SY=$[$MAXDATE-$[$NOWDATEUSED-Latestdate]]if[$SY-gt $WARNDATE]; Then Echo "OK"Else Echo "Warning"fi
Else
Exit
Fi
#!/bin/Bashuser=Rootif!grep "^ $USER \>"/etc/shadow; ThenExit1fiwarndate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f6 ' Latestdate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f3 ' MaxDate=`grep "^ $USER \>"/etc/shadow |Cut-D:-f5 ' MILLS=`Date+%s ' nowdateused=`Expr$MILLS/86400' SY=$[$MAXDATE-$[$NOWDATEUSED-Latestdate]]if[$SY-gt $WARNDATE]; Then Echo "OK"Else Echo "Warning"fi
Exercise: Write a script
Determines whether the total entry for history commands in the command history is greater than 1000, or if greater than, displays "Some command will gone." Otherwise, "OK" is displayed.
Linux--shell Programming (three) conditional judgment and arithmetic operation