1、查询一个文件中空白行行数。
#!/bin/bash
FILE=/etc/inittab
if [ ! -e $FILE ];then
echo "No $FILE"
exit 8
fi
if grep "^$" $FILE &> /dev/null;then
echo "Total blank line:`grep "^$" $FILE |wc -l`."
else
echo "No blank line."
fi
2、判断一个用户的UID和GID是否相等
#!/bin/bash
USERNAME=user1
if ! grep "^$USERNAME\>" /etc/passwd &> /dev/null;then
echo "No such user: $USERNAME."
exit 1
fi
USERID=`id -u $USERNAME`
GROUPID=`id -g $USERNAME`
if [ $USERID -eq $GROUPID ];then
echo "Good guy."
else
echo "Bad guy."
fi
3、判断用户密码是否过期
#!/bin/bash
#
USERNAME=user1
`id $USERNAME &> /dev/null`
M=$?
if [ ! $M -eq 0 ];then
echo "No such user: $USERNAME"
exit 1
fi
W=`grep "^$USERNAME" /etc/shadow |cut -d: -f6`
S=`date +%s`
T=`expr $S / 86400`
L=`grep "^$USERNAME" /etc/shadow |cut -d: -f5`
N=`grep "^$USERNAME" /etc/shadow |cut -d: -f3`
H=`expr $T - $N`
U=`expr $L - $H`
if [ $U -lt $W ];then
echo "Worning."
else
echo "ok."
fi
注意:可以使用bash -x XX.sh进行测试程序执行
5.多分支判断指定文件的类型
#!/bin/bash
#
FILE=/etc/passwd
if [ ! -e $FILE ];then
echo "No such file."
fi
if [ -f $FILE ];then
echo "common file."
elif [ -f $FILE ];then
echo "Directory."
else
echo "Unknow."
fi