Shell脚本应用(五)if语句
单分支的if语句:
if 条件测试操作
then
命令序列
fi
例:判断挂载点目录是否存在,若不存在则新建此目录。
[root@dbserver script]# cat chkmountdir.sh
#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
fi
[root@dbserver script]# chmod +x chkmountdir.sh
[root@dbserver script]# ./chkmountdir.sh
[root@dbserver script]# ls /media/
cdrom RHEL_6.5 x86_64 Disc 1
例:若当前用户不是root,则提示报错:
[root@dbserver script]# cat chkifroot.sh
#!/bin/bash
if [ "$USER" != "root" ];then
echo "错误:非root用户,权限不足!"
exit 1
fi
fdisk -l /dev/sda
[root@dbserver script]# chmod +x chkifroot.sh
[root@dbserver script]# ./chkifroot.sh
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e59bd
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 6528 51915776 8e Linux LVM
[root@dbserver script]# su - oracle
[oracle@dbserver ~]$ source /script/chkifroot.sh
错误:非root用户,权限不足!
2.双分支的if语句:
if 条件测试操作
then
命令序列1
else
命令序列2
fi
例:编写一个连通性的测试脚本,通过位置参数$1提供目标主机地址。然后根据ping检测结果给出相应的提示。
[root@dbserver script]# cat ping.sh
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $1 is up."
else
echo "Host $1 is down."
fi
[root@dbserver script]# chmod +x ping.sh
[root@dbserver script]# ./ping.sh 192.168.1.11
Host 192.168.1.11 is up.
[root@dbserver script]# ./ping.sh 192.168.1.12
Host 192.168.1.12 is down.
例:通过shell脚本检查httpd服务是否运行;
[root@dbserver script]# cat chkhttpd.sh
#!/bin/bash
/etc/init.d/httpd status &>/dev/null
if [ $? -eq 0 ];then
echo "监听地址:$(netstat -anpt |grep httpd | awk '{print $4}')"
echo "进程PID号:$(pgrep -x httpd)"
else
echo "警告:httpd服务不可用"
fi
[root@dbserver script]# chmod +x chkhttpd.sh
[root@dbserver script]# ./chkhttpd.sh
警告:httpd服务不可用
[root@dbserver script]# service httpd start
[root@dbserver script]# ./chkhttpd.sh
监听地址::::80
进程PID号:2398
3.多分支if语句
if 条件测试操作1
then
命令序列1
elif 条件测试操作2
then
命令序列2
else
命令序列3
fi
例:编写一个成绩分档脚本;
[root@dbserver script]# cat gradediv.sh
#!/bin/bash
read -p "请输入你的成绩[0-100]:" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ];then
echo "$GRADE 分!你分数很牛逼"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ];then
echo "$GRADE 分!还行吧小伙子"
else
echo "$GRADE 分!你是个垃圾"
fi
[root@dbserver script]# chmod +x gradediv.sh
[root@dbserver script]# ./gradediv.sh
请输入你的成绩[0-100]:100
100 分!你分数很牛逼
[root@dbserver script]# ./gradediv.sh
请输入你的成绩[0-100]:78
78 分!还行吧小伙子
[root@dbserver script]# ./gradediv.sh
请输入你的成绩[0-100]:50
50 分!你是个垃圾
就到这吧,慢慢积累
转载于:https://blog.51cto.com/liliming/1793023
Shell脚本应用(五)if语句相关推荐
- 如何获取shell脚本中某条语句的执行时间
有没有办法获取shell脚本中某条语句的执行时间 如果要获得语句执行所花的时间 用time命令 在语句的前面加上time命令 输出结果一共3列 比如我执行time sleep 5 real 0m ...
- shell脚本(五)——函数的格式、传参、变量、递归、函数库
shell脚本(五)--函数 一.shell函数定义 二.格式 1.格式一: 2.格式二: 三.函数返回值 四.函数传参 五.函数变量的作用范围 六.递归 1.阶乘 2.递归查目录 七.创建函数库 一 ...
- Shell脚本break和continue语句应用
记录:436 场景: Shell脚本break和continue语句应用.在for.while循环中使用break和continue语句. 版本:CentOS Linux release 7.9.20 ...
- shell 脚本编程 五:函数
一.函数定义 有两种格式可以用来在 bash shell 脚本中创建函数. 第一种格式采用关键字 function,后跟分配给该代码块的函数名. function name { commands } ...
- linux用if语句编程序,Shell 脚本基础 - 使用 if 语句进行条件检测
Bourne Shell 的 if 语句和大部分编程语言一样 - 检测条件是否真实,如果条件为真,shell 会执行这个 if 语句指定的代码块,如果条件为假,shell 就会跳过 if 代码块,继续 ...
- shell 脚本实战 五
摘自 shell脚本实战 第二版 第四章 Unix调校 脚本27 显示带有行号的文件 在显示文件时添加行号有很多种方法,其中一些实现起来相当简洁.例如,下面是 awk 的 做法: awk '{prin ...
- shell脚本:使用if-then语句和test命令
一.基本结构化命令if-then语句格式: bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行.如果该命令的退出状态 ...
- 怎么编写Linux脚本循环语句,shell脚本及常用循环语句
博文结构 什么是shell 简单编辑shell 循环语句 一.什么是shell及作用 Shell字面理解就是个"壳",是操作系统(内核)与用户之间的桥梁,充当命令解释器的作用,将用 ...
- linux的shell脚本if语句,Shell脚本编程之判断语句
一.单分支if语句 格式:if 判断条件:then statement1 statement2 ... fi 二.双分支的if语句 格式:if 判断条件; then statement1 statem ...
- shell脚本if与read语句用法练习
#位置参数变量: #my_shell.sh ubuntu($1) centos($2) linux($3) #引用方式:$1 $2 $3 #useradd $1 #useradd $2 #userad ...
最新文章
- 基于ESP8266WiFi步进电机控制
- 项目上线与LOG记录
- Oracle过滤与排序
- mybatis学习(19):模糊查询#
- c语言形式参数若为b 4,2016年计算机二级c语言题库
- 技术实操丨SoundNet迁移学习之由声音分类到语音情感识别
- Uniapp 移动端调用摄像头扫描二维码或者条形码
- 40题计算机程序设计基础(C语言)编程习题
- charles(青花瓷)抓包配置mac ios版,详细好用
- 【一键新机】免root/不刷机/拒绝Xposed 实现 Android改机,全新技术分析。
- (孔乙己茴香豆的的“茴”字有几种写法)三栏布局两边固定中间自适应
- 网站建设需遵循的六个步骤
- 温故而知新的意思是什么?温故而知新出自哪里?
- JSHOP2的基本使用
- 基于图像算法的透明容器液面液位识别算法
- word格式刷刷完格式后公式上凸了怎么办?
- 【面试篇】ConcurrentHashMap1.7和1.8详解对比
- 云帆文档管理系统版本更新说明:v4.6.0
- MOS管和三极管的工作原理对比
- 第三章:IP地址加掩码讲解和ping命令运用