cat-tee命令结合重定向功能实现文本内容写入

cat-tee命令结合重定向功能实现文本内容写入

Cat命令结合重定向功能实现文本内容写入

  • 将stdin标准输入的内容重定向到test.txt文件(若此文件不存在则创建),且当stdin中含有EOF时完成写入
  • cat 追加内容用 >>,覆盖内容用 >
  • 其中EOF可以替换为任意字符串

写入内容到文本,覆盖文本原有内容

cat > /root/test.txt << EOF
this is first line 
this is second line
this is thrid line
EOF

追加内容到文本,保留文本原有内容

cat >> /root/test.txt << EOF
this is fourth line
this is fifth line
EOF

脚本中的变量问题

文本中的$TAG变量会被直接赋值,对第一个EOF加引号即可避免

TAG=8.0
cat > /root/run_mysql.sh << "EOF"
#!/bin/bash
docker run -it -d \
    --name mysql \
    -e MYSQL_ROOT_PASSWORD=my-secret-pw \
    mysql:$TAG
EOF

文本内容中的缩进

cat > /root/test.txt << EOF
linegroup1
	this is first line 
	this is second line
linegroup2
	this is thrid line
	this is fourth line
EOF

空格缩进执行结果

# cat test.txt 
linegroup1
    this is first line 
    this is second line
linegroup2
    this is thrid line
    this is fourth line

tab缩进执行结果

# cat test.txt 
linegroup1
this is first line 
this is second line
linegroup2
this is thrid line
this is fourth line

第二个EOF前含有制表符

如果在脚本中使用cat EOF并且存在如下缩进格式,可以看到第二个EOF前含有缩进:

cat test.sh 
#!/bin/bash
if true;
then
        cat > /root/test.txt <<- EOF
        this is first line
        this is second line
        this is thrid line
        EOF
fi

缩进存在两种情况:tab制表符缩进和空格缩进,这种缩进会导致制表符或空格被判定为结尾符,而非EOF,针对制表符缩进将第一个EOF改为<<- EOF格式即可解决。

man bash中的说明:

# man bash | grep "<<-"
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line  containing  delimiter.

但是针对空格缩进该方法并不能解决问题,第二个EOF前如果为空格缩进必须修改为制表符缩进。

查看文本缩进格式,^I表示制表符,$表示换行符

:set list
:set list TAB 

直接将EOF输出传递给命令

# Create multiple YAML objects from stdin
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep-less
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000"
EOF

tee命令也可实现类似cat命令的效果,同时将写入内容输出到屏幕上

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://ng7k8581.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

如何确保 tee 命令追加信息到文件中?

默认情况下,在同一个文件下再次使用 tee 命令会覆盖之前的信息。如果你想的话,可以通过 -a 命令选项改变默认设置。基本上,-a 选项强制 tee 命令追加信息到文件。

[command] | tee -a [file]

如何让 tee 写入多个文件?

这非常之简单。你仅仅只需要写明文件名即可。

[command] | tee [file1] [file2] [file3]

如何让 tee 命令的输出内容直接作为另一个命令的输入内容?

使用 tee 命令,你不仅可以将输出内容写入文件,还可以把输出内容作为另一个命令的输入内容。比如说,下面的命令不仅会将文件名存入 output.txt 文件中,还会通过 wc 命令让你知道输入到 output.txt 中的文件数目。

ls file* | tee output.txt | wc -l

如何使用 tee 命令提升文件写入权限?

假如你使用Vim 编辑器打开文件,并且做了很多更改,然后当你尝试保存修改时,你得到一个报错,让你意识到那是一个 root 所拥有的文件,这意味着你需要使用 sudo 权限保存修改。

如此情况下,你可以在 Vim 内使用 tee 命令来提高权限。

:w !sudo tee %

上述命令会向你索要 root 密码,然后就能让你保存修改了。

如何让 tee 命令忽视中断?

-i 命令行选项使 tee 命令忽视通常由 ctrl+c 组合键发起的中断信号(SIGINT)。

[command] | tee -i [file]

当你想要使用 ctrl+c 中断该命令,同时让 tee 命令优雅的退出,这个选项尤为实用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值