if 语句中的 bash EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9349616/
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:35:45  来源:igfitidea点击:

bash EOF in if statement

bashconditionaleof

提问by Arthur

I am trying to do an action in a IF ELSE statement in Bash, but receive an error like this one:

我正在尝试在 Bash 的 IF ELSE 语句中执行操作,但收到如下错误:

Syntax error: end of file unexpected (expecting "fi")

语法错误:文件结尾意外(期望“fi”)

Now I am quite new to this, so probably the solution to my problem should not be that difficult :)

现在我对此很陌生,所以我的问题的解决方案可能不那么困难:)

if [ "$DAYNAME" = 'Sunday' ]; then
    echo 'The backup will be uploaded'
    ftp -n $HOST <<EOF
        quote USER $USER
        quote PASS $PASSWD
        put $filetoday
        delete ${filethreeweeksago}
        quit
    EOF
fi

Of course the vars are already filled.

当然,vars 已经被填满了。

I think it has to do with the EOF notation, because when i remove them, the problem disapears. Unfortunatly I don't know how to use the code without the EOF notation.

我认为这与 EOF 符号有关,因为当我删除它们时,问题就消失了。不幸的是,我不知道如何使用没有 EOF 符号的代码。

Can anyone tell me why this error is comming up?

谁能告诉我为什么会出现这个错误?

回答by cnicutar

Drop the blanks and it should work:

删除空白,它应该可以工作:

    EOF
^^^^

回答by Taleeb

add a dash to EOF if you want to keep the tabs: from:

如果要保留选项卡,请在 EOF 中添加破折号:从:

ftp -n $HOST <<EOF

to:

到:

ftp -n $HOST <<-EOF

回答by Ritesh Jhaggar

EOL inside If-Else

If-Else 中的 EOL

Syntax of EOL can be this type

EOL 的语法可以是这种类型

if [ condition ]; then
    echo "Comment"
else
    cat >> file.txt <<-EOL

    EOL
fi