如何退出从 bash 脚本创建的 ssh 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15458805/
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 exit from ssh session created from bash script?
提问by user784637
I have the following bash script test.sh
我有以下 bash 脚本 test.sh
#!/bin/bash
ssh -o "StrictHostKeyChecking no" user@
#do some stuff
exit
However if I run this script ./test.shI need to enter ctrl+dto get out of the ssh session and it appears as if the exitcommand is not working the way I intended.
但是,如果我运行此脚本./test.sh,则需要输入ctrl+d才能退出 ssh 会话,并且该exit命令似乎没有按我预期的方式工作。
Is there any way I can exit this ssh session that was created from this bash script?
有什么办法可以退出从这个 bash 脚本创建的这个 ssh 会话吗?
回答by Basile Starynkevitch
You may want to code a here document
您可能想对此处的文档进行编码
#!/bin/bash
ssh -o "StrictHostKeyChecking no" user@ << ENDHERE
here do some remote stuff
also here
exit
ENDHERE
If you code <<-ENDHEREthe initial spaces or tabs in each line of the here document are suppressed. Parameters (like $foo) are substituted unless you code <<\ENDHERE, and the ENDHEREcan actually be any word, conventionally in capitals.
如果您<<-ENDHERE对此处文档的每一行中的初始空格或制表符进行编码,则会被抑制。$foo除非您编码<<\ENDHERE,否则将替换参数(如),并且ENDHERE实际上可以是任何单词,通常为大写。
Read the Advanced Bash Scripting guide(which does have some imperfections)
阅读Advanced Bash Scripting 指南(它确实有一些缺陷)

