bash 意外 token 然后错误

标签 bash

我编写了一个 bash 脚本,但在测试变量是否为空的条件时收到错误。

下面是一个示例脚本:

我没有提到为变量 a 和 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

错误:意外标记“then”附近的语法错误。

我尝试了另一种变体:

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

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

最佳答案

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 进行编辑,它会使您的拼写错误带有语法颜色...

关于bash 意外 token 然后错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604420/

相关文章:

linux - 重启进程脚本linux

bash - 并行化 bash 脚本

bash - 逐行读取文件并将值赋给变量

用于生成 HTML 模板的 Bash 脚本

python - 如何在 bash 脚本中捕获来自 Python 的输入

php - 反引号 `` 叫什么?

linux - 使用 sed 从文件 linux 替换单词

linux - Bash:等到 CPU 使用率低于阈值

Bash - 将 odp/ppt 幻灯片导出为图像

bash - 如何比较 shell 脚本中两个目录中的文件名?