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
Acount= ' grep ' \<bash$ '/etc/passwd |wc-l '
If [$acount-gt 0];then
echo "There has $acount such users"
Else
echo "No such users"
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
I= ' grep ' ^$ '/etc/inittab|wc-l '
If [$i-gt 0];then
echo "There is $i blank lines"
Else
echo "No 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";
[' Id-u user1 '-eq ' id-g user1 ']&& echo ' good guy ' | | echo "Bad guy"
Exercise: Write a script
Given a user, get the password warning period;
And then determine whether the user last modified password time is less than the maximum age of the warning period;
If less than. "Warning" is displayed; otherwise, "OK" is displayed;
Tip: The method of arithmetic operation $[$A-$B], which represents the value of variable A minus the value of variable B
#!/bin/bash
if! ID $ &>/dev/null;then
echo "User $ no exists!"
Else
Wtime= ' grep ' $ '/etc/shadow|cut-d:-f6 '
Ctime= ' grep ' $ '/etc/shadow|cut-d:-f3 '
Utime= ' grep ' $ '/etc/shadow|cut-d:-f5 '
now= ' Date +%s/86400|bc '
ltime=$[$utime-$[$now-$ctime]
If [$ltime-lt $wtime];then
echo "warning!"
Else
echo "OK"
Fi
Fi
Exercise: Write a script
What is the total entry for the history command in the decision command history?
[Email protected] ~]# history |tail-1|cut-d '-f3
568
Note: It is not possible to use the history |wc-l to calculate, because the historical command can store the size of the environment variable histsize, and overflow when it exceeds, our topic means to count the historical commands that have been used.
How to perform arithmetic operations in the shell:
A=3
B=7
1. Let arithmetic expression
Let c= $A + $B
2. $[arithmetic operation expression]
$[$A + $B]
3, $ (arithmetic operation expression)
$ (($A + $B))
4. Expr arithmetic expression, the action in the expression and the operator must have a space between, and the assignment requires a command reference
c= ' expr $A + $B '
This article is from the "single Season rice" blog, please be sure to keep this source http://linzb.blog.51cto.com/5192423/1732473
Bash's three script exercises and arithmetic operations