Bash subshel​​l用ssh命令非常奇怪的行为 [英] Bash subshell very weird behavior with ssh command

查看:74
本文介绍了Bash subshel​​l用ssh命令非常奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑脚本s2:

#!/bin/bash
ssh localhost tail

和脚本s1

#!/bin/bash
./s2 &
sleep 1000
wait

现在调用s1.它不会调用ssh命令.

Now call s1. It won't call the ssh command.

如果您删除&在s1中会. 如果直接调用./s2,它将. 如果您在控制台中输入./s2&会的.

If you remove the & in s1 it will. If you call ./s2 directly it will. If you type in the console ./s2 & it will.

但是./s1不起作用.为什么?

But ./s1 won't work. Why ?

推荐答案

我不知道它不会调用ssh命令.如果我取消睡眠,它将立即退出.

I don't know for me it does not call the ssh command. If i remove the sleep then it exits immediately.

确定它调用了ssh命令.一定要!没有魔术.

Sure it calls the ssh command. It has to! There's no magic.

假设一个大错误是因为仅在删除<​​c1>时脚本立即退出,而没有调用ssh. 这个结论是不正确的. 可能还有其他原因来解释这种观察到的行为.

It's a big mistake to assume that just because the script exits immediately when you remove sleep, it is not calling ssh. That conclusion is incorrect. There can be other reasons to explain this observed behavior.

如果您想验证ssh没有运行, 一种方法是查看系统日志.

If you wanted to verify that ssh did not run, one way to do that would be to look at your system logs.

该脚本很快就会退出 (不是立即退出),因为ssh很快就会退出. 运行s2时,它会收到空的stdintail命令不消耗任何东西,什么也不输出, 因此ssh shell终止而没有任何输出.就是这样.

The script exits very soon (not immediately) because ssh exits very soon. When s2 runs, it receives empty stdin, which the tail command consumes and outputs nothing, and so the ssh shell terminates without any output. That's all.

s2更改为此,以查看区别:

Change s2 to this to see the difference:

#!/bin/bash
ls / | ./s2 &
wait

这样,s2将具有一个非空的stdin可以使用, 并会产生一些输出.

This way s2 will have a non-empty stdin to work with, and should produce some output.

wait使得sleep 1000从一开始就没有必要.

Btw the sleep 1000 was unnecessary from the beginning, thanks to the wait.

这篇关于Bash subshel​​l用ssh命令非常奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆