当前位置:网站首页>shell---条件语句练习
shell---条件语句练习
2022-07-28 05:29:00 【小白啥都要懂】
1、ping主机测试,查看主机是否存活;
脚本如下所示:
read -p "please input your hostname:" hostname
ping -c2 $hostname &> /dev/null
if [ $? = 0 ]
then
echo "host $hostname is ok"
else
echo "host $hostname is fail"
fi结果如下所示:
[[email protected] ~]# ./ping.sh
please input your hostname:www.baidu.com
host www.baidu.com is ok
[[email protected] ~]# ./ping.sh
please input your hostname:192.168.188.200
host 192.168.188.200 is fail
[[email protected] ~]# 2、判断一个用户是否存在;
#!/bin/bash
##############################################################
# File Name: 2.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 16:44:24
# Description:
##############################################################
read -p "please input username:" username
grep "$username" /etc/passwd &> /dev/null
if [ $? -eq 0 ];then
echo "$username is already exist"
else
echo "$username is not exist"
fi3、判断当前内核主版本是否为3,且次版本是否大于10;
#!/bin/bash
##############################################################
# File Name: kernal_num.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 17:46:08
# Description:
##############################################################
ker_num=`uname -r | awk -F . '{print $1}'`
sec_num=`uname -r | awk -F . '{print $2}'`
if [ "$ker_num" -eq 3 ];then
if [ "$sec_num" -le 10 ];then
echo "the most kernel is $ker_num"
echo "the second version no more than 10"
else
echo "the most kernel is $ker_num"
echo "the second version more than 10"
fi
fi4、判断vsftpd软件包是否安装,如果没有则自动安装;
#rpm -qa | grep vsftpd;echo $?
rpm -q vsftpd
#yun list vsftpd
if [ $? -eq 0 ];then
echo "package is alread exist"
else
echo "the package is installing"
`yum install vsftpd` &> /dev/null
fi5、判断httpd是否运行;
#!/bin/bash
##############################################################
# File Name: http_jun.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 18:09:47
# Description:
##############################################################
httpd_jun=`ps -ef | grep httpd | grep -v grep | wc -l`
if [ "$httpd_jun" -ge 1 ];then
echo "the httpd is running"
else
echo "the httpd is not running"
fi6、判断指定的主机是否能ping通,必须使用$1变量;
[[email protected] ~]# ./ping.sh www.baidu.com
www.baidu.com is ok
[[email protected] ~]# cat ping.sh
#!/bin/bash
##############################################################
# File Name: ping.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 18:28:15
# Description:
##############################################################
ping -c 2 $1 &> /dev/null
if [ "$?" -eq 0 ];then
echo "$1 is ok"
else
echo "$1 is not ok"
fi
[[email protected] ~]#7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[[email protected] ~]# echo "邮件正文"l mail -s"邮件主题" alice
[[email protected] ~]# cat mem.sh
#!/bin/bash
##############################################################
# File Name: mem.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 09:31:16
# Description:
##############################################################
root_now=`df -h | grep -w / | tr -s " " | awk -F " " '{print $5}' | tr -d %`
mem_now1=`free -m | grep "^Mem:" | tr -s " " | awk -F " " '{print $3}'`
mem_now2=`free -m | grep "^Mem:" | tr -s " " | awk -F " " '{print $4}'`
mem_avg=$((mme_now1/mem_now2))
if [ "$root_now" -gt 10 ];then
echo "the rootmemory is less than 10"
echo "root memory now is $root_now" ; `mail -s "root_mem" root`
elif [ "$mem_avg" -gt 1 ];then
echo "the memory is more than 1"
echo "the memory now is $mem_avg";`mail -s "memory" root`
else
echo "The status is right"
fi8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10;
[[email protected] ~]# cat jun__num.sh
#!/bin/bash
##############################################################
# File Name: jun__num.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 20:44:51
# Description:
##############################################################
read -p "please input your number:" number
if echo "$number" | grep "[0-9]" &> /dev/null;then
echo "thr number is right"
if [ "$number" -gt 10 ];then
echo "$number is more than 10"
elif [ "$number" -eq 10 ];then
echo "$number is equal 10"
else
echo "$number is less than 10"
fi
fi9、计算用户输入的任意两个整数的和、差、乘积、商、余数,
判断用户输入的参数是否是两个,如果不是,提示用法;
判断用户输入的是否是整数,如果不是,则给出提示终止运行。
[[email protected] ~]# cat 2.sh
#!/bin/bash
##############################################################
# File Name: 2.sh
# Version: V1.0
# Author: xx
# Email: [email protected]
# Organization: http://www.xx.com/xx/
# Created Time : 2022-07-16 20:56:53
# Description:
##############################################################
if [ "$#" -ne 2 ];then
echo "usage: $0 num1 num2"
exit 1
fi
expr "$1" + "$2" &> /dev/null
if [ "$?" -ne 0 ];then
echo "one or two is not a interger number"
exit 2
fi
echo "$1+$2=$(($1+$2))"
echo "$1-$2=$(($1-$2))"
echo "$1*$2=$(($1*$2))"
echo "$1/$2=$(($1/$2))"
echo "$1%$2=$(($1%$2))"
边栏推荐
猜你喜欢

Esxi arm edition version 1.10 update

codesensor:将代码转化为ast后再转化为文本向量

PXE unattended installation management

Applet navigator cannot jump (debug)

DOM -- event chain, event bubble and capture, event proxy
![[learning records of erudite Valley] Super summary, attentive sharing | collection](/img/a3/4183a074a7cdc41fe7624624492280.png)
[learning records of erudite Valley] Super summary, attentive sharing | collection

MOOC翁恺C语言 第四周:进一步的判断与循环:1.逻辑类型与运算2.级联和嵌套的判断

DHCP principle and configuration

Wechat applet custom compilation mode

Repair the faulty sector
随机推荐
Esxi community nvme driver update v1.1
Esxi community network card driver updated in March 2022
DOM Foundation
Applets: WSX scripts
Group management and permission management
组管理和权限管理
On cookies and session
Escape character notes
Custom component -- pure data field & component life cycle
LNMP搭建过程详解
anaconda3无法打开navigator的解决办法
Results fill in the blanks carelessly (violent solution)
Small turtle C (Chapter 5 loop control structure program 567) break and continue statements
Shell script - regular expression
Applets: lifecycle
metasploit渗透ms7_010练习
Clock tree analysis example
Servlet
MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation
Blue Bridge Cup square filling number