[root@xuexi-001 shell]# vi file1.sh #!/bin/bash f="/tmp/aminglinux" if [ -f $f ] then echo $f exist else touch $f fi [root@xuexi-001 shell]# cat file1.sh #!/bin/bash f="/tmp/aminglinux" if [ -f $f ] then echo $f exist else touch $f fi [root@xuexi-001 shell]# chmod +x file1.sh [root@xuexi-001 shell]# sh file1.sh #执行完后这个文件已经建立 [root@xuexi-001 shell]# ls /tmp/ aminglinux [root@xuexi-001 shell]# sh -x file1.sh + f=/tmp/aminglinux + '[' -f /tmp/aminglinux ']' + echo /tmp/aminglinux exist /tmp/aminglinux test [root@xuexi-001 shell]# sh file1.sh /tmp/aminglinux test
[root@xuexi-001 shell]# sh -x file2.sh + f=/tmp/aminglinux1 + '[' -d /tmp/aminglinux1 ']' + echo /tmp/aminglinux1 exist /tmp/aminglinux1 exist [root@xuexi-001 shell]# ls /tmp/aminglinux aminglinux aminglinux1/ [root@xuexi-001 shell]# ls /tmp/aminglinux /tmp/aminglinux [root@xuexi-001 shell]# ls /tmp/ aminglinux aminglinux1
目录和文件均可以touch 的,touch的目的是 若是这个文件或目录不存在,它会建立这个文件,若是这个文件或目录存在了,在touch 就会更改这个文件的三个 timelinux
[root@xuexi-001 shell]# vi file2.sh #!/bin/bash f="/tmp/aminglinux2" if [ -e $f ] then echo $f exist else touch $f fi [root@xuexi-001 shell]# sh file2.sh [root@xuexi-001 shell]# sh -x file2.sh + f=/tmp/aminglinux2 + '[' -e /tmp/aminglinux2 ']' + echo /tmp/aminglinux2 exist /tmp/aminglinux2 exist [root@xuexi-001 shell]# ls /tmp/ aminglinux aminglinux1 aminglinux2
[root@xuexi-001 shell]# vi file2.sh #!/bin/bash f="/tmp/aminglinux2" if [ -r $f ] then echo $f readable fi [root@xuexi-001 shell]# sh file2.sh /tmp/aminglinux2 readable 会看到文件可读的
去判断是否刻度可写,就判断执行shell脚本的当前用户shell
[root@xuexi-001 shell]# vi file2.sh #!/bin/bash f="/tmp/aminglinux2" if [ -w $f ] then echo $f writeable fi [root@xuexi-001 shell]# sh file2.sh /tmp/aminglinux2 writeable
[root@xuexi-001 shell]# vi file2.sh #!/bin/bash f="/tmp/aminglinux2" if [ -x $f ] then echo $f exeable fi [root@xuexi-001 shell]# sh file2.sh [root@xuexi-001 shell]# 由于不能够执行,因此没有任何输出
f="/tmp/aminglinux2" [ -f $f ] && rm -f $f //前一条命令执行成功才会继续执行以后的命令 等同于下面的表达方式 if [ -f $f ] then rm -rf $f fi
f="/tmp/aminglinux2" [ -f $f ] || touch $f //前面命令不成功时,执行后面的命令 if [ ! -f $f ] // “!”表示了若是这条命令不成功,就往下执行 then touch $f fi