我编写了一个 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
对于编辑,它会对您的拼写错误进行语法着色...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)