1 采用 [ 时,有四个地方必须有空格,即 if [ -f "$FILE" ];then echo exists; fi
2 采用test命令时,有三个地方必须有空格,即 if test -f "$FILE";then echo exists; fi
#!/bin/bash
#test file exists
FILE="1"
if [ -e "$FILE" ];then echo exists; fi
if test -e "$FILE";then echo exists; fi
if [ -e "$FILE" ] ; then echo exists; fi
if [ -e "$FILE" ]; then echo exists; fi
if [ -e "$FILE" ]; then echo exists; fi
[ -e "$FILE" ] && echo exists
test -e "$FILE" && echo exists
以上写法都是合法的。
你之所以需要三个或四个空格,是因为[本质上是一个Linuxe命令。
The reason you need a space is because [ is actually a command line. Type which [ and you will see what that it is in /usr/bin. You can write any if [...];then command as if test ...;then