开发者

Why are my here-docs (<<-) giving me a syntax error?

methods() {
        cat <<-!
        start
        stop
        restart
        reload
        status
        methods
        !
}

Is this correct I am getting error

syntax error: unexp开发者_运维知识库ected end of file


For here-docs in ancient shells, you have to match the tag exactly. That means:

methods() {
        cat <<!
        start
        stop
        restart
        reload
        status
        methods
!
}

Yes, at the start of the line, although you could do tricky things like cat <<'^I!' to set the marker to a single tab followed by !.

Now bash (and possibly earlier shells) fixed that with the <<- variant which strips off all leading tabs from your data lines and the end marker before processing. That way, you could still indent nicely:

methods() {
        cat <<-!
        start
        stop
        restart
        reload
        status
        methods
        !
}

But, note the proviso: it strips tabs, not whitespace in general. If you have spaces (or any non-tab character, printable or otherwise) anywhere before that ! character, it won't work.

If you're using vi, you can enter :set list to see the non-printable characters a bit better, otherwise xd or od -xcb can give you a hex dump of your file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜