1、ping主机测试,查看主机是否存活;
mkdir -p /shells/test
cd /shells/test/
vim 1.sh
#!/bin/bash
if ping -c2 192.168.227.130 &>/dev/null
then
echo "192.168.227.130 is reachable"
else
echo "192.168.227.130 is not reachable"
fi
bash 1.sh
2、判断一个用户是否存在;
vim 2.sh
#!/bin/bash
read -p "input your user:" user
id $name &>/dev/null
if [ $? -eq 0 ]
then
echo "$user exitence"
else
echo "$user exitence"
fi
bash 2.sh
3、判断当前内核主版本是否为3,且次版本是否大于10;
vim 3.sh
#!/bin/bash
main = uname -r | cut -d '.' -f 1
inmain = uname -r | cut -d '.' -f 2
if [ "$main" -eq 3 ] && [ "$inmain" -ge 10 ]
then
echo "main version is "$main"
inmain version is "$inmain""
else
echo "main version is not 3
inmain version is more than 10"
fi
~
bash 3.sh
4、判断vsftpd软件包是否安装,如果没有则自动安装;
vim 4.sh
#!/bin/bash
rpm -qa | grep vsftpd >&/dev/null
if [ $? -eq 0 ]
then
echo" Installed "
else
yum isntall -y vsftpd
fi
bash 4.sh
5、判断httpd是否运行;
vim 5.sh
#!/bin/bash
status="`ps -ef | grep httpd | wc -l`"
if [ $status -ge 1 ]
then
echo "httpd is running"
else
echo "httpd is not running"
fi
bash 5.sh
6、判断指定的主机是否能ping通,必须使用$1变量;
vim 6.sh
#!/bin/bash
ping -c2 192.168.227.130 &>/dev/null
if [ "$?" -eq 0 ]
then
echo "192.168.227.130 is reachable"
else
echo "192.168.227.130 is not reachable"
fi
bash 6.sh
7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” alice
vim 7.sh
#!/bin/bash
root_total=`free -m | tr -s " " | cut -d" " -f2 | head -2 | tail -1`
root_used=`free -m | tr -s " " | cut -d" " -f3 | head -2 | tail -1`
mem_used=`df -hP | tr -s " " | cut -d" " -f5 | head -2 | tail -1 | cut -d"%" -f1`
root=$[$root_used / $root_total]
if [ $root -gt 80 ]
then
echo "gen less than20%" | mail -s "报警信息" alice
elif [ $mem_used -gt 80 ]
then
echo "member is more than 80%" | mail -s "报警信息" alice
else
echo "is usuall"
fi
bash 7.sh
8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10
vim 9.sh
#!/bin/bash
read -p "please input number:" num
expr $num + 1 >&/dev/null
if [ $? -eq 0 ]
then
echo "this is a number"
else
echo "this is not a number"
fi
bash 9.sh
9、计算用户输入的任意两个整数的和、差、乘积、商、余数,
判断用户输入的参数是否是两个,如果不是,提示用法;
判断用户输入的是否是整数,如果不是,则给出提示终止运行。
a=$1
b=$2
if [ $# -eq 2 ]
then
if [[ "$a" =~ ^[0-9]*$ && "$b" =~ ^[0-9]*$ ]]
then
echo "a、b is a number"
echo a+b=$((a+b))
echo a-b=$((a-b))
echo a*b=$((a*b))
echo a/b=$((a/b))
echo a%b=$((a%b))
else
echo "a、b is not a number"
exit 0
fi
else
echo "this is not two number"
exit 0
fi