在通过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...谢谢。
发布于 2020-04-10 02:44:30
您可能希望使用单引号逐字传递远程命令:
scp file root@${SERVER}:/tmp
ssh ${SERVER} '
while IFS= read -r line; do
echo "$line"
done </tmp/file
'确保您使用的是</tmp/file,而不是<</tmp/file。序列<<用于启动这里的文档,在本例中这不是您想要的。
https://stackoverflow.com/questions/61133245
复制相似问题