bash-if block 不工作

标签 bash shell

<分区>

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

test.sh[3]: 第 7 行语法错误:`then' 意外

谁能帮帮我?谢谢!!

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

最佳答案

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

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

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

编辑:

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

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

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

关于bash-if block 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925182/

相关文章:

linux - 如何在 Linux 中使用 bash 压缩 90 天的旧文件并将其移动到特定文件夹

linux - 比 tee 更好的输出到控制台和输出文件的方法?

linux - 用于查找使用 KVM/virsh 命令创建的虚拟机的 IP 地址的 Shell 脚本

linux - 如何按日期对文件进行排序并保存排序的文件(用于转换附加图像)?

linux - Bash fork 子 shell

macos - .bash_profile 中的别名无法正常工作

linux - 如何使用awk/sed增加文件中数字的匹配模式?

linux - 在 Linux 中对两个文件进行排序并找到每个文件唯一的行

linux - 如何在不实际尝试登录的情况下检查 SSH 服务器是否正在监听主机

python - 定期更新文件的代码