意外的文件结尾

WxP*_*lot 5 bash

有人可以解释为什么文件的结尾在第 49 行是意外的吗?(第49行是最后一行之后的一行)

#!/bin/bash 

timeend=$(date -u +%H%M)
timestart=$(date --date "$timeend 30 minutes ago" -u +%H%M)
firsttime=0

while true
do
    if [[ $firsttime -eq 0 ]]; then
    time=$timestart
    increment=0
    fi
    if [[ $firsttime -ne true ]]; then
    increment=$(( $increment + 2 ))
    time=$(( $timestart + $increment ))
    fi
    if [[ $time -ge $timeend ]]; then
    break
    fi 

    gpnids << EOF
    RADFIL   = NEXRIII|CLT|TR0
    RADTIM   = "$time"
    TITLE    = 1/-2
    PANEL    = 0
    DEVICE   = gif|radar"$increment".gif|1280;1024|C
    CLEAR    = Y
    TEXT     = 1/2/2/hw
    COLORS   = 7
    WIND     =  
    LINE     =  
    CLRBAR   =  
    IMCBAR   = 5/v/LL/.005;.6/.4;.01
    GAREA    = dset
    MAP      = 24 + 23 + 1/1/2 + 14 + 15/1/2
    LATLON   = 0
    OUTPUT   = t

    $mapfil = lorvus.usg + hicnus.nws + hipona.nws + louhus.nws + loisus.nws
    run

    exit
    EOF
    firsttime=1

    gpend

 done
Run Code Online (Sandbox Code Playgroud)

ter*_*don 12

您还应该收到另一个错误,这可能会提供更多信息:

/home/terdon/scripts/b.sh:第 49 行:警告:此处文档在第 21 行由文件结尾分隔(需要“EOF”)

/home/terdon/scripts/b.sh:第 50 行:语法错误:文件意外结束

您的错误是在结束heredoc 的字符串之前有空格。举一个简单的例子,这抱怨:

#!/bin/bash 

cat << EOF
   hello
   EOF
Run Code Online (Sandbox Code Playgroud)

但这不会:

#!/bin/bash 

cat << EOF
   hello
EOF
Run Code Online (Sandbox Code Playgroud)

  • @WxPilot _非常_ 错误。在很多情况下,空格是 bash 中的一个问题。例如`foo = bar` 失败,`foo= bar` 也是如此,在许多情况下bash 会自动在空格等处进行拆分。除非绝对必要,否则您应该始终避免使用它。 (2认同)

Oli*_*Oli 11

我得到两行应该可以帮助你弄清楚发生了什么:

./test: line 48: warning: here-document at line 21 delimited by end-of-file (wanted `EOF')
./test: line 49: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

您的heredoc( << EOF) 构造形式不正确。它对空格敏感,因此您可以将其剥离:

...
    command <<EOF
        ...
EOF
Run Code Online (Sandbox Code Playgroud)

或者让它知道你正在使用标签(并且它必须是标签):

...
    command <<-EOF
        ...
    EOF
Run Code Online (Sandbox Code Playgroud)

我更喜欢第二个,因为它可以让你更好地构建脚本......你的脚本已经可以从中受益。