shell(2)
shell(2)
简答题
1、编写一个shell脚本,从键盘读入一个成绩,并按优秀、良好、中等、及格、不及格输出成绩。
我的答案:
#/bin/bash
read -p "请输入学生成绩(0-100):" score
if [ $sum -gt 100 ] ;thenecho "输入有误,成绩大于100"
elif [ $sum -ge 90 ] ;thenecho "优"
elif [ $sum -ge 80 ] ;thenecho "良"
elif [ $sum -ge 70 ] ;thenecho "中"
elif [ $sum -ge 60 ] ;thenecho "及格"
elif [ $sum -ge 0 ] ;thenecho "不及格"
elif [ $sum -lt 0 ] ;thenecho "输入有误,成绩小于0"
fi
参考答案 :
#!/bin/bash
next:
read -p "please input a score:" score (2分)
if [ $score lt 0 -o $score gt 100 ] (4分)
thenecho "illegal score,please input again"goto next
fi
if [ $score ge 90 ] (6分)
thenecho "A"
elif [ $score -ge 80 ] (7分)
thenecho "B"
elif [ $score -ge 70 ] (8分)
thenecho "C"
elif [ $score -ge 60 ] (9分)
thenecho "D"
Else (10分)echo "E"
fi
2、请用shell程序写出求1~100所有素数的和。
我的答案:
#!/bin/bash
for((i=1;i<=100;i++))
doif((i<=2))thenif((i==2))thenlet sum+=ifielseflag=0for((j=2;j<=i/2;j++))doif((i%j==0))thenflag=1breakfidoneif((flag==0))thenlet sum+=ififi
done
echo "all prime(1~100) sum is $sum"
参考答案 :
#!/bin/bash
sum=0
for((i=1;i<=100;i++))
doflag=0for((j=2;j<=i/2;j++))doif((i%j==0))thenflag=1breakfidoneif((flag==0))thenlet sum+=ifi
done
echo "$sum"
3、有一个IP地址文件ip.txt,内容如下:
10.22.110.1
10.22.110.10
10.22.110.30
10.22.110.20
10.22.110.40
10.22.110.50
10.22.110.60
请写一个shell程序,统计出有多少台主机可ping通,多少台主机不可Ping通。
我的答案:
#!/bin/bash
n1=0
n2=0
while read line
doping $line -c 3if [ $? -eq 0 ] then let n1++eliflet n2++fi
done<ip.txt
echo "there are $n1 machine reachable"
echo "there are $n2 machine unreachable"
参考答案 :
#!/bin/bash
n=0
m=0
for ip in `cat ip.txt`
doping -c 3 v$ipif [ $? -eq 0 ]thenlet n++elselet m++fi
done
echo "reachable :$n, unreachable: $m"