我编写了一个 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/