天天看点

文件、目录属性判断rm -f $f

接收用户的输入

read -t 30 -p "please input a number." n //-t为时间30秒

1.[ -f file ]判断是否是普通文件,且存在为真

[root@localhost shell]# f="./if01.sh"

[root@localhost shell]# [ -f $f ]&&echo ok || echo no

ok

[root@localhost ~]# vi file01.sh

#!/bin/bash

f="/tmp/lsxlinux"

if [ -f $f ]

then

echo $f exist

else

touch $f

fi

[root@localhost ~]# bash -x file01.sh //-x查看执行的步骤

f=/tmp/lsxlinux

'[' -f /tmp/lsxlinux ']'

touch /tmp/lsxlinux

[root@localhost ~]# bash -x file01.sh

echo /tmp/lsxlinux exist

/tmp/lsxlinux exist

2.[ -d file ] 判断是否是目录,且存在为真

[root@localhost shell]# [ -d ./lsx ] && echo ok || echo no

no

d="/tmp/lsxlinux"

if [ -d $d ]

echo $d exist

touch $d

Fi

[root@localhost ~]# bash -x file02.sh

'[' -d /tmp/lsxlinux ']'

3.[ -e file ] 判断文件或目录存在为真,不辨明是目录还是文件

[root@localhost shell]# if [ -e ./lsx ];then echo exists;else echo on;fi //目录不存在

on

[root@localhost shell]# if [ -e ./if03.sh ];then echo exists;else echo on;fi //文件存在

exists

[root@lr shell]# [ -e if09.sh ]&&echo ok||echo no

4.[ -r file ] 判断文件存在且可读为真

if [ -r $f ]

echo $f read

'[' -r /tmp/lsxlinux ']'

echo /tmp/lsxlinux read

/tmp/lsxlinux read

5.[ -w file ] 判断文件存在且可写为真

if [ -w $f ]

echo $f write

'[' -w /tmp/lsxlinux ']'

echo /tmp/lsxlinux write

/tmp/lsxlinux write

[ -x file ] 判断文件存在且可执行为真

if [ -x $f ]

echo $f exe

'[' -x /tmp/lsxlinux ']'

##判断存在的情况

[ -f $f ] && rm -f $f

#if [ -f $f ]

#then

#fi

//判断不存在

[ -f $f ] && touch $f

if [ ! -f $f ]

本文转自 虾米的春天 51CTO博客,原文链接:http://blog.51cto.com/lsxme/2058289,如需转载请自行联系原作者

上一篇: nginx配置ssl
下一篇: ssl原理