02: shell中的if、case、for等语句
目录:
1.1 shell中常用运算符返回顶部
| 运算符 | 描述 | 示例 |
| 文件比较运算符 | ||
| -e filename | 如果 filename 存在,则为真 | [ -e /var/log/syslog ] |
| -d filename | 如果 filename 为目录,则为真 | [ -d /tmp/mydir ] |
| -f filename | 如果 filename 为常规文件,则为真 | [ -f /usr/bin/grep ] |
| -L filename | 如果 filename 为符号链接,则为真 | [ -L /usr/bin/grep ] |
| -r filename | 如果 filename 可读,则为真 | [ -r /var/log/syslog ] |
| -w filename | 如果 filename 可写,则为真 | [ -w /var/mytmp.txt ] |
| -x filename | 如果 filename 可执行,则为真 | [ -L /usr/bin/grep ] |
| filename1 -nt filename2 | 如果 filename1 比 filename2 新,则为真 | [ /tmp/install/etc/services -nt /etc/services ] |
| filename1 -ot filename2 | 如果 filename1 比 filename2 旧,则为真 | [ /boot/bzImage -ot arch/i386/boot/bzImage ] |
| 字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法) | ||
| -z string | 如果 string 长度为零,则为真 | [ -z "$myvar" ] |
| -n string | 如果 string 长度非零,则为真 | [ -n "$myvar" ] |
| string1 = string2 | 如果 string1 与 string2 相同,则为真 | [ "$myvar" = "one two three" ] |
| string1 != string2 | 如果 string1 与 string2 不同,则为真 | [ "$myvar" != "one two three" ] |
| 算术比较运算符 | ||
| num1 -eq num2 | 等于 | [ 3 -eq $mynum ] |
| num1 -ne num2 | 不等于 | [ 3 -ne $mynum ] |
| num1 -lt num2 | 小于 | [ 3 -lt $mynum ] |
| num1 -le num2 | 小于或等于 | [ 3 -le $mynum ] |
| num1 -gt num2 | 大于 | [ 3 -gt $mynum ] |
| num1 -ge num2 | 大于或等于 | [ 3 -ge $mynum ] |
1.2 使用if条件语句返回顶部
1、单分支if语句
#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
fi
判断是否存在/media/cdrom目录,如果没有就创建
#!/bin/bash
if [ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 1
fi
fdisk -l /dev/sda
是root用户执行命令、不是root用户直接退出
2、双分支if语句应用
#!/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
使用ping测试网络连通性
[root@localhost ~]# chmod +x pinghost.sh
[root@localhost ~]# ./pinghost.sh 192.168.10.10
Host 192.168.10.10 is up.
[root@localhost ~]# ./pinghost.sh 192.168.10.1
Host 192.168.10.1 is down.
3、多分支if语句应用
#!/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
多分支if语句应用
1.3 shell 中的for循环 返回顶部
1、根据姓名列表批量的添加用户
1)创建用户的列表文件
[root@localhost ~]# vim /root/users.txt
zhangsan
lisi
wangwu
2)编辑批量添加用户的脚本
[root@localhost ~]# vim uaddfor.sh
#!/bin/bash
ULIST=$(cat /root/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo "" | passwd --stdin $UNAME &>/dev/null
done
uaddfor.sh
[root@localhost ~]# ./uaddfor.sh
[root@localhost ~]# tail -3 /etc/passwd
3)编辑批量删除用户的脚本
[root@localhost ~]# vim udelfor.sh
#!/bin/bash
ULIST=$(cat /bbb/users.txt)
for UNAME in $ULIST
do
userdel -r $UNAME &>/dev/null
done
udelfor.sh
1、根据IP地址列表检查主机状态
1)创建IP地址列表文件
[root@localhost ~]# vim /bbb/ipadds.txt
172.16.1.1
172.16.1.111
172.16.1.222
2)编辑循环检查各主机的脚本
[root@localhost ~]# vim chkhosts.sh
#!/bin/bash
HLIST=$(cat /bbb/ipadds.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is up."
else
echo "Host $IP is down."
fi
done
chkhosts.sh
1.4 shell中的while循环语句 返回顶部
1、批量添加用户脚本
[root@localhost ~]# vim uaddwhile.sh
#!/bin/bash
PREFIX="stu"
i=1
while [ $i -le 20 ]
do
useradd ${PREFIX}$i
echo "" | passwd --stdin ${PREFIX}$i &> /dev/null
let i++
done
uaddwhile.sh
2、批量删除用户脚本
[root@localhost ~]# vim udelwhile.sh
#!/bin/bash
PREFIX="stu"
i=1
while [ $i -le 20 ]
do
userdel -r ${PREFIX}$i
let i++ done
udelwhile.sh
3、猜价格游戏
[root@localhost ~]# vim pricegame.sh
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格范围为0-999,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "恭喜你答对了,实际价格是 $PRICE"
echo "你总共猜测了$TIMES 次"
exit 0
elif [ $INT -gt $PRICE ] ; then
echo "太高了!"
else
echo "太低了!"
fi
done
pricegame.sh
1.5 使用case分支语句返回顶部
1、编写检查用户输入的字符类型的脚本
[root@localhost ~]# vim hitkey.sh
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" KEY
case "$KEY" in
[a-z]|[A-Z])
echo "您输入的是 字母."
;;
[0-9])
echo "您输入的是 数字."
;;
*)
echo "您输入的是 空格、功能键或者其他控制字符."
esac
hitkey.sh
2、编写系统服务脚本模板
#!/bin/bash
# The next lines are for chkconfig on RedHat systems.
# chkconfig: 35 98 02
# description: Starts and stops xxx Server # The next lines are for chkconfig on SuSE systems.
# /etc/init.d/xxx
#
### BEGIN INIT INFO
# Provides: xxx
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Short-Description: Starts and stops xxx Server
# Description: Starts and stops xxx Server
### END INIT INFO case $1 in
start) # 服务启动需要做的步骤
...
;;
stop) # 服务停止需要做的步骤
...
;;
restart) # 重启服务需要做的步骤
...
;;
status) # 查看状态需要做的步骤
...
;;
*) echo "$0 {start|stop|restart|status}"
exit 4
;;
esac
系统服务脚本模板
3、编写squid服务脚本
[root@s2 ~]# vi /etc/init.d/squid
#!/bin/bash
#chkconfig: 2345 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#description: squid - internet object cache.
PID="usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -anpt | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动squid…….."
$CMD
fi
;; stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;; status)
[ -f $PID ] &> /dev/null
if [$? -eq 0 ]
then
netstat -anpt | grep squid
else
echo "squid is not running"
fi
;; restart)
$0 stop &> /dev/null
echo "正在关闭squid……"
$0 start &> /dev/null
echo "正在启动squid……"
;; reload)
$CMD -k reconfigure
;; check)
$CMD -k parse
;; *)
echo "用法:$0 {start | stop |restart | reload | check | status}"
;;
esac
squid
将squid添加为系统服务
[root@s2 init.d]# chmod +x /etc/init.d/squid
[root@s2 init.d]# chkconfig --add squid
[root@s2 init.d]# chkconfig squid on
02: shell中的if、case、for等语句的更多相关文章
- shell中select、case的使用
case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select select结 ...
- Shell中的条件测试和循环语句
1.条件测试:test或[ 如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为0 运行结果: 带与.或.非的测试命令[ ! EXPR ] : E ...
- shell中while循环的陷阱
在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...
- C Shell中的变量数组
今天刚刚在看一点C Shell的内容,发现一个挺好玩的东西!就是环境变量可以像数组那样来设置!具体设置语法如下: set variable=(element1 element2 ...) //注意元素 ...
- (二)shell中case语句、程序传参、while
2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...
- shell中case的用法学习笔记
这篇文章主要为大家介绍shell中的case语句:可以把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码. 本文转自:http://www.jbxue.com/article ...
- linux bash shell中case语句的实例
本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- 【转】shell中的$0 $n $# $* $@ $? $$ 变量 if case for while
shell中的$0 $n $# $* $@ $? $$ shell 编程 | shift 命令用法笔记 $0当前脚本的文件名 $n传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个 ...
随机推荐
- POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...
- is_link
'Symbolic Link' to File1 content containing path to File1'Hard Link' to File1 content containing Fi ...
- 新建虚拟机_WIN7 32位系统
准备工作:下载win7 32位纯净版镜像文件 大部分步骤与安装XP系统相似,此处只说明一下不同: 创建好虚拟机后启动有报错:CHS data ERROR,无法从CD/DVD启动 编辑虚拟机--> ...
- CF573C Bear and Drawing 构造+树论
正解:构造 解题报告: 传送门! 这题首先可以画下图找下规律,,,然后通过找规律可以发现,最终的方案一定是一条主干+一些枝条,而且这些枝条的分杈一定小于等于2 明确一下主干的定义,最左边的节点和最右边 ...
- KVM VHOST中irqfd的使用
2018-01-18 其实在之前的文章中已经简要介绍了VHOST中通过irqfd通知guest,但是并没有对irqfd的具体工作机制做深入分析,本节简要对irqfd的工作机制分析下.这里暂且不讨论具体 ...
- python课件-淘宝-目录.txt
卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python课件-淘宝-目录.txt│ ├─01python核心编程阶段-linux基础(│ linux_h ...
- mysql 内置功能 视图介绍
之前的多表查询本质是把多张有关系的表连接在一起组成一张虚拟表,从而进行查询 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使用时只需使用[名称]即 ...
- 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
在Ubuntu中,有时候运用sudo apt-get install 安装软件时,会出现一下的情况 E: Could not get lock /var/lib/dpkg/lock - open ( ...
- mysql数据库的初始化及相关配置
接着上篇文章我们继续探讨在安装完mysq数据库之后的一些相关配置: 一.mysql数据库的初始化 我们在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通 ...
- context、config
Tomcat启动时已经创建了context,并使用它读取了web.xml中的参数,后台可以从context里获取参数 后台获取参数代码: ServletContext context = getSer ...