bash 脚本条件部分中的 EOT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36054419/
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
EOT in conditional section of bash script
提问by anark10n
So, i'm trying to get an ftp script working but i'm hitting a snag. Here's the script:
所以,我试图让 ftp 脚本工作,但我遇到了障碍。这是脚本:
#!/bin/bash
HOST='192.168.178.122'
USER='ftpuser'
PASSWD='passa.2015'
DATE=`date +%d-%m-%Y`
FILE="archive-"$DATE".tar.gz"
prep=0
echo "File is="$FILE
echo "Prepare_val="$prep
if [ $prep -eq 0 ]
then
find Web -maxdepth 1 -mindepth 1 -not -type l -print0 | tar --null --files-from - -cpzvf $FILE
ftp -n $HOST << EOT
user $USER $PASSWD
put $FILE
quit
bye
EOT
fi
When i try and run this script, it returns the following error:
当我尝试运行此脚本时,它返回以下错误:
ftp-script.sh: 22: ftp-script.sh: Syntax error: end of file unexpected (expecting "fi")
If i remove the EOT section, it executes fine, but the EOT is the only means by which the ftp commands can be run without needing user intervention. Does anyone know how to place an EOT in a conditional without causing the error I get.
如果我删除 EOT 部分,它可以正常执行,但 EOT 是无需用户干预即可运行 ftp 命令的唯一方法。有谁知道如何在条件中放置 EOT 而不会导致我得到的错误。
回答by Simone
The closing EOT
must be at the beginning of the line, with no previous spaces or tabs.
Try this:
结束EOT
必须在行首,前面没有空格或制表符。尝试这个:
ftp -n $HOST << EOT
user $USER $PASSWD
put $FILE
quit
bye
EOT
回答by riteshtch
You can persist your indentation to have better readability like this:
您可以坚持缩进以获得更好的可读性,如下所示:
contents of script.bash:
script.bash 的内容:
#!/bin/bash
#normal usage
cat <<EOF
abcd
xyz
EOF
echo "*************************"
#using heredoc without script indentation
if [[ true ]]; then
cat <<EOF
abcd
xyz
EOF
fi
echo "*************************"
#using heredoc with script indentation
if [[ true ]]; then
cat <<-EOF
abcd
xyz
EOF
fi
Output:
输出:
$ ./script.bash
abcd
xyz
*************************
abcd
xyz
*************************
abcd
xyz
$
Bottom line: use <<-EOT
instead of <<EOT
(Note the hyphen) to persist your indentation
底线:使用<<-EOT
而不是<<EOT
(注意连字符)来保持缩进