开发者

What is the proper indentation for bash scripts?

开发者 https://www.devze.com 2022-12-20 13:16 出处:网络
What is the proper indentation 开发者_JS百科for a bash script?As a java/c++ monkey I religiously indent my code.But it seems you are not allowed to indent this code:

What is the proper indentation 开发者_JS百科for a bash script? As a java/c++ monkey I religiously indent my code. But it seems you are not allowed to indent this code:

#! /bin/bash

if [ $# = 0 ]
then
        # there was no arguments => just do to standard output.
        echo "there are no parameters"
else
cat << EOF
==========================================================
==========================================================
==========================================================
==========================================================
DESCRIPTION:

$1
----------------------------------------------------------

EOF
fi

When indented it does not recognize the EOF and if you just unindented the EOF (confusing) it prints indented.

Q: What is the proper indenting for bash scripts?


With bash (3.2 at least) and ksh (do not know about others) you can indent the here-documents using <<-, and the leading tabs will be stripped (not spaces, only tabs), e.g.

if [...]; then
    cat <<-EOF
        some text
    EOF
fi


yes you can "indent", by using <<- (see bash man page on here documents)

if [ $# = 0 ]
then
        # there was no arguments => just do to standard output.
        echo "there are no parameters"
else
    cat <<-EOF
    ==========================================================
    ==========================================================
    ==========================================================
    ==========================================================
    DESCRIPTION:

    $1
    ----------------------------------------------------------
    EOF
fi


This is not a bash indenting problem, this is a here-file problem. The label that you specify after <<, i.e., EOF, must appear alone in a line, without leading or trailing whitespaces.

For the here-file itself, it is used as typed, indentation included.


Mouviciel is correct.

You can put the here-file text in a separate file if you want to preserve indentation. You would then have to handle the substitution yourself, however.

0

精彩评论

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