bash 如何将 ctrl+d 插入到我的 linux 脚本中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33459219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to insert ctrl+d into my linux script?
提问by Gum Bi
I want to make the following commands:
我想执行以下命令:
cat > template.txt [enter in the terminal] text [Ctrl+d in the terminal]
cat > template.txt [在终端中输入] text [在终端中使用Ctrl+d]
in a script.
在脚本中。
Is there a way to tell the script to do enter\Ctrl d? Is there a way to create a file and write to it in script?
有没有办法告诉脚本执行 enter\Ctrl d?有没有办法创建一个文件并在脚本中写入它?
I didn't find anything that worked for me.
我没有找到任何对我有用的东西。
Thanks.
谢谢。
回答by Jameson
A Here Documentis kind of like a script version of what you're talking about, I think, although it is not entirely clear to me from your description.
一个here文档是有点像你在说什么有关的脚本版本,我认为,虽然它不是从你的描述完全清楚给我。
#!/bin/bash
cat > template.txt <<- EOF
Here
is some
text.
EOF
Ctrl-D itself isthe EOF character, ASCII 4.
Ctrl-D 本身是EOF 字符,ASCII 4。
回答by Walter A
When you want an interactieve user enter lines and add them to your file until the user enters an ^D you can use the next script:
如果您希望交互用户输入行并将它们添加到您的文件中,直到用户输入 ^D,您可以使用下一个脚本:
echo "Please give input"
while read -r line; do
echo "Enter next line or ^D"
echo "${line}" > template.txt
done
echo "After loop"
You do not have to check for ^D, that will be recognized by read without doing something extra.So you do not need to use CTRL-V CTRL-D
in vi
.
您不必检查 ^D,它会被 read 识别而无需做任何额外的事情。所以您不需要使用CTRL-V CTRL-D
in vi
。