虚拟化容器,大数据,DBA,中间件,监控。

Linux cat EOF在shell脚本里面的使用详解

20 12月
作者:admin|分类:系统运维


1、cat >file记录的是键盘输入,相当于从键盘创建文件,并且只能创建新文件,不能编辑已有文件。>是数据重导向,会将你输入的文本内容输出到file中。

[root@localhost ~]# cat >test.txt <<EOF
> hellow this is test
> EOF
[root@localhost ~]# cat test.txt 
hellow this is test

2、cat <<EOF

cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的,EOF是"end of file",表示文本结束符。

结合这两个标识,即可避免使用多行echo命令的方式,并实现多行输出的结果

 

【范例1】 使用cat >> file <<EOF实现多行文件的输入到文件


[root@localhost ~]# cat >>test.txt<<EOF
> log_facility=daemon
> pid_file=/var/run/nrpe.pid
> server_port=5666
> nrpe_user=nagios
> nrpe_group=nagios
> allowed_hosts=127.0.0.1,nagios.xcar.com.cn,mon.xcar.com.cn
> dont_blame_nrpe=0
> debug=0
> command_timeout=60
> connection_timeout=300
> command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
> EOF

[root@localhost ~]# cat test.txt 
hellow this is test
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,nagios.xcar.com.cn,mon.xcar.com.cn
dont_blame_nrpe=0
debug=0
command_timeout=60
connection_timeout=300
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

【范例2】shell脚本使用cat > file <<EOF实现多行输出到文件


[root@localhost ~]# cat oracle_insatll.sh 
#/bin/bsh
#This program configure the environment for oracle database
#2020-4-27
#########################################
rac1="rac1"
rac1_priv="rac1-priv"
rac1_vip="rac1-vip"

rac2="rac2"
rac2_priv="rac2-priv"
rac2_vip="rac2-vip"

cat >> /etc/host <<EOF
10.1.70.158  $rac1
10.1.70.161  $rac1_vip
11.1.1.1     $rac1_priv

10.1.70.159  $rac2
10.1.70.162  $rac2_vip
10.1.1.2  $rac2_priv
EOF


[root@localhost ~]# ./oracle_insatll.sh 
[root@localhost ~]# cat /etc/host
10.1.70.158  rac1
10.1.70.161  rac1-vip
11.1.1.1     rac1-priv

10.1.70.159  rac2
10.1.70.162  rac2-vip
11.1.1.2 rac2-priv

 

浏览455 评论0
返回
目录
返回
首页
Linux SetGID(SGID) Linux kill killall pkill 使用方法