gpt4 book ai didi

linux - 获取语​​法错误 : "fi" unexpected (expecting "then") in shell scripts

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:36 25 4
gpt4 key购买 nike

我正在为备份设置一些 shell 脚本。一切正常,但在使用 if then 语句时出现语法错误。

查看https://www.tutorialspoint.com/unix/if-else-statement.htm他们使用一个简单的 if 语句,该语句在网站上有效,因为它有一个试用弹出部分。当我将内容复制到另存为 .sh 的文件中并在我的 ubuntu 服务器 (16.04.2 x64) 上尝试时,我收到语法错误:“fi”意外(期待“then”)

网站代码如下

#!/bin/sh
a=10
b=20

if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

要在我使用的终端上运行

sh /path/to/file/script.sh

有几个问题被问到并得到了回答,但似乎没有一个与我的情况相同。我需要在服务器上更新什么吗?如果我使用 bash 而不是 sh,它会显示意外标记。有人遇到过这个问题吗?

如果可能有帮助,当我在终端中运行时,它似乎不喜欢空格,下面是完整的错误

sh /var/www/check-day.sh
: not foundeck-day.sh: 4: /var/www/check-day.sh:
/var/www/check-day.sh: 10: /var/www/check-day.sh: Syntax error: "fi"
unexpected (expecting "then")

最佳答案

您的脚本中有 CR。使用 dos2unix 修复。

关于linux - 获取语​​法错误 : "fi" unexpected (expecting "then") in shell scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com