【发布时间】:2012-06-06 03:30:26
【问题描述】:
这是将此处文档的内容分配给变量的一种方法。但是,它的执行将返回状态 1 而没有说明原因。
#! /bin/bash
# set -e -x
# This implementation returns 1
define(){ IFS='\n'; read -r -d '' ${1}; }
define thedoc <<'EOF'
Here is my here doc.
There was an ASCII banana here too,
but `read` would just it concatenate to mush.
EOF
# The here document will print with the following when `set -e` in not invoked.
echo $thedoc
只要set -e 已关闭,所有内容都会通过检查甚至执行进行检查。这不是上面的 Banana 独有的,而是上面 define() 构建的任何此处的文档。该错误从何而来?
【问题讨论】:
-
我不确定它是什么意思,但是当我运行你的代码时,所有的 'n' 都被替换为
thedoc值中的空格。 -
这种行为是意料之中的。香蕉也许是个糟糕的选择。 :P
标签: bash shell variable-assignment