CentOS 7 Shell脚本编程第九讲 条件测试和判断

常用条件测试有 test 命令,[] [[]]等。

##-f 检查是否为文件
[root@promote ~]# ls
anaconda-ks.cfg test.txt 
[root@promote ~]# test -f test.txt && (echo 0 || echo 1)
0
[root@promote ~]# test -f test1.txt && (echo 0 || echo 1)
[root@promote ~]# test -f test.txt && echo 0 || echo 1
0
[root@promote ~]# test -f test.txt1 && echo 0 || echo 1
1
[root@promote ~]# 

详细信息请看。

文件表达式
-e:文件存在
-f:表示这个文件是个一般文件(regular file)(不是目录或者设备文件) 
文件表达式
-e filename 如果 filename文件存在,则为真
-d filename 如果 filename为目录,则为真 
-f filename 如果 filename为是般文件(regular file)(不是目录或者设备文件),则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真 
-w filename 如果 filename可写,则为真 
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真
filename1 -nt filename2 如果 filename1比 filename2新,则为真。
filename1 -ot filename2 如果 filename1比 filename2旧,则为真。

整数变量表达式
比较符号          (( ))中[[ ]]使用符号    说明
-eq 等于              ==或=               equal
-ne 不等于             !=                  not equal
-gt 大于               >                   greater than
-ge 大于等于           >=                  greater equal
-lt 小于               <                   less than
-le 小于等于           <=                  less equal

条件判断中test 和 [ ]等价。(( ))支持简单运算,[ ] 与[[]]不支持,[[ ]]支持&&和||。比较运算符前后推荐添加空格。

详细用法可以查看帮助。

#没有全面列举
[root@promote ~]# man test
[root@promote ~]# man bash
[root@promote ~]# info bash
[root@promote ~]# man bash

#部分帮助文件如下
      -a FILE        True if file exists.
      -b FILE        True if file is block special.
      -c FILE        True if file is character special.
      -d FILE        True if file is a directory.
      -e FILE        True if file exists.
      -f FILE        True if file exists and is a regular file.
      -g FILE        True if file is set-group-id.
      -h FILE        True if file is a symbolic link.
      -L FILE        True if file is a symbolic link.
      -k FILE        True if file has its `sticky' bit set.
      -p FILE        True if file is a named pipe.
      -r FILE        True if file is readable by you.
      -s FILE        True if file exists and is not empty.
      -S FILE        True if file is a socket.
      -t FD          True if FD is opened on a terminal.
      -u FILE        True if the file is set-user-id.
      -w FILE        True if the file is writable by you.
      -x FILE        True if the file is executable by you.
      -O FILE        True if the file is effectively owned by you.
      -G FILE        True if the file is effectively owned by your group.
      -N FILE        True if the file has been modified since it was last read.

注意对比以下代码输出结果。

[root@promote ~]# [ -f /etc/passwd -a /etc/services ] && echo 0 || echo 1
0
[root@promote ~]# [[ -f /etc/passwd -a /etc/services ]] && echo 0 || echo 1 #错误使用-a
[root@promote ~]# [[ -f /etc/passwd && /etc/services ]] && echo 0 || echo 1
0
[root@promote ~]# [ -f /etc/passwd && /etc/services ] && echo 0 || echo 1 #错误使用&&

本节内容通过一段简单代码结束。

#只能输入数字1、2、3
#save a .sh file
#!/bin/bash
testdir=/root/testdir
[! -d testdir ] && mkdir -p /root/testdir
cat << END
	1.[install lamp]
	2.[install lnmp]
	3.[exit]
	please input 1 or 2 or 3 :
END
read input
expr $input + 0 &>/dev/null #Not necessarily 0, /dev//null means not output to standard output interface
[ $? -ne 0 ] && {
	echo "please input {1|2|3}"
	exit 1
}
[ $? -ne 1 ] && {
	echo "Start install lamp ..."
	exit 1
}
[ $? -ne 2 ] && {
	echo "Start install lnmp ..."
	exit 1
}

[root@promote ~]# ls
anaconda-ks.cfg  get-docker.sh  install_mongodb.sh  test  testdir.sh  test.txt  users
[root@promote ~]# 

#简单目录是否存在判断
#查看文件内容
[root@promote ~]# cat testmkdir.sh 
#!/bin/bash
if [ ! -d /root/var/testdir ] ;then
mkdir -p /root/var/testdir
fi 
#执行脚本
[root@promote ~]# bash testmkdir.sh 
[root@promote ~]# ls /root/var/testdir | grep t
testdir
[root@promote ~]#

 

转载于:https://my.oschina.net/u/1011130/blog/3030975

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值