1.求100以内所有偶数之和
解析:本题主要考察如何遍历出100内所有偶数,并将其累加。
#!/bin/bash
#penman:liuyao
#Time:20190128
#求100以内所有偶数之和
#用到for循环得出100内的数;再用if条件判断100内的偶数;再做个累加。
#注意for后跟do/done 而if结束记得fi
#只要最后的结果,将输出写在done外,要每部的结果写在done前。
sum=0
for ((i=0;i<=100;i++))
do
if [ $((i%2)) -eq 0 ];then
((sum+=i))
fi
done
echo "All Even Number Sums Within 100 is : $sum"
运行结果
[root@localhost ~]# sh s1.sh
All Even Number Sums Within 100 is : 2550
2.写一个脚本,判断192.168.1.0/24网络中,当前在线的IP有哪些?
解析:如何判断当前某段ip有那些在线,可以将ping与echo $?结合来判断,学的好的可以对其进行优化。
#!/bin/bash
#Penman
#Time:20190130
for i in `seq 1 255` #for循环i取1到255
do
ping -c 1 192.168.1.$i &> /etc/null #ping结果错误重定向到/etc/null
#ift条件判断上部结果为0输出up,否则输出down。
if [ $? -eq 0 ];then
echo -e "192,168.1.$i is up"
else
echo -e "192.168.1.$i is down"
fi
done
运行结果
[root@localhost ~]# sh s2.sh
192.168.1.1 is down
192.168.1.2 is down
192.168.1.3 is down
192.168.1.4 is down
3.实现输入2个数和运算符的计算器
解析:本题看似简单但有好多要自己注意,如输入两个什么类型的数,及没有输入完整该如何等等,都需要自己去考虑与优化。
#!/bin/bash
#penman:liuyao
#Time:20190202
read -p "please input tow number :" a b
read -p "please input operator :" c
if [ ! -z $b ] #判断第二个变量是否为空,为空则提示
then
expr $a + $b &> /dev/null #判断输入的是否为整数,用expr让两个数相加不为整数的报错
if [ $? -eq 0 ]
then
echo -e "result=$(($a $c $b))"
else
echo -e "you must input zhengshu"
fi
else
echo -e "you must input tow number"
fi
运行结果
[root@localhost ~]# sh s3.sh
please input tow number :2 4
please input operator :+
result=6
4.显示一个菜单给用户:
cpu)display cpu information
mem)display memory information
disk)display disks information
quit)quit
要求:(1)提示用户给出自己的选择
(2)正确的选择则给出相应的信息;否则,则提示重新选择正确的选项;
提示:例如正确选择了cpu则输出cpu的信息
解析:本题要理解好题意,显示一个菜单选项,有两点要求提示用户及正确选项正确与否做出相应回应。
echo "Please enter your choice:
(cpu) display cpu information
(mem) display memory information
(disk) display disks information
(quit) quit"
#判断了不选择则退出,选择错误则重选;
read -t 10 -p "your choice:" a
if [ -z "$a" ];
echo "Not choice ends `break`."
elif [ $a == cpu ];then
cat /proc/cpuinfo
elif [ $a == mem ];then
cat /proc/meminfo
elif [ $a == disk ];then
fdisk -l
elif [ $a == quit ];then
break
else
echo -e "Please enter again"
fi
运行结果
[root@localhost ~]# sh s4.sh
Please enter your choice:
(cpu) display cpu information
(mem) display memory information
(disk) display disks information
(quit) quit
your choice:Not choice ends .
[root@localhost ~]# sh s4.sh
Please enter your choice:
(cpu) display cpu information
(mem) display memory information
(disk) display disks information
(quit) quit
your choice:disk
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
… … … …
5.写一个服务脚本:
变量$lockfile,变量值为:/var/lock/SCRIPT_NAME
(1)此脚本可接受start、stop、restart、status四个参数之一;
(2) 如果参数非此四者,则提示使用帮助后退出;
(3)start,则创建lockfile,并显示启动;stop,则删除lockfile,并显示停止;restart,则先删除此文件再创建此文件,而后显示重启完成;status,如果lockfile存在,则显示running,否则,则显示为stopped。
解析:SCRIPT_NAME脚本名;变量 $lockfile引用脚本名;下来注意逻辑顺序,可用输出验证是否正确。
#!/bin/bash
#Penman:liuyao
#Time:20190203
#运用if逻辑判断,主要在理解题。
SCRIPT_NAME=s5.sh
lockfile=/var/lock/$SCRIPT_NAME
read -p "Please input start or stop or restart or status :" a
if [ $a == start ];then
if [ -f $lockfile ];then
echo "$SCRIPT_NAME always is exit"
else
touch $lockfile
[ $? -eq 0 ] && echo -e "$SCRIPT_NAME is running"
fi
elif [ $a == stop ];then
if [ -f $lockfile ];then
rm -rf $lockfile && echo -e "$SCRIPT_NAME is stop"
else
echo "$SCRIPT_NAME is not exit"
fi
elif [ $a == restart ];then
if [ -f $lockfile ];then
rm -rf $lockfile && touch $lockfile
echo -e "$SCRIPT_NAME is reboot succes"
else
echo -e "$SCRIPT_NAME not exist"
fi
elif [ $a == status ];then
if [ -f $lockfile ];then
echo -e "$SCRIPT_NAME is running"
else
echo -e "$SCRIPT_NAME is stopped"
fi
else
echo -e "input for start; stop; restart; status"
break
fi
运行结果
[root@localhost ~]# sh s5.sh
Please input start or stop or restart or status :1
input for start; stop; restart; status
[root@localhost ~]# sh s5.sh
Please input start or stop or restart or status :start
s5.sh is running
[root@localhost ~]# sh s5.sh
Please input start or stop or restart or status :stop
s5.sh is stop
[root@localhost ~]# sh s5.sh
Please input start or stop or restart or status :restart
s5.sh not exist
[root@localhost ~]# sh s5.sh
Please input start or stop or restart or status :status
s5.sh is stopped