【问题标题】:Shell Script Looping not working while on remote SSH在远程 SSH 上,Shell 脚本循环不起作用
【发布时间】:2020-07-22 18:52:37
【问题描述】:

我一直在尝试通过 SSH 远程命令访问时进行循环工作,但它不起作用。该命令在主机中直接运行时有效。

scp file root@${SERVER}:/tmp
ssh ${SERVER} "
while IFS= read -r line; do
   echo "$line"
done </tmp/file
"

我曾尝试在主脚本中使用单引号,但它会导致错误。

bash: line n: warning: here-document at line n delimited by end-of-file 

任何建议将不胜感激。

更新

测试文件

1
2
3
4
5
6

脚本测试

SERVER='client'
ssh ${SERVER} '
echo "inside remote ClientServer"
echo "cat testfile"
cat /tmp/testfile

while read line; do
   echo "${line}"
done <</tmp/testfile
'
echo "Back to MasterServer..."

终端结果

root@server]# ./test
S
Kernel 4.14.35-1902.10.7.el7uek.x86_64 on an x86_64

inside remote ClientServer
cat testfile
1
2
3
4
5
6
bash: line 8: warning: here-document at line 8 delimited by end-of-file (wanted `/tmp/testfile')
Back to MasterServer...

谢谢。

【问题讨论】:

    标签: linux loops unix ssh remote-server


    【解决方案1】:

    您可能希望使用单引号逐字传递远程命令:

    scp file root@${SERVER}:/tmp
    ssh ${SERVER} '
    while IFS= read -r line; do
       echo "$line"
    done </tmp/file
    '
    

    确保您使用的是&lt;/tmp/file,而不是&lt;&lt;/tmp/file。序列&lt;&lt; 用于启动here-document,在这种情况下这不是您想要的。

    【讨论】:

    • 我确实尝试使用单引号,但出现错误。我还在遥控器中推送了一个脚本,然后通过 SSH 调用它,但也无法正常工作。
    • @emyatsuna 你能复制/粘贴包含代码和结果的终端会话吗?
    • @emyatsuna 你也可以复制/粘贴file的内容吗?
    • 猫测试文件 1 2 3 4 5 6
    • 嗨@rtx13,我只需要将脚本重新复制到新脚本。它解决了问题!谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-01-17
    • 2023-02-23
    • 2014-08-29
    • 2015-12-21
    相关资源
    最近更新 更多