我试图捕获scp命令到Bash中的变量或数组的输出。我不太清楚这条短信是从本地还是远程发来的?
到目前为止,我已经尝试了以下几点:
#!/bin/bash -x
mytty=$(tty)
echo "mytty is ${mytty}"
myvar=$(scp test.js crashdog@webserver.com:/home/crashdog 2>&1 > $mytty)
echo "myvar is ${myvar}"我可以看到我的tty上的文本如下:
test.js 100% 1200 1.2KB/s 00:00 但我的心仍然是空的。那么我的两个问题,上面的文字是从哪里来的?以及如何将文本分配给Bash中的变量或数组。
谢谢
发布于 2022-06-18 11:32:21
您的问题是,当scp的stdout不是终端时,它不会输出任何内容。
要欺骗它,您可以使用script
out=$(script -qefc "scp test.js crashdog@webserver.com:/home/crashdog" /dev/null)
echo "$out"您可以在How to trick an application into thinking its stdout is a terminal, not a pipe中找到有关它的更多信息。
https://stackoverflow.com/questions/72668210
复制相似问题