【发布时间】:2020-05-19 19:31:55
【问题描述】:
我正在尝试编写一个 Shell 脚本来编译一个 c 程序并检查它是否打印“Hello World!”到标准输出。 这就是我现在所拥有的。 diff 中的第二个参数检查是一个只有“Hello World!”的文件。在里面。
#!/bin/sh
gcc -Wall hello.c -o hello
./hello > outfile
if diff outfile check >/dev/null ; then
echo Same
else
echo Different
fi
在我尝试运行脚本后,我的 Ubuntu 一直显示此错误。
"./build.sh: 8: ./build.sh: Syntax error: "fi" unexpected (expecting "then")"
请让我知道我的代码有什么问题。
【问题讨论】:
-
您的脚本运行良好——您是如何运行它的?以
"sh -x build.sh"运行并发布输出。还要在check上发布一个十六进制转储(例如hexdump -Cv check的输出) -
我怀疑你的脚本文件有 DOS/Windows 行尾;见this question。
-
次要问题:您在这里讨论的是 POSIX shell 脚本,但您的问题使用 bash 标签。请删除它,因为这里没有与 bash 相关的内容。