7.设计一个shell程序,计算n的阶乘,要求:1)从命令行接收参数n; 2)程序运行后判断参数n的合法性,即是否有参数,如有,是否为正整数;若非法,给出错误提示信息。3)最后输出计算结果。
代码: #!/bin/bash
multiply() {
if [ $1 -gt 1 ]; then
result=`expr $result \\* $1` next=`expr $1 - 1` multiply $next fi }
if [ $# -ne 1 ]; then
echo -e \ exit 1 fi
result=1
if [ $1 -eq 1 ]; then :
elif [ $1 -gt 1 ]; then multiply $1 else
echo \ exit 1 fi
echo \
exit 0
8.编写Shell程序,程序功能,先建立一个学生组student,然后批量建立若干个学生用户,用户名的前导字符形如stu,再加数字序号,如stu20,用户名的前导字符和用户个数通过参数传递。格式如:mysh stu 100 。 代码: #!/bin/bash
if [ $# -eq 2 ];then a=$1 b=$2 c=$a$b
groupadd student
useradd $c
echo \Fi
9.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
(1)在下午4 :50删除/abc目录下的全部子目录和全部文件;
(2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中的全部数据并追加到/backup目录下的bak01.txt文件内;
(3)每逢星期一下午5:50将/data目录下的所有目录和文件归档并压缩为文件:backup.tar.gz;
请编写一个crontab文件,实现上述方案。 Vim /etc/crontab
50 16 * * * root rm -rf /abc/* 2>&1 & 00 8-18 * * * root cat /xyz/x1|awk '{print $1}' >> /backup/bak01.txt 2>&1 & 50 17 * * 1 root cd /data;tar -zcvf backup.tar.gz * 2>&1 & 55 17 * * * root umount /hdc 2>&1 &
10. 考勤模拟Shell程序设计: 用shell设计一个模拟考勤程序,实现如下功能选择界面:
1:上班签到
2:下班签出
3:考勤信息查询 考勤程序运行后,提示用户输入上述功能选择,并验证用户输入的用户名和密码;用户信息保存在userinfo.dat中。如果是上班签到,记录签到信息,如果签到时间大于上午8时,则提示用户迟到,并记录该迟到信息到check.dat中。如果是下班签出,记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到check.dat。如果用户选择确信信息查询,则将check.dat中对应的用户迟到早退的信息查询出来并显示。用户选择功能执行完,Shell程序继续回到功能选择界面等待下一个用户进行操作。
代码: #!/bin/bash while [ 1 ] do
#clear
#屏幕显示提示信息
echo \欢迎使用本系统********\ echo \录入签到\ echo \下班签出\ echo \查询 \
echo \ echo \请输入您的选项:\
#读入操作选项 read choice
#对相应的选项进行操作
case $choice in
1) echo \请输入您的名字:\ read name
echo \请输入密码:\ read password
if test -r userinfo.dat then
while read fname fpassword do
if test \ then
if test \ then break fi fi
done < userinfo.dat else
echo System Error : no such file userinfo.dat fi
if test \ then
echo \不存在该用户!\
elif test \ then
echo \密码不正确!\ else
hour=`date +%H` if test \ then
echo \您迟到了!\
echo \迟到了 ---日期:`date`\ else
echo \早上好!\ fi fi;;
2) echo \请输入您的名字:\ read name
echo \请输入密码:\ read password
if test -r userinfo.dat then
while read fname fpassword do
if test \ then
if test \ then break fi fi
done < userinfo.dat else
echo System Error : no such file userinfo.dat fi
if test \ then
echo \不存在该用户!\
elif test \ then
echo \密码不正确!\