shell做业及答案

一、判断/etc/inittab文件是否大于100行,若是大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”shell

#!/bin/bash
a=` wc -l /etc/inittab|cut -d ' ' -f1`
if [ $a -gt 100 ];then
echo '/etc/inittab is a big file.'
else
echo '/etc/inittab is small file.'
fibash

二、给定一个用户,来判断这个用户是什么用户,若是是管理员用户,则显示“该用户为管理员”,不然显示“该用户为普通用户”
# !/bin/bashit

userid=`id -u $1`
if [ $userid -eq 0 ];then
echo $1'该用户为管理员.'
else
echo $1'该用户为普通用户.'
fiawk

三、判断某个文件是否存在
#!/bin/bash
if [ ! -d "qwer" ]; then
echo '文件不存在'
mkdir qwer
else
echo '文件存在'
fidate

四、判断当前系统上是否有用户的默认shell程序是否为bash程序,若是有,就显示有多个这类用户,不然就显示没有这类用户;【而且显示出那些用户是bash】
#!/bin/bash
grep "\<bash$" /etc/passwd &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
USERS=`grep "\<bash$" /etc/passwd | wc -l`
echo "$USERS users has shell of bash"
else
echo "no such users"
fifile

五、写出一个脚本程序,给定一个文件,好比:/etc/inittab a、判断这个文件中是否有空白行? b、若是有,则显示其空白行的行号,不然显示没有空白行
#!/bin/bash
A=`grep '^$' /etc/inittab | wc -l`
if [ $A -gt 0 ]; then
echo "$A有空白行"
else
echo "没有空白行"
fi
六、写一个脚本程序,给定一个用户,判断其UID与GID是否同样,若是同样,就显示该用户为“good guy”,不然显示为“bad 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
七、写一个脚本程序,给定一个用户,获取其密码警告期限;而后判断用户最近一次修改密码的时间距离今天是否已经小于警告期限;
#!/bin/bash
W=`grep "student" /etc/shadow | cut -d: -f6`
S=`date +%s`
T=`expr $S/86400`
L=`grep "^student" /etc/shadow | cut -d: -f5`
N=`grep "^student" /etc/shadow | cut -d: -f3`
SY=$[$L-$[$T-$N]]grep

if [ $SY -lt $W ]; then
echo 'Warning'
else
echo 'OK'
fi
八、判断命令历史中历史命令的总条目是否大于1000,若是大于,则显示“some command will gone”,不然显示OK
#!/bin/bash
A=`history |wc -l`
if [ $A -gt 1000 ];then
echo "some command will gone."
else
echo "OK"
if程序

九、给定一个文件,若是是普通文件,就显示出来,若是是目录文件,也显示出来,不然就显示“没法识别”
#!/bin/bash
a=`-`
b=`d`
c=`ll yum.conf|cut -d ' ' -f1 |awk -F '' '{print $1}'`
if [ $c -eq $a ];then
echo "普通文件"
elif [ $c -eq $b ];then
echo "目录文件"
else
echo "没法识别"
ficommand

十、写一个脚本,能接受一个参数(文件路径),判断这个参数若是是一个存在的文件就显示“ok”,不然显示“No such file”
#!/bin/bash
A=` ll /etc/inittab|wc -l`----------------->1
if [ $A -ge 1 ];then
echo "ok"
else
echo "NO such file"
fi密码

十一、写一个脚本,给脚本传递两个参数,显示两则之和和二者之积#!/bin/basha=2b=3if [ $sum -eq '+'];thenecho "二者之和为:$[$a+$b]"elseecho "二者之积为:$[$a*$b]"fi

相关文章
相关标签/搜索