#!/bin/bash
#describe
# Determine whether the IP is online at 192.168.1.0/24
#进行ping测试,ping成功则该IP在线,否则该IP不在线,对ping的结果进行判断
for ip in {1..255};do
{
ping -c 1 192.168.1.$ip > /dev/null
if [ $? -eq 0 ]; then
echo 192.168.1.$ip UP
else
echo 192.168.1.$ip DOWN
fi
}& #让其在后台执行i,缩短执行时间,可以不按顺序显示,哪个执行完毕就先显示哪个
done
wait #等待此脚本结束才可出命令行
2.求100以内所有偶数之和
#!/bin/bash
#2019-1-29
#describe:
# Calculate the sum of even Numbers up to 100
#
#用for循环出100以内的偶数,在for循环中调用变量进行计算其
sum=0
for ((i=0;i<=100;i+=2));do
sum=$((sum+i))
done
echo $sum
3.实现输入2个数可以进行加、减、乘、除功能的计算器
#!/bin/bash
#describe
# Enter two Numbers, add, subtract, multiply and divide
#先确定要进行哪一种操作,判断,然后再输入要操作的两个数,进行引用然后操作
q=1
#while实现循环运算
while [ $q ]
do
#选择要进行的操作
read -p "请选择您要进行的运算(请输入整数):
1.加
2.减
3.乘
4.除
其他:退出运算
:" o
#判断用户选择哪种操作,然后进行相应的运算
if [ $o -eq 1 ];then
read -p "请输入您的第一个正整数:" a
read -p "请输入您的第二个正整数:" b
c=$((a + b))
echo $a"+"$b"="$c
elif [ $o -eq 2 ];then
read -p "请输入您的第一个正整数:" a
read -p "请输入您的第二个正整数:" b
c=$((a - b))
echo $a"-"$b"="$c
elif [ $o -eq 3 ];then
read -p "请输入您的第一个正整数:" a
read -p "请输入您的第二个正整数:" b
c=$((a * b))
echo $a"*"$b"="$c
elif [ $o -eq 4 ];then
read -p "请输入您的第一个正整数:" a
read -p "请输入您的第二个正整数:" b
c=$((a / b))
echo $a"/"$b"="$c
else
break #退出while循环
fi
echo ""
echo ""
echo ""
done
4.显示一个菜单给用户:
cpu)display cpu information
mem)display memory information
disk)display disks information
quit)quit
要求:(1)提示用户给出自己的选择
(2)正确的选择则给出相应的信息;否则,则提示重新选择正确的选项;
提示:例如正确选择了cpu则输出cpu的信息
#!/bin/bash
#describe
# Choose your want result
#
q=1
while [ $q ]
do
#提示用户做出选择
read -p "Please enter your choice:
cpu) display cpu inforation
mem) display memory information
disk) display disks information
quit) quit
:" o
#
#根据用户的输入进行判断,然后执行用户的选择
if [ $o == cpu ];then
cat /proc/cpuinfo
elif [ $o == mem ];then
cat /proc/meminfo
elif [ $o == disk ];then
fdisk -l
elif [ $o == quit ];then
break
else
echo "please enter again"
fi
echo ""
echo ""
echo ""
done
#!/bin.bash
#describe
# To operate on the / var/lock/subsys/test.sh file
#两个变量,一个文件,一个文件的路径
SCRIPT_NAME=test.sh
lockfile=/var/lock/subsys/$SCRIPT_NAME
#提示用户输入的信息
read -p "please input start or stop or restart or status:" o
#对用户输入的信息进行操作并作出相应的反应
if [ $o == start ];then
if [ -f $lockfile ];then
echo "$SCRIPT_NAME always is exit."
else
touch $lockfile
[ $? -eq 0 ] && echo "$SCRIPT_NAME is running."
fi
elif [ $o == stop ];then
if [ -f $lockfile ];then
rm -f $lockfile && echo "$SCRIPT_NAME is stop."
else
echo "$SCRIPT_NAME is not exit."
fi
elif [ $o == restart ];then
if [ -f $lockfile ];then
rm -f $lockfile && touch $lockfile
echo "$SCRIPT_NAME is reboot complete."
else
echo "$SCRIPT_NAME not exist"
fi
elif [ $o == status ];then
if [ -f $lockfile ];then
echo "$SCRIPT_NAME is running."
else
echo "$SCRIPT_NAME is stopped."
fi
else
echo "input error,and the input range is start, stop, start, and status."
break
fi