bash 如何在 shell 脚本中使用 ctrl-D
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3948708/
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 use ctrl-D in a shell script
提问by Shruti
I have a bash shell script that requires ctrl-D to break out of the terminal.can anyone tell me how to use it in the shell script
我有一个 bash shell 脚本,需要 ctrl-D 来退出终端。谁能告诉我如何在 shell 脚本中使用它
example
例子
ssh host last --->displays the list of users who were logged on to that host
ssh host last --->显示登录到该主机的用户列表
I have different hosts the output is appended to one final and when I'm executing this particular shell script along with other series of commands, i'm using ctrl-D to see the output
我有不同的主机,输出被附加到一个 final,当我执行这个特定的 shell 脚本和其他一系列命令时,我使用 ctrl-D 查看输出
suppose my shell script is myscript.sh to execute myscript.sh
假设我的 shell 脚本是 myscript.sh 来执行 myscript.sh
./myscript.sh
./myscript.sh
ctl-D
ctl-d
connection to host closed
与主机的连接已关闭
output is displayed
显示输出
Instead, I want to use ctrl-D in my script file
相反,我想在我的脚本文件中使用 ctrl-D
回答by Ignacio Vazquez-Abrams
There is no way to do this directly. Use a heredoc to feed stdin instead.
没有办法直接做到这一点。使用 heredoc 来代替标准输入。
./someprog.sh << EOF
something here
EOF
回答by George Vasiliou
You could try exec <&-
&-
is used to close a file descriptor (ps:everything in linux is a kind of file...)<&-
is closing file descriptor 0 = stdin - can also be written as 0<&-
您可以尝试exec <&-
&-
用于关闭文件描述符(ps:linux 中的所有内容都是一种文件...)<&-
正在关闭文件描述符 0 = stdin - 也可以写为0<&-
If you open a normal terminal in your linux machine and type exec <&-
you will see your terminal to close/dissapear like if you press ^D.
如果你在你的 linux 机器上打开一个普通的终端并输入exec <&-
你会看到你的终端关闭/消失,就像你按下一样^D.
PS1: Similarly, exec >&-
closes stdout
PS1:同样,exec >&-
关闭标准输出
PS2: If you close stdin with exec <&-
you can re-open to continue your script with something like exec </dev/tty
PS2:如果您关闭标准输入,exec <&-
您可以重新打开以继续使用类似的脚本 exec </dev/tty
回答by tsds
To detect Ctrl-D in scripts and run custom logic on it you can read input char by char and check if char key code corresponds to Ctrl-D.
要检测脚本中的 Ctrl-D 并在其上运行自定义逻辑,您可以逐个字符读取输入字符并检查字符键代码是否对应于 Ctrl-D。
Here is a code example:
这是一个代码示例:
IFS=
while true; do
read -n 1 key
line=$line$key
printf -v key_code "%d" "'$key"
if [ $key_code -eq 4 ]; then
echo "Ctrl-D pressed!"
fi
if [ "$key" = '' ]; then
echo "Line was $line"
line=
fi
done
回答by Orachigami
Use this
用这个
read Input
#When ^D is used, read ends up with the exit code "1"
if [[ $? == 1 ]]
then
echo "[Ctrl+D] is captured"
fi
回答by spraff
#! /bin/sh
echo 'Enter one line at a time'
echo 'Press CTRL+D to finish'
cat >test.out
echo 'OK'
回答by dogbane
Why don't you tell last
how many lines you want to see? Then you won't have to interrupt it with a Ctrl+D. For example, to see 10 lines use:
你为什么不说last
你想看多少行?这样你就不必用 Ctrl+D 打断它了。例如,要查看 10 行,请使用:
ssh hostname "last -10"