shell编程-条件判断与流程控制
1.条件判断式
按照文件类型进行判断:
两种判断格式:
test -e /root/install.log
[ -e /root/install.log ]
判断命令是否正确执行:
[ -d /root ] && echo "yes" || echo "no"
按文件权限判断:
两个文件之间的比较:
[ /root/stu.txt -ef /tmp/stu.txt]
两个整数之间的比较:
字符串的判断:
a=21,b=56
[ "$a" == "$b" ]
多重条件的判断:
2.流程控制
单分支语句if:
判断登陆用户是否为root:
vim isRoot.sh
#!/bin/bash
test=$(env | grep USER |cut -d "=" -f 2)
if [ "$test" == "root"];then
echo "the user is root"
fi
双分支if语句:
read -t 30 -p "please enter a dir: "dir
if [ -d "$dir" ]
then
echo "is dir"
else
echo "no"
fi
判断Apache是否启动
test=$(ps aux | grep httpd |grep -v grep)
if [ -n "$test" ]
then
echo "$(date) httpd is running"
else
echo "httpd is not running"
services httpd start
fi
多分支if语句:
多分支case语句:
for循环:
cd /root/test/
ls *.tar.gz > ls.log
ls *.tgz >> ls.log
for i in $( cat ls.log )
do
tar -zxf $i & > /dev/null
done
rm -rf ls.log
for i in $(cat /etc/passwd | grep /bin/bash |grep -v root |cut -d ":" -f1)
do
userdel -r $i
done
while循环和until循环:
最新文章
- Python Day21
- Linux如何找出用户的创建时间
- php5.5安装及phpmyadmin&;nginx配置php模块
- DelegatingFilterProxy
- NSMutableArray 根据key排序
- Inaccurate values for “Currently allocated space” and “Available free space” in the Shrink File dialog for TEMPDB only
- MSP430常见问题之IO端口类
- Ubuntu 14.04 SSH + 远程登录xrdp
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
- django 模板视图,表单视图,各种视图
- Spring security oauth2最简单入门环境搭建
- Python -- Web -- WSGI
- 解决phpcms V9缩略图模糊的方法
- 理解Go Interface
- html基础和CSS选择器
- [C++ Primer Plus] 第10章、对象和类(一)程序清单——辨析三个const
- 【问题解决方案】之 关于某江加密视频swf专用播放器仍无法播放的问题
- python-异常处理、元类
- vCenter简单查看多少虚拟机在开机状态和一共多少虚拟机
- Oracle EBS - Setup: 配置文件Profile