标签:shell习题
shell习题-抽签脚本
1、写一个脚本执行后,输入名字,产生随机数01-99之间的数字。
2、如果相同的名字重复输入,抓到的数字还是第一次抓取的结果,
3、前面已经抓到的数字,下次不能在出现相同数字。
4、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出
继续等待别的学生输入。
参考答案
while : do read -p "Please input a name:" name if [ -f /work/test/1.log ];then bb=`cat /work/test/1.log | awk -F: '{print $1}' | grep "$name"` if [ "$bb" != "$name" ];then #名字不重复情况下 aa=`echo $RANDOM | awk -F "" '{print $2 $3}'` while : do dd=`cat /work/test/1.log | awk -F: '{print $2}' | grep "$aa"` if [ "$aa" == "$dd" ];then #数字已经存在情况下 echo "数字已存在." aa=`echo $RANDOM | awk -F "" '{print $2 $3}'` else break fi done echo "$name:$aa" | tee -a /work/test/1.log else aa=`cat /work/test/1.log | grep "$name" | awk -F: '{print $2}'` #名字重复 echo $aa echo "重复名字." fi else aa=`echo $RANDOM | awk -F "" '{print $2 $3}'` echo "$name:$aa" | tee -a /work/test/1.log fi done
shell习题-猜数字
写一个猜数字脚本,当用户输入的数字和预设数字(随机生成一个小于100的数字)一样时,直接退出,否则让用户一直输入,并且提示用户的数字比预设数字大或者小。
参考答案:
#!/bin/bash m=`echo $RANDOM` n1=$[$m%100] while : do read -p "Please input a number: " n if [ $n == $n1 ] then break elif [ $n -gt $n1 ] then echo "bigger" continue else echo "smaller" continue fi done echo "You are right."
shell习题-下载文件
创建一个函数,能接受两个参数:
1)第一个参数为URL,即可下载的文件;第二个参数为目录,即下载后保存的位置;
2)如果用户给的目录不存在,则提示用户是否创建;如果创建就继续执行,否则,函数返回一个51的错误值给调用脚本;
3)如果给的目录存在,则下载文件;下载命令执行结束后测试文件下载成功与否;如果成功,则>返回0给调用脚本,否则,返回52给调用脚本;
提示,在函数中返回错误值给调用脚本,使用return
参考答案:
#!/bin/bash if [ ! -d $2 ] then echo "please make directory" exit 51 fi cd $2 wget $1 n=`echo $?` if [ $n -eq 0 ];then exit 0 else exit 52 fi
shell习题-列出子目录
脚本的功能:
脚本可以带参数也可以不带,参数可以有多个,每个参数必须是一个目录,脚本检查参数个数,若等于0,则列出当前目录本身;否则,显示每个参数包含的子目录。
参考答案
#!/bin/bash if [ $# == 0 ] then ls -ld `pwd` else for i in `seq 1 $#` do a=$i echo "ls ${!a}" ls -l ${!a} |grep '^d' done fi
标注: 你可能会对${!a}有疑问,这里是一个特殊用法,在shell中,$1为第一个参数,$2为第二个参数,以此类推,那么这里的数字要是一个变量如何表示呢?比如n=3,我想取第三个参数,能否写成 $$n? shell中是不支持的,那怎么办? 就用脚本中的这种方法: a=$n, echo ${!a}
shell习题-获取ip
提示用户输入网卡的名字,然后我们用脚本输出网卡的ip。 看似简单,但是需要考虑多个方面,比如我们输入的不符合网卡名字的规范,怎么应对。名字符合规范,但是根本就没有这个网卡有怎么应对。
参考答案:
#!/bin/bash while : do read -p "请输入网卡名: " e e1=`echo "$e" | sed 's/[-0-9]//g'` e2=`echo "$e" | sed 's/[a-zA-Z]//g'` if [ -z $e ] then echo "你没有输入任何东西" continue elif [ -z $e1 ] then echo "不要输入纯数字在centos中网卡名是以eth开头后面加数字" continue elif [ -z $e2 ] then echo "不要输入纯字母在centos中网卡名是以eth开头后面加数字" continue else break fi done ip() { ifconfig | grep -A1 "$1 " |tail -1 | awk '{print $2}' | awk -F ":" '{print $2}' } myip=`ip $e` if [ -z $myip ] then echo "抱歉,没有这个网卡。" else echo "你的网卡IP地址是$myip" fi
shell习题-用户交互脚本
写一个脚本,执行后,打印一行提示“Please input a number:”,要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入”end”停止。
参考答案:
#!/bin/bash while : do read -p "Please input a number:(end for exit) " n num=` echo $n |sed -r 's/[0-9]//g'|wc -c ` if [ $n == "end" ] then exit elif [ $num -ne 1 ] then echo "what you input is not a number!Try again!" else echo "your input number is: $n" fi done
shell习题-脚本传参
使用传参的方法写个脚本,实现加减乘除的功能。例如: sh a.sh 1 2,这样会分别计算加、减、乘、除的结果。
要求:
1 脚本需判断提供的两个数字必须为整数
2 当做减法或者除法时,需要判断哪个数字大
3 减法时需要用大的数字减小的数字
4 除法时需要用大的数字除以小的数字,并且结果需要保留两个小数点。
参考答案:
#!/bin/bash if [ $# -ne 2 ] then echo "The number of parameter is not 2, Please useage: ./$0 1 2" exit 1 fi is_int() { if echo "$1"|grep -q '[^0-9]' then echo "$1 is not integer number." exit 1 fi } max() { if [ $1 -ge $2 ] then echo $1 else echo $2 fi } min() { if [ $1 -lt $2 ] then echo $1 else echo $2 fi } sum() { echo "$1 + $2 = $[$1+$2]" } minus() { big=`max $1 $2` small=`min $1 $2` echo "$big - $small = $[$big-$small]" } mult() { echo "$1 * $2 = $[$1*$2]" } div() { big=`max $1 $2` small=`min $1 $2` d=`echo "scale =2; $big / $small"|bc` echo "$big / $small = $d" } is_int $1 is_int $2 sum $1 $2 minus $1 $2 mult $1 $2 div $1 $2
shell习题-被3整除
写一个脚本: 计算100以内所有能被3整除的正整数的和
参考答案:
#!/bin/bash sum=0 for i in {1..100};do if [ $[$i%3] -eq 0 ];then sum=$[$i+$sum] fi done echo "sum:$sum"
shell习题-带选项的用户脚本
要求如下:
1 只支持三个选项 ‘–del’ ‘–add’ –help输入其他选项报错。
2 使用‘–add’需要验证用户名是否存在,存在则反馈存在。且不添加。 不存在则创建该用户,切>添加与该用户名相同的密码。并且反馈。
3 使用‘–del’ 需要验证用户名是否存在,存在则删除用户及其家目录。不存在则反馈该用户不存>在。
4 –help 选项反馈出使用方法
5 支持以,分隔 一次删除多个或者添加多个用户。
6 能用echo $? 检测脚本执行情况 成功删除或者添加为0,报错信息为其他数字。
7 能以,分割。一次性添加或者 删除多个用户。 例如 adddel.sh –add user1,user2,user3…….
8 不允许存在明显bug。
参考答案
#!/bin/bash #written by aming. if [ $# -eq 0 -o $# -gt 2 ] then echo "use $0 --add username or $0 --del username or $0 --help." exit 1 fi case $1 in --add) n=0 for u in `echo $2|sed 's/,/ /g'`; do if awk -F: '{print $1}' /etc/passwd |grep -qw "$u" then echo "The user $u exist." else useradd $u echo -e "$u\n$u"|passwd $u >/dev/null 2>&1 echo "The user $u added successfully." n=$[$n+1] fi done if [ $n -eq 0 ]; then exit 2 fi ;; --del) n=0 for u in `echo $2|sed 's/,/ /g'`; do if awk -F: '{print $1}' /etc/passwd|grep -qw "$u" then userdel -r $u echo "The user $u deleted successfully." n=$[$n+1] else echo "The user $u not exist." fi done if [ $n -eq 0 ]; then exit 3 fi ;; --help) echo -e "--add can add user,and the passwd is the same as username. It can add multiuser such as --add user1,user2,user3..." echo "--del cat delete user.It can delete user such as --del user1,user2,user3..." ;; *) echo "use $0 --add username or $0 --del username or $0 --help." exit 1 ;; esac
shell习题-打印只有一个数字的行
2017年8月28日
shell习题
No Comments
aming
如题,把一个文本文档中只有一个数字的行给打印出来。
参考答案:
shell习题