1.04 Heredocument与参数替换

heredocument输出内容是可以含有变量,但是变量是否被替换取决于heredocument的limit string是否被引用

不被引用的情况 #

#!/bin/bash
Name="wangteng"
Respondent="the author of the fine scripts"
cat <<EOF
hello,there ,$Name
greetings to you,$Name,from $Respondent
EOF

exit 0

执行输出

hello,there ,wangteng
greetings to you,wangteng,from the author of the fine scripts

引用的情况 #

#!/bin/bash
Name="wangteng"
Respondent="the author of the fine scripts"
cat <<'EOF'
hello,there ,$Name
greetings to you,$Name,from $Respondent
EOF

exit 0

执行输出

hello,there ,$Name
greetings to you,$Name,from $Respondent

利用禁用参数替换的特点来实现使用脚本生成脚本或其他代码 #

#!/bin/bash

outfile=generated.sh

(
cat <<'EOF'
#!/bin/bash

echo "this is a generated shell script"
echo "generated file will be named $outfile"

a=7
b=3

let "c = $a * $b"
echo "c = $c"
exit 0
EOF
) > $outfile

if [ -f "$outfile" ]
then
  chmod 755 $outfile
else
  echo "have in creating file: \"$outfile\""
fi

exit 0

上一页➡️

下一页➡️

Calendar Aug 29, 2022
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者