5.4
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
Nano dd.sh
#!/bin/bash
#
grep "\<bash$"/etc/passwd &>/dev/null
RETVAL = $?
If [$RETVAL-eq 0];then
USERS = ' grep ' \<bash$ '/etc/passwd | Wc-l '
echo "The shells of $USERS USERS is bash."
Else
echo "No such user."
Fi
Exercise: Write a script
Determine if a user's default shell is bash on the current system
If so, displays one of the user names, otherwise, it shows no such user
Nano dd.sh
#!/bin/bash
#
grep "\<bash$"/etc/passwd &>/dev/null
RETVAL = $?
If [$RETVAL-eq 0];then
USERS = ' grep ' \<bash$ '/etc/passwd | head-1 | cut-d:-f1 '
echo "$USERS is one of such USERS"
Else
echo "No such user."
Fi
Exercise: Write a script
Given a file, such as/etc/inittab
Determine if there is a blank line in this file
If so, displays the number of blank lines, otherwise, no blank lines are displayed
#!/bin/bash
FILE =/etc/inittab
If grep "^$" $FILE &>/dev/null;then
echo "Total Blank lines: ' grep" ^$ "$FILE | Wc-l '. "
Else
echo "No blank line."
Fi
Exercise: Write a script
Given a user, determine if its UID and GID are the same
If it does, the user is displayed as "good guy";
#! /bin/bash
USERNAME =user1
USERID = ' Id-u $USERNAME '
GROUPID = ' Id-g $USERNAME '
if[$USERID-eq $GROUPID];then
echo "Good guy"
Else
echo "Bad guy"
Fi
Further requirements: Do not use the ID command to obtain its ID number
Exercise: Writing a script
Given a user, get their password warning period:
And then determine whether the last time the user changed the password is today less than the warning period
Hint: The method of arithmetic operation $[$A-$B]: The result of the value of variable A minus the value of variable B
If it is less than, "Warning" is displayed: otherwise, "OK" is displayed
Exercise: Write a script
The total entry of the history command in the Interpretation command history is greater than 1000;
"Some command would gone."; Otherwise, "OK" is displayed
How to perform arithmetic operations in the shell:
A = 3
B = 6
1 Let arithmetic op-expression
Let C = $A + $B
2 $[arithmetic operation expression]
c = $[$A + $B]
3 $ ((arithmetic expression))
c = $ (($A + $B))
4 The Expr command operation expression, with spaces between the operands and operators in the expression, and
Using a command reference
c = ' expr $A + $B '
5.4shell Programming 3