使用while语句计算1 +2+3+4+.... +n,其中n的大小由键盘输入决定,输出最后的和.
[root@localhost ~]# vi aa.sh
#! /bin/bash
read -p "please input n:" n
i=0
s=0
while [ "$i" != "$n" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo $s
~
(ctrl+c)
"aa.sh" 10L, 113C written
[root@localhost ~]# ./aa.sh
please input n:50
1275
[root@localhost ~]# ./aa.sh
please input n:100
50507. (简答题.5.0分)
脚本编程:
使用for和if语句测试ping 10.1.1.201-210地址的连接通,如果通输出yes,不通输出no
[root@localhost ~]# vim bb.sh
#! /bin/bash
network="192.168.50."
for i in $(seq 1 10)
do
ip=${network}${i}
ping -c 1 -w 1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "$ip is yes"
else
echo"$ip is no"
fi
done
~
~
~
~
~
~
~
~
~
~
~
"bb.sh" 12L, 185C 已写入
[root@localhost ~]# ./bb.sh
192.168.50.1 is yes