bash - 这里是与VI交互的文档
问题描述
我只是想弄清楚 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.
任何人都可以帮助解决此错误吗?我还假设 ^[ 代表被按下的退出按钮?
解决方案
vi
并且vim
旨在在终端中以交互方式使用,而不是编写脚本。
使用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
它通常是它的非可视版本。
推荐阅读
- javascript - 如何在 OpenLayers 中的两个遥远多边形之间创建飞行动画
- audio - 为什么我的音高对象听起来与 PRAAT 中的原始音频文件不同?
- amazon-web-services - Terraform:放置扩展策略时出错:AccessDeniedException:帐户未授权
- azure-cosmosdb - Azure Cosmos api 客户端选择 Azure.Cosmos 还是 Azure.DocumentDB.Core?
- excel - 数据透视表显示某些结果的多行,但不显示其他结果
- c++ - 构建 CMake 项目后 Visual Studio 中的引用问题
- pandas - 使用方法链接将可变长度切片分配给列
- grpc - gRPC 端到端处理元数据(标头)
- python - 将一个特定的数据点标记添加到箱线图或小提琴图(使用 holoviews / hvplot)
- android - 如何从 Xamarin Android 生成 App Bundle(可以上传到 Google Play 商店)?