如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别

来源:学生作业帮助网 编辑:作业帮 时间:2024/07/14 18:23:42
如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别
xUn@~@9Ŭr"WD9,t-Ye]*-)UPJD#Ԇ$jЦyb"1t1䲳;off[5T劚i!"-Q(GEPbk8x0-m$jԀB~`mI(cռ2P{?R_t^ X}ߛkvy QB&#Q- :87'26яxWjRu Zǃo63HL< QREaj_2t۟fg9k i~1 n?WHB#%}yMiD f ofTDkYJNh\~[ءVȣWL6Wug.C ]:{7รPqEފV"X(LbdF;

如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别
如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别

如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别
if [ $1 ]  判断参数是否存在

-bash-4.1$ sh one.sh 
please para:
-bash-4.1$ sh one.sh testOne
para: testOne
-bash-4.1$ cat one.sh 
#!/bin/bash

if [ $1 ]
then
   echo "para: $1"      #有参数testOne 输出此行
else
   echo "please para:"  #没有参数testOne 输出此行
fi
-bash-4.1$


if [ -a $1]  判断文件或目录是否存在,即$1变量上面对应的是testOne

-bash-4.1$ ls 
one.sh  te.sh  testOne  two.sh //此目录下,文件列表
-bash-4.1$ sh two.sh adf     // 没有这个文件adf
no exist file: adf
-bash-4.1$ sh two.sh testOne    // 有这个文件夹testOne
exist file :  testOne
-bash-4.1$ cat two.sh          //脚本内容
#!/bin/bash
if [ -a $1 ]
then
   echo "exist file :  $1"
else
   echo "no exist file: $1"
fi
-bash-4.1$
-a现在一般情况下都会用-e来替换.