linux cat 进入文件,包括所有从复制+粘贴中退出的代码

719*_*016 2 linux bash command-line cat

我可以将代码打印到 STDOUT 来模拟 Ctrl+c 内部的行为cat吗?例如,我希望我的脚本打印出cat命令,后跟文件的内容,如下所示:

cat > /my/file/name.txt
I want this line in the filename above
and this one
and this one as well but I want to exit cat in the next line
Ctrl+c somehow
Run Code Online (Sandbox Code Playgroud)

我想要的行为是让用户复制上面的行,然后将它们粘贴到终端窗口中,无需再打字,就有一个name.txt包含三行内容的文件,保存,然后返回到交互式终端提示。

有任何想法吗?

小智 5

使用此处文档功能:

cat > /my/file/name.txt <<EOF
I want this line in the filename above
and this one
and this one as well but I want to exit cat in the next line
EOF
Run Code Online (Sandbox Code Playgroud)


Raú*_*udo 5

如果您按 Ctrl+D(这表示输入结束),您甚至不需要此处文档。

Ctrl+C 表示对进程发出 SIGINT(发送中断信号),而 Ctrl+D 则为当前从标准输入读取的进程提供文件结束符。