[英]bash unexpected token then error
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.我编写了一个 bash 脚本,但在测试变量是否为空的条件时收到错误消息。
Below is an example script:下面是一个示例脚本:
I have not mentioned the commands that are executed to assign values to variables a and fne but我没有提到为变量 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
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
again same error.又是同样的错误。
when I try comparing this way:当我尝试以这种方式进行比较时:
if[ "$a" == "" ]; then
again same error.又是同样的错误。
I am not sure what is the reason for the error.我不确定错误的原因是什么。 The value of variable a is like this:
变量a的值是这样的:
Something with it (1) : [x, y]与它有关的东西 (1) : [x, y]
it contains, spaces, brackets, comma, colon.它包含空格、括号、逗号、冒号。 I am enclosing the variable name in double quotes in comparison.
相比之下,我将变量名用双引号括起来。
You are missing the space after the if
:您缺少
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
Side note: if you were using vi
for editing, it would have syntax-colored your typo...旁注:如果您使用
vi
进行编辑,它会为您的错字添加语法颜色...
if [ ! -z "$1" ]; then
echo "is not empty"
else
echo "is empty"
fi
Try This!尝试这个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.