Loading

shell(2)

shell(2)

简答题

1、编写一个shell脚本,从键盘读入一个成绩,并按优秀、良好、中等、及格、不及格输出成绩。

我的答案:

#/bin/bash
read -p "请输入学生成绩(0-100):" score
if [ $sum -gt 100 ] ;then
    echo "输入有误,成绩大于100"
elif [ $sum -ge 90 ] ;then
    echo "优"
elif [ $sum -ge 80 ] ;then
    echo "良"
elif [ $sum -ge 70 ] ;then
    echo "中"
elif [ $sum -ge 60 ] ;then
    echo "及格"
elif [ $sum -ge 0 ] ;then
    echo "不及格"
elif [ $sum -lt 0 ] ;then
    echo "输入有误,成绩小于0"
fi

参考答案 :

#!/bin/bash
next:
read -p "please input a score:"  score  (2分)
if [ $score lt 0 -o $score gt 100 ]    (4分)
then
   echo "illegal score,please input again"
   goto next
fi
if [ $score ge 90 ]    (6分)
then
   echo "A"
elif [ $score -ge 80 ]   (7分)
then
    echo "B"
elif [ $score -ge 70 ]    (8分)
then
     echo "C"
elif [ $score -ge 60 ]    (9分)
then
     echo "D"
Else                      (10分)
     echo "E"
fi

2、请用shell程序写出求1~100所有素数的和。

我的答案:

#!/bin/bash
for((i=1;i<=100;i++))
do
   if((i<=2))
   then
       if((i==2))
       then
           let sum+=i
       fi
   else
      flag=0
      for((j=2;j<=i/2;j++))
      do
          if((i%j==0))
          then
               flag=1
               break
          fi
      done
      if((flag==0))
      then
          let sum+=i
      fi
   fi
done
echo "all prime(1~100) sum is $sum"

参考答案 :

#!/bin/bash
sum=0
for((i=1;i<=100;i++))
do
    flag=0
   for((j=2;j<=i/2;j++))
   do
       if((i%j==0))
       then
            flag=1
            break
      fi
   done
   if((flag==0))
   then
       let sum+=i
   fi
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 
do
  ping $line -c 3
  if [ $? -eq 0 ] 
  then 
    let n1++
  elif
    let 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`
do
    ping -c 3 v$ip
   if [ $? -eq 0 ]
  then
     let n++
  else
    let m++
  fi
done
echo "reachable :$n, unreachable: $m"
posted @ 2024-07-09 13:36  qing影  阅读(60)  评论(0编辑  收藏  举报