【问题标题】:Create a file with content through ssh without a text editor在没有文本编辑器的情况下通过 ssh 创建包含内容的文件
【发布时间】:2013-09-29 00:22:48
【问题描述】:

我知道可以通过“touch”命令在 ssh 中创建一个文件,然后使用 vi 或 pico 等文本编辑器对其进行编辑并添加内容。

我想知道是否可以创建一个文件并将其内容添加到一个命令行中? 比如:

[create file command] [filename.txt] ["this is the contents of filename.txt"]

我问这是否可能的原因是因为我有一个ssh client in Go,其中一个会话只接受一个调用来运行,我打算将它用于一个没有用户输入的自动化应用程序,所以我想避免使用文本编辑器。

【问题讨论】:

    标签: ssh openssh


    【解决方案1】:

    这里有一个解决方案:

    ssh [user@]hostname 'echo "this is the contents of filename.txt" > <path/filename>'
    

    带有“>”的回显会覆盖现有文件,如果不存在则创建一个新文件。

    将“>”替换为“>>”以将文本附加到现有文件中。

    干杯

    【讨论】:

    • 谢谢,这行得通。我猜“ssh [user@]hostname”是连接到远程,但是 Go 中的 ssh 客户端已经连接,所以我只需要运行“'echo”这是 filename.txt 的内容”>/tmp/filename .txt'" 我已经准备好了,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2017-12-11
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多