bash-if 块不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9925182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:54:06  来源:igfitidea点击:

bash-if block not working

bashshell

提问by Monojit

I have below code.The logic here is if HostList conatains any blanck entry it should set class as blank, else it should be red.Now I am getting error-

我有下面的代码。这里的逻辑是,如果 HostList 包含任何空白条目,它应该将类设置为空白,否则它应该是红色的。现在我收到错误 -

test.sh[3]: syntax error at line 7 : `then' unexpected

test.sh[3]:第 7 行的语法错误:“然后”意外

can any one help me out?Thanks!!

任何人都可以帮助我吗?谢谢!

     #! /bin/bash
file=./HostList.txt
{
  echo "<table>"
  printf "<tr>"
  if[%s -eq =""]; then
      class="blank"
  else
    class="red"
  fi    
    "<td" $class">%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    </tr>\n" $(cat "$file"|cut -d'.' -f1)
  echo "</table>"
}  > table.html


exit 0

回答by user123444555621

Bash is very sensitive about whitespace. This should work:

Bash 对空格非常敏感。这应该有效:

if [ "%s" = "" ]; then

Note that =is used for string comparison and -eqis used for integers.

请注意,=用于字符串比较并-eq用于整数。

edit:

编辑:

More precisely, bash splits your original code like this:

更准确地说,bash 像这样拆分您的原始代码:

if[%s # supposedly a command
-eq # parameter
=""] # parameter
; # end of command
then # keyword

At this point, Bash realizes that there is an unmatchen thenkeyword, and doesn't even try to run if[%s(which would fail too).

此时,Bash 意识到存在一个不匹配的then关键字,并且甚至不会尝试运行if[%s(这也会失败)。

回答by paxdiablo

I'm not sure what all that HTML markup is doing in there but the ifstatement should be something like:

我不确定所有 HTML 标记在那里做什么,但if语句应该是这样的:

if [[ $something == "" ]] ; then
    # do something
fi

In other words, you need some spaces between the brackets and the arguments, at a bare minimum.

换句话说,括号和参数之间至少需要一些空格。

回答by chepner

First, your ifstatement needs some spacing, and the -eqis unnecessary:

首先,您的if语句需要一些间距,并且-eq是不必要的:

if [ %s = "" ]; then
    class="blank"
else
    class="red"
fi

But more importantly, %sis not a variable, so you can't compare it to anything (or as pointed out in the comments, not usefully). It's just a placeholder for the printfcommand. You're going to have to be a little more explicit:

但更重要的是,%s它不是一个变量,所以你不能将它与任何东西进行比较(或者如评论中指出的那样,没有用)。它只是printf命令的占位符。你将不得不更明确一点:

hosts=($(cat "$file"))
echo "<table>"
echo "<tr>"
for host in ${hosts[@]}; do
    host=$(echo $host | cut -d'.' -f1)
    if [ "$host" = "" ]; then
        echo "<td class='blank'>"
    else
        echo "<td class='red'>"
    fi
done
echo "</tr>\n"
echo "</table>"

(The preceding has been minimally tested.)

(前面已经过最低限度的测试。)