bash意外的令牌然后错误
我已经编写了一个bash脚本,并且在测试变量是否为空的条件时收到错误消息.
I have written a bash script and I am receiving an error when I am testing a condition whether a variable is empty or not.
下面是一个示例脚本:
我没有提到执行将变量a和fne赋值的命令,但是
I have not mentioned the commands that are executed to assign values to variables a and fne but
#! /bin/bash
for f in /path/*
do
a=`some command output`
fne=`this command operates on f`
if[ -z "$a" ]
then
echo "nothing found"
else
echo "$fne" "$a"
fi
done
错误:意外令牌"then"附近的语法错误.
error: syntax error near unexpected token, "then".
我尝试了另一种类似的方法:
I tried another variation like this:
#! /bin/bash
for f in /path/*
do
a=`some command output`
fne=`this command operates on f`
if[ -z "$a" ]; then
echo "nothing found"
else
echo "$fne" "$a"
fi
done
同样的错误.
当我尝试以这种方式进行比较时:
when I try comparing this way:
if[ "$a" == "" ]; then
同样的错误.
我不确定错误的原因是什么.变量a的值如下:
I am not sure what is the reason for the error. The value of variable a is like this:
与之相关的事情(1):[x,y]
Something with it (1) : [x, y]
包含空格,方括号,逗号,冒号.相比之下,我将变量名用双引号引起来.
it contains, spaces, brackets, comma, colon. I am enclosing the variable name in double quotes in comparison.
您在if
之后缺少空格:
#! /bin/bash
for f in /path/*
do
a=`some command output`
fne=`this command operates on f`
if [ -z "$a" ]; then
echo "nothing found"
else
echo "$fne" "$a"
fi
done
旁注:如果您使用vi
进行编辑,则它会为您的错别字添加语法颜色...
Side note: if you were using vi
for editing, it would have syntax-colored your typo...