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 是一个数字,表示第几个参数.例如,第一个 ...
随机推荐
- Databases: MySQL tRIGger--chinese character-set php
DELIMITER |create TRIGGER tr_calllog_insert after insert on messagescalllog for each row beginIF mes ...
- 阿里云短信发送服务SDK-Python3
本文提供阿里云的短信发送服务SDK,使用Python3实现. # -*- coding: utf-8 -*- # pip install requests import requests import ...
- 2018/03/16 echo、print_r、print、var_dump之间的区别
还是先说下我对这个方法的理解 print_r()用于 cli模式下的输出调试,error_log() 调试 var_dump() 用于页面的显示调试 echo() 用处比较多,__toString() ...
- Qt With Web(两种方法: WebAssembly,WebGL)
Qt 写的程序,可以通过某些方式,在 web 上浏览 https://www.youtube.com/watch?v=OJ1ieyuIQzEwww.youtube.comhttps://www.yo ...
- vueawesomeswiper自定义 导航点
1,pagination的配置 pagination: { el: '.swiper-paginationfull', // type:'bullets', // bulletElement : 's ...
- 【python-opencv】20-图像金字塔
知识点介绍 图像金字塔原理: 高斯金字塔 拉普拉斯金字塔: 代码层面知识点: cv2.PyrDown:降采样 cv2.PyrUp:升采样 高斯金字塔与拉普拉斯金字塔 图像金字塔是图像中多尺度表达的 ...
- fork 了别人的仓库后,如何将自己的代码和原仓库保持一致
fork 了别人的仓库后,如何将自己的代码和原仓库保持一致 git remote add upstream http:// git fetch upstream
- hibernate注解(二)mappedBy
1.双向关系中使用mappedBy避免生成中间表 (1).只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性: (2).mappe ...
- javaScript高级教程(二)Scope Chain & Closure Example
<!DOCTYPE html> <html> <head> <meta charset=gb2312 /> <title>js</ti ...
- xpath教程 3 - xpath的小结
一.xpath提取内容 1.提取节点中最表层的文本 htmlobj.xpath("./text()") 在scrapy中用extract()[0]方法抽取文本.如: temp['t ...