目录1. 命令行输出2. 写入文本3. 追加文本4. 覆盖文本5. 自定义 EOF6. 另一种格式7. 示例7.1 配置文件7.2 新建分区并挂载7.3 设置变量7.4 输出脚本7.
EOF适用场景:
$ cat << EOF
> Hello
> EOF
Hello
cat << EOF >1.txt
111
222
333
EOF
复制终端是这样的。
cat << EOF >1.txt
> 111
> 222
> 333
> EOF
回车后
$ cat 1.txt
111
222
333
cat << EOF >> 1.txt
444
555
666
EOF
查看1.txt内容
$ cat 1.txt
111
222
333
444
555
666
cat << EOF >1.txt
aaa
bbb
ccc
EOF
查看
$ cat 1.txt
aaa
bbb
ccc
cat << a > 1.txt
111
222
333
a
输出:
$ cat 1.txt
111
222
333
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
或者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
$ 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
$ sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)
$ echo -e "$sql"
cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF
查看内容
$ cat print.sh
#!/bin/bash
echo $PWD
echo /home/user
$ cat <<EOF | grep 'b' | tee b.txt
> foo
> bar
> baz
> EOF
bar
baz
$ cat b.txt
bar
baz
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"
]
}
How does “cat << EOF” work in bash?
What is Cat EOF in Bash Script?
到此这篇关于Bash EOF的使用小结的文章就介绍到这了,更多相关Bash EOF内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Bash EOF的使用小结
本文链接: https://lsjlt.com/news/177513.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0