linux - bash脚本的奇怪行为

标签 linux bash ssh

我有以下 script.sh:

#!/bin/bash
ssh server1 "echo hello from server1"
ssh server2 "echo hello from server2"

当作为 cat ./script.sh | 执行时bash 我明白了

hello from server1

当它作为 bash ./script.sh 执行时,我得到了

hello from server1

hello from server2

谁能解释一下区别? :)

最佳答案

在第一种情况下,cat 的输出连接到 bash 和 ssh 的标准输入。 ssh 从它的 stdin 读取,因此消耗了 cat 的其余输出,尽管在这种情况下结果被丢弃,因为远程命令本身从不读取 stdin。

在第二种情况下,bash 和 ssh 的标准输入是您的终端,bash 单独打开脚本文件,因此 ssh 看不到它。

关于linux - bash脚本的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6829750/

相关文章:

Java:监听远程 Linux 服务器上的目录更改

java - 脚本如何获取不同类型的命令行参数并将其提供给 java 程序?

bash - 使用 date 命令获取下个月?

git - git 可以通过 ssh 端口转发工作吗?

来自 Ed25519 OpenSSL pem 私钥的 ssh-keygen

bash:通过 ssh 并行执行命令

mysql - 将简单的 postgreSQL 函数转换为 Mysql

c - 带有 Glut 的键盘处理程序

linux - 如何在 archlinux 中使用 shadowsocksR?

linux -/usr/bin/cut : Argument list too long in bash script