简体   繁体   English

bash脚本的正确缩进是什么?

[英]What is the proper indentation for bash scripts?

What is the proper indentation for a bash script? 什么是bash脚本的正确缩进? As a java/c++ monkey I religiously indent my code. 作为一个java / c ++猴子,我虔诚地缩进我的代码。 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. 缩进时,它不能识别EOF,如果你只是缩进EOF(令人困惑),它会打印缩进。

Q: What is the proper indenting for bash scripts? 问: bash脚本的正确缩进是什么?

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), eg 使用bash(至少3.2)和ksh(不了解其他人)你可以使用<<-缩进here-documents,并且前导标签将被剥离(不是空格,只有标签),例如

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

yes you can "indent", by using <<- (see bash man page on here documents) 是的,你可以“缩进”,使用<<- (参见这里文件的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

This is not a bash indenting problem, this is a here-file problem. 这不是bash缩进问题,这是一个here-file问题。 The label that you specify after << , ie, EOF , must appear alone in a line, without leading or trailing whitespaces. << ,即EOF之后指定的标签必须单独出现在一行中,不带前导或尾随空格。

For the here-file itself, it is used as typed, indentation included. 对于here-file本身,它用作包含的类型,缩进。

Mouviciel is correct. Mouviciel是对的。

You can put the here-file text in a separate file if you want to preserve indentation. 如果要保留缩进,可以将here-file文本放在单独的文件中。 You would then have to handle the substitution yourself, however. 然而,您必须自己处理替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM