Bash EOF 技巧

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Bash EOF 技巧

Bash EOF 技巧

文章目录

EOF适用场景:

  • 命令行多行输出
  • 脚本包装
  • 类型配置文件

1. 命令行输出

$ cat << EOF
> Hello
> EOF
Hello

2. 写入文本

cat << EOF >1.txt
111
222
333
EOF

复制终端是这样的。

cat << EOF >1.txt
> 111
> 222
> 333
> EOF

回车后

$ cat 1.txt
111
222
333

3. 追加文本

cat << EOF >> 1.txt
444
555
666
EOF

查看1.txt内容

$ cat 1.txt
111
222
333
444
555
666

4. 覆盖文本

cat << EOF >1.txt
aaa
bbb
ccc
EOF

查看

$ cat 1.txt
aaa
bbb
ccc

5. 自定义 EOF

cat << a > 1.txt
111
222
333
a

输出:

$ cat 1.txt
111
222
333

6. 另一种格式

  • cat > filename <<EOF
  • cat << EOF > filename
cat > 1.txt <<EOF
123
456
789
EOF

查看

$ cat 1.txt
123
456
789

追加内容

cat >> 1.txt <<EOF
abc
def
ghi
EOF

查看内容

$ cat 1.txt
123
456
789
abc
def
ghi

7. 示例

7.1 配置文件

或者cat << EOF > /usr/local/mysql/my.cnf

cat > /usr/local/mysql/my.cnf << EOF         
[client]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
basedir = /usr/local/mysql/
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
sync_binlog=1
log_bin = mysql-bin
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
port = 3306
EOF

7.2 新建分区并挂载

$ cat auto_add_disk.sh         
#!/bin/bash
fdisk  /dev/sdb  <<EOF
n
p
1
wq
EOF
/sbin/mkfs .ext4  /dev/sdb1  &&   /bin/mkdir  -p  /data  &&  /bin/mount  /dev/sdb1  /data
echo  'LABEL=data_disk /data ext4 defaults 0 2'  >>  /etc/fstab

7.3 设置变量

$ sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)
$ echo -e "$sql"

7.4 输出脚本

cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF

查看内容

$ cat print.sh
#!/bin/bash
echo $PWD
echo /home/user

7.5 匹配输出

$ cat <<EOF | grep 'b' | tee b.txt
> foo
> bar
> baz
> EOF
bar
baz
$ cat b.txt
bar
baz

7.6 json 文本

cat >> /etc/docker/daemon.json < EOF
{
   "exec-opts": ["native.cgroupdriver=systemd"],
   "log-driver": "json-file",
   "log-opts": {
   "max-size":  "100m"
    },
   "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
 }
 EOF

查看

$ cat /etc/docker/daemon.json
{
   "exec-opts": ["native.cgroupdriver=systemd"],
   "log-driver": "json-file",
   "log-opts": {
   "max-size":  "100m"
    },
   "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
 }

参考:

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如&quot;password&quot;、&quot;confidential&quot;、&quot;secret&quot;),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
166 0
|
4月前
|
Linux Shell Windows
4:Bash shell命令-步入Linux的现代方法
4:Bash shell命令-步入Linux的现代方法
83 0
|
Ubuntu 安全 Linux
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
233 0
|
11月前
|
关系型数据库 MySQL Shell
【Linux命令】-bash: mysql: command not found
【Linux命令】-bash: mysql: command not found
127 0
|
4月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
100 0
Linux Bash 脚本中的 IFS 是什么?
|
2月前
|
存储 Shell Linux
Linux|创建和使用 Bash 别名
Linux|创建和使用 Bash 别名
42 6
|
4月前
|
Java Shell Linux
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
|
4月前
|
Shell Linux
【Linux】Bash支持各种指令选项的原理:命令行参数
【Linux】Bash支持各种指令选项的原理:命令行参数
|
4月前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)