bash line continuation切断

时间:2015-10-16 19:55:54

标签: linux bash shell

我有一个小的bash脚本bdocs.sh

#!/bin/bash

REPO=$1

QUICKBOOK=/usr/bin
AUTOINDEX=/usr/bin
XSLTPROC=/usr/bin
DOCBOOK_DTD=/usr/share/xml/docbook/schema/dtd/4.2
DOCBOOK_XSL=/usr/share/xml/docbook/stylesheet/docbook-xsl

if [[ -n "$REPO" ]]; then
    echo cmake -DBOOSTQUICKBOOK_ROOT=$QUICKBOOK             \
        -DBOOSTAUTOINDEX_ROOT=$AUTOINDEX                    \ 
        -DXSLTPROC_ROOT=$XSLTPROC                           \ 
        -DDOCBOOK_DTD_ROOT=$DOCBOOK_DTD                     \
        -DDOCBOOK_XSL_ROOT=$DOCBOOK_XSL                     \ 
        -DHPX_WITH_DOCUMENTATION=On                         \ 
        $REPO
fi

然而,如果我称之为> ./bdocs.sh ../repo,它只会回声:

cmake -DBOOSTQUICKBOOK_ROOT=/usr/bin -DBOOSTAUTOINDEX_ROOT=/usr/bin  
./build_docs.sh: line 14: -DXSLTPROC_ROOT=/usr/bin: No such file or directory
./build_docs.sh: line 16: -DDOCBOOK_DTD_ROOT=/usr/share/xml/docbook/schema/dtd/4.2: No such file or directory
./build_docs.sh: line 17: -DHPX_WITH_DOCUMENTATION=On: command not found
./build_docs.sh: line 18: h: command not found

在将第二个命令行参数传递给cmake后,我的行继续是怎么回事?

2 个答案:

答案 0 :(得分:3)

在行尾\之后一定不能有空格。

答案 1 :(得分:1)

效率稍低但更具可读性的是使用cat和此处的文档:

#!/bin/bash

REPO=$1

QUICKBOOK=/usr/bin
AUTOINDEX=/usr/bin
XSLTPROC=/usr/bin
DOCBOOK_DTD=/usr/share/xml/docbook/schema/dtd/4.2
DOCBOOK_XSL=/usr/share/xml/docbook/stylesheet/docbook-xsl

if [[ -n "$REPO" ]]; then
    cat <<EOF
cmake -DBOOSTQUICKBOOK_ROOT=$QUICKBOOK
      -DBOOSTAUTOINDEX_ROOT=$AUTOINDEX
      -DXSLTPROC_ROOT=$XSLTPROC
      -DDOCBOOK_DTD_ROOT=$DOCBOOK_DTD
      -DDOCBOOK_XSL_ROOT=$DOCBOOK_XSL
      -DHPX_WITH_DOCUMENTATION=On
      $REPO
EOF
fi

如果生成的文件格式不重要(只是生成器的格式),请使用数组来保存cmake个参数;换行是隐含的。

#!/bin/bash

REPO=$1

QUICKBOOK=/usr/bin
AUTOINDEX=/usr/bin
XSLTPROC=/usr/bin
DOCBOOK_DTD=/usr/share/xml/docbook/schema/dtd/4.2
DOCBOOK_XSL=/usr/share/xml/docbook/stylesheet/docbook-xsl

arguments=(-DBOOSTQUICKBOOK_ROOT=$QUICKBOOK
           -DBOOSTAUTOINDEX_ROOT=$AUTOINDEX
           -DXSLTPROC_ROOT=$XSLTPROC
           -DDOCBOOK_DTD_ROOT=$DOCBOOK_DTD
           -DDOCBOOK_XSL_ROOT=$DOCBOOK_XSL
           -DHPX_WITH_DOCUMENTATION=On
)
if [[ -n "$REPO" ]]; then
    echo cmake "${arguments[@]}" "$REPO"
fi