[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat file1.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@hf-01 shell]# sh -x file1.sh 第一次执行,会创建该文件
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ touch /tmp/hanfeng
[root@hf-01 shell]# sh -x file1.sh 第二次执行,会提示该文件已存在
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# cat !$
cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -d $f ]
then
echo $f exist
else
mkdir $f
fi
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -d /tmp/hanfeng ']'
+ mkdir /tmp/hanfeng
[root@hf-01 shell]#
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -e /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -r $f ]
then
echo $f readable
fi
[root@hf-01 shell]# sh file2.sh 会看到文件可读的
/tmp/hanfeng readable
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -w $f ]
then
echo $f writeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng writeable
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -x $f ]
then
echo $f exeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng exeable
f="/tmp/aminglinux"
[ -f $f ] && rm -f $f //前一条命令执行成功才会继续执行之后的命令
等同于下面的表达方式
if [ -f $f ]
then
rm -rf $f
fi
f="/tmp/aminglinux"
[ -f $f ] || touch $f //前面命令不成功时,执行后面的命令
if [ ! -f $f ] // “!”表示了如果这条命令不成功,就往下执行
then
touch $f
fi