
Linux下Shell编程问题
题目要求:有文本文件student.txt,该文件是所有学生的信息,每个学生存储一行信息,信息格式如下:学号:姓名:性别:专业如(101:张三:男:计算机)设计一个she...
题目要求:有文本文件student.txt,该文件是所有学生的信息,每个学生存储一行信息,信息格式如下:学号:姓名:性别:专业 如(101:张三:男:计算机)设计一个shell,名称为student.sh,该shell完成如下功能:1)当输入 student.sh -a 101:张三:男:计算机 时,首先判断101记录是否存在,如果不存在,则把该信息写入文件,如果存在,则给出提示,并输出文件中学号为101的该行信息2)当输入 student.sh -d 101 时,首先判断101记录是否存在,如果不存在,给出提示,如果存在,则提示用户确认是否要删除记录,如用户输入y或者yes,则删除文件中学号为101的该行信息,如果用户输入n或no时,则不做删除操作3)当输入 student.sh -s 101 时,首先判断101记录是否存在,如果不存在,给出提示,如果存在,则输出文件中学号为101的该行信息4)当输入 student.sh时,输出文件的全部内容5)当用户输入的选项不正确时,给出错误提示,并输入该shell的用法,如(student.sh -q 123456)
展开
3个回答
2013-07-14
展开全部
很简单嘛#!/bin/bash if { $# == 0} then cat student.txtelseif { $#==2 && $1 == "\-a" } if {`cat student.txt |wc -l ` > ="$2"} then { echo " exist" cat student.txt | sed -n '(($2))p' } else echo " not exist"elseif {$#==2 && $1 == "\-d"} if { `cat student.txt |wc -l ` >="$2"} then { echo " delete or not ?input yes or y or no or n" read character if { $charecter == "yes" || $charecter =="y"} cat student.txt | sed -N '$2'p } else exit (0) elseif { "$2" == "\-s " && $#==2} if { `cat student.txt |wc -l ` >="$2"} then { echo " exist " cat student.txt |sed -n '$2'p } else { echo "not exist" exit (1) }else echo " Useage:basename $0 parameter1 parameter2"fi
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2024-08-19 广告
ForceSCADA是力控科技信创产品的重要组成部分,具备完全自主知识产权,支持部署在Linux桌面版、服务器版、嵌入式等系统架构下。使用ForceSCADA可以搭建创新性高、扩展性佳、融合度强的SCADA平台,进而构建高效、智能化的监控中...
点击进入详情页
本回答由力控科技提供
2013-07-14
展开全部
#!/bin/bash
_stfile=/root/test/student.txt
usage ()
{
echo "Usage: ./`basename $0` {-a|d|s} number"
}
if [ $# -eq 0 ];then
cat ${_stfile}
else
case $1 in
-a)
if [ "`cat ${_stfile} | grep -c $2`" != "1" ];then
echo "The student's information is added!"
echo $2 >> ${_stfile}
elif [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
echo "The student's information as follow:"
cat ${_stfile} | grep $2
fi
;;
-d)
if [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
read -p "You want to delete the information:[y/n]" DEL
case $DEL in
y|yes)
sed -i "/$2/d" ${_stfile}
;;
n|no)
exit 0
;;
esac
else
echo "The student is not exist!"
fi
;;
-s)
if [ "`cat ${_stfile} | grep -c $2`" != "1" ];then
echo "The student is not exist!"
elif [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
echo "The student's information as follow:"
cat ${_stfile} | grep $2
fi
;;
*)
usage
;;
esac
fi
_stfile=/root/test/student.txt
usage ()
{
echo "Usage: ./`basename $0` {-a|d|s} number"
}
if [ $# -eq 0 ];then
cat ${_stfile}
else
case $1 in
-a)
if [ "`cat ${_stfile} | grep -c $2`" != "1" ];then
echo "The student's information is added!"
echo $2 >> ${_stfile}
elif [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
echo "The student's information as follow:"
cat ${_stfile} | grep $2
fi
;;
-d)
if [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
read -p "You want to delete the information:[y/n]" DEL
case $DEL in
y|yes)
sed -i "/$2/d" ${_stfile}
;;
n|no)
exit 0
;;
esac
else
echo "The student is not exist!"
fi
;;
-s)
if [ "`cat ${_stfile} | grep -c $2`" != "1" ];then
echo "The student is not exist!"
elif [ "`cat ${_stfile} | grep -c $2`" = "1" ];then
echo "The student's information as follow:"
cat ${_stfile} | grep $2
fi
;;
*)
usage
;;
esac
fi
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-14
展开全部
我觉得你这个怎么那么像是学生作业呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询