这里是要与VI交互的文档

问题描述 投票:0回答:2

我只是想弄清楚EOF和此处的文件。我在网上阅读了一些可以扩展的好主意。应该使用此处文档创建一个文本文件并向其中添加文本。语法如下:

#!/bin/sh
filename=test.txt
vim $filename <<EndOfCommands
i
This file was created automatically from
a shell script.
^[
ZZ
EndOfCommands

不幸的是,我现在收到一个错误:

./EOF.sh 
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...

Vim: Finished.

任何人都可以帮助解决此错误吗?我还假设^ [代表被按下的逃生按钮?

bash shell vim vi heredoc
2个回答
1
投票

vivim旨在在终端中交互使用,而不是编写脚本。

改为使用ed,它可以愉快地接受来自所有类型的标准输入源的输入:

#!/bin/sh
filename=test.txt
ed -s "$filename" <<EndOfCommands
i
This file was created automatically from
a shell script.
.
w
EndOfCommands

((在插入模式下,带有单个句点的行表示输入结束并返回到命令模式,类似于在vi(m)中的转义。)

使用ex是另一种选择;在安装了vim的系统上,它通常是它的非v常规版本。


0
投票

使用vim:

filename="test.txt"
vim -c ":wq! $filename" - << EOF
This file was created automatically from
a shell script.
EOF

参见:man vim

© www.soinside.com 2019 - 2024. All rights reserved.