代码: 全选
#!/bin/bash
# generate-script.sh
# 这个脚本的诞生基于Albert Reiner的一个主意.
OUTFILE=generated.sh
# -----------------------------------------------------------
(
cat <<'EOF'
#!/bin/bash
echo "This is a generated shell script."
# Note that since we are inside a subshell,
#+ we can't access variables in the "outside" script.
echo "Generated file will be named: $OUTFILE"
# Above line will not work as normally expected
#+ because parameter expansion has been disabled.
# Instead, the result is literal output.
a=7
b=3
let "c = $a * $b"
echo "c = $c"
exit 0
EOF
) > $OUTFILE
# -----------------------------------------------------------
# 将'limit string'引用起来将会阻止上边
#+ here document消息体中的变量扩展.
# 这会使得输出文件中的内容保持here document消息体中的原文.
if [ -f "$OUTFILE" ]
then
chmod 755 $OUTFILE
else
echo "Problem in creating file: \"$OUTFILE\""
fi
exit 0
代码: 全选
./generate-script: 行 50: 警告: 立即文档在第 11 行被文件结束符分隔 (需要 `EOF')
./generate-script: 行 51: 语法错误: 未预期的文件结尾