bash意外的令牌然后错误 [英] bash unexpected token then error

查看:95
本文介绍了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...

这篇关于bash意外的令牌然后错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆