当前位置:网站首页>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))"
边栏推荐
- Technology sharing | do you know the functions of the server interface automated testing and requests library?
- Joern的代码使用-devign
- 主动扫描技术nmap详解
- Custom components -- slots
- Upload and download files from Ubuntu server
- win下安装nessus
- MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays
- RAID磁盘阵列
- Three cache technologies -- localstorage, sessionstorage, cookies
- MySQL installation and use
猜你喜欢

Blue bridge code error ticket

Common models in software development

Archery database audit platform deployment

Custom component -- data listener

Applet navigator cannot jump (debug)

kali下安装nessus

About gcc:multiple definition of

MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation

一、PXE概述和安装

Servlet
随机推荐
MOOC Weng Kai C language fourth week: further judgment and circulation: 1. Logical types and operations 2. Judgment of cascading and nesting
[learning notes] coding ability
Wechat applet custom compilation mode
Monotonic queue, Luogu p1886 sliding window
修复故障扇区
Esxi community network card driver updated in March 2022
Erudite Valley Learning Records] Super summary, attentive sharing | common APIs
Network - data link layer
[learning notes] process creation
Pictures are adaptive to the screen
Archery database audit platform deployment
Result fill in the blank (dfs*c language)
视频格式基础知识:让你了解MKV、MP4、H.265、码率、色深等等
Esxi community network card driver updated again
LNMP搭建过程详解
MOOC翁恺 C语言 第三周:判断与循环:2.循环
DHCP服务
Media set up live broadcast server
Asynchronous programming promise
关于正则的教程