如题 shell script 中 if [ $1 ] 和 if [ -a $1] 区别
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/04 10:13:17
如题 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来替换.