我有下面的代码.这里的逻辑是如果HostList包含任何blanck条目它应该将类设置为空白,否则它应该是红色.现在我收到错误 -
任何人都可以帮助我吗?谢谢!
#! /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
Run Code Online (Sandbox Code Playgroud)
Bash对空白非常敏感.这应该工作:
if [ "%s" = "" ]; then
Run Code Online (Sandbox Code Playgroud)
注意,=它用于字符串比较,-eq用于整数.
编辑:
更确切地说,bash将原始代码拆分为:
if[%s # supposedly a command
-eq # parameter
=""] # parameter
; # end of command
then # keyword
Run Code Online (Sandbox Code Playgroud)
在这一点上,Bash意识到有一个unmatchen then关键字,甚至没有尝试运行if[%s(这也会失败).