2013-10-26 98 views
2

我写了一个bash脚本,并且当我测试一个变量是否为空的条件时收到一个错误。bash意外的令牌,然后错误

下面是一个示例脚本:

我没有提到被执行来给变量赋值和FNE但

#! /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 

错误的命令:靠近意外的标记语法错误“那么” 。

我想这样的另一个变化:

#! /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 

再次相同的错误。

当我尝试比较这样:

if[ "$a" == "" ]; then 

再次相同的错误。

我不确定错误的原因是什么。变量a的值是这样的:

一些与它(1):[X,Y]

它包含,空格,括号,逗号,冒号。我将变量名用双引号括起来进行比较。

+0

很难弄清楚为什么你正在使用一个循环。你永远不会使用变量'f'。 – devnull

回答

6

您的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进行编辑,这将有语法着色你的错字......

+0

谢谢。我已经理解了:) –