目录
1、Here Document概述
■ 使用I/O重定向的方式将命令列表提供给交互式程序
■ 标准输入的一种替代品
■ 语法格式
命令<<标记
…
…
标记
■ Here Document使用注意事项
● 标记可以使用任意合法字符
● 结尾的标记一定要顶格写, 前面不能有任何字符
● 结尾的标记后面也不能有任何字符(包括空格)
● 开头标记前后的空格会被省略掉
1.1、Here Document免交互
■ 通过read命令接收输入并打印
[root@localhost ~]# vim here_non_interactive_read.sh
#!/bin/bash
read i <<EOF
Hi
EOF
echo $i
[root@localhost ~]# chmod +X here_non_interactive_read.sh
[root@localhost ~]# ./here_non_interactive_read.sh
[root@localhost ~]# Hi
■ 通过passwd给用户设置密码
[root@localhost ~]# vim here_non_interactive_passwd.sh
#!/bin/bash
passwd tom <<EOF
This_ is_ password
This_ is_ password
EOF
[root@localhost ~]# chmod +x here_ non_interactive_passwd.sh
[root@localhost ~]# ./here_non_interactive_passwd.sh
[root@localhost ~]#
1.2、Here Document变量设定
■ 变量替换
[root@localhost ~]# vim here_var_replace.sh
#!/bin/bash
doc_file="2019.txt"
i="company"
cat> $doc_ file << HERE
Take him from home to $i
HERE
[root@localhost ~]# chmod +x here_var_replace.sh
[root@localhost ~]# ./here_var_replace.sh
[root@localhost ~]# cat 2019.txt
Take him from home to company
■ 变量设定
[root@localhost ~]# vim here_var_set.sh
#!/bin/bash
ivar="Great! Beautyful!"
myvar=$(cat <<EOF
This is Line 1.
That are Sun,Moon and Stars.
$ivar
EOF
)
echo $myvar
[root@localhost ~]# sh here_var_set.sh
This is Line 1. That are Sun,Moon and Stars. Great<