带有空字符串的BASH文件测试运算符
当path参数为空字符串时,BASH文件测试操作符返回什么?例如:带有空字符串的BASH文件测试运算符,bash,Bash,当path参数为空字符串时,BASH文件测试操作符返回什么?例如: directory="" # Something may or may not set "directory" if [ -d "$directory" ]; then # Do something... fi 您需要引用$directory: if [ -d "$directory" ]; then 否则,在扩展之后,您将得到 if [ -d ]; then 这相当于if[-n“-d”];然后,情况总是如此。(-
directory=""
# Something may or may not set "directory"
if [ -d "$directory" ]; then
# Do something...
fi
您需要引用
$directory
:
if [ -d "$directory" ]; then
否则,在扩展之后,您将得到
if [ -d ]; then
这相当于
if[-n“-d”];然后
,情况总是如此。(-d
是非空字符串。)您需要引用$directory
:
if [ -d "$directory" ]; then
否则,在扩展之后,您将得到
if [ -d ]; then
这相当于
if[-n“-d”];然后
,情况总是如此。(-d
是非空字符串。)在您的示例中,该语句将生成以下测试:
if [ -d ]; then
...
fi
这不会测试你想要的
防止此类错误的公认做法是将变量用双引号括起来,如下所示:
if [ -d "$directory" ]; then
...
fi
在您的示例中,该语句将生成以下测试:
if [ -d ]; then
...
fi
这不会测试你想要的
防止此类错误的公认做法是将变量用双引号括起来,如下所示:
if [ -d "$directory" ]; then
...
fi