shell作业

1、判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”
#!/bin/bash
line=`wc -l /etc/inittab |cut -d" " -f1`
if [$line -gt 100];then
echo "/etc/inittab is a small file"
else
echo "/etc/inittab is a small file"
fi
2、给定一个用户,来判断这个用户是什么用户,如果是管理员用户,则显示“该用户为管理员”,否则显示“该用户为普通用户”
#!/bin/bash
userid="id -u $1"
if [userid -eq 0];then
echo "admin"
else
echo "common user"
fi
3、判断某个文件是否存在
#!/bin/bash
read -p '请输入文件' a
if [ -e $a ];then
echo "文件存在"
else
echo "文件不存在"
fi
4、判断当前系统上是否有用户的默认shell程序是否为bash程序,如果有,就显示有多个这类用户,否则就显示没有这类用户;【并且显示出那些用户是bash】
#!/bin/bash
BASHLINE=`grep "bash$" /etc/passwd | wc -l`
if [ $BASHLINE -eq 0 ];then
echo "没有这类用户"
else
echo "有这类用户,有 $BASHLINE 个."
echo `grep 'bash$' /etc/passwd | cut -d":" -f1`
fi
5、写出一个脚本程序,给定一个文件,比如:/etc/inittab a、判断这个文件中是否有空白行? b、如果有,则显示其空白行的行号,否则显示没有空白行
#!/bin/bash
read -p '请输入一个文件' a
kongbai=`grep ^$ $a |wc -l`
if [ $kongbai -eq 0 ];then
echo "没有空白行"
else
echo "有空白行"
echo `grep -n '^$' $a`
fi
6、写一个脚本程序,给定一个用户,判断其UID与GID是否一样,如果一样,就显示该用户为“good guy”,否则显示为“bad guy”
#!/bin/bash
read -p '请输入一个用户' a
userid=`id -u $a`
grpid=`id -g $a`
if [ $userid -eq $grpid ];then
echo "good guy"
else
echo "bad guy"
fi
7、写一个脚本程序,给定一个用户,获取其密码警告期限;然后判断用户最近一次修改密码的时间距离今天是否已经小于警告期限;
#!/bin/bash
w=`grep "root" /etc/shadow | cut -d":" -f6`
s=`date +%s`
t=`expr $s/86400`
l=`grep "^root" /etc/passwd |cut -d":" -f5`
n=`grep "^root" /etc/passwd |cut -d":" -f3`
sy=$[ $l - $[ $t - $n ] ]
if [ $sy -gt -$w ];then
echo "Woring"
else
echo "OK"
fi
8、判断命令历史中历史命令的总条目是否大于1000,如果大于,则显示“some command will gone”,否则显示OK
#!/bin/bash
his=`history | wc -l`
if [ $his -ge 1000 ];then
echo "Some command will gone."
else
echo "ok"
fi
9、给定一个文件,如果是普通文件,就显示出来,如果是目录文件,也显示出来,否则就显示“无法识别”
#!/bin/bash
read -p'请输入一个文件名称:' a
if [ ! -e $a ];then
echo "No such file"
exit 6
fi
if [ -f $a ];then
echo "Common file."
elif [ -d $a ];then
echo "Directory."
else
echo "Unknown."
fi
10、写一个脚本,能接受一个参数(文件路径),判断这个参数如果是一个存在的文件就显示“ok”,否则显示“No such file”
#!/bin/bash
read -p '请输入文件路径:' a
if [ -e $a ];then
echo "ok"
else
echo "No such file"
fi
11、写一个脚本,给脚本传递两个参数,显示两则之和和两者之积
#!/bin/bash
read -p'请输入第一个参数:' a
read -p'请输入第二个参数:' b
echo "两数之和为:$[ $a+$b ]"
echo "两数之积为:$[ $a*$b ]"

猜你喜欢

转载自www.cnblogs.com/lyali/p/11346647.html