linux - 在远程 SSH 上,Shell 脚本循环不起作用
问题描述
我一直在尝试通过 SSH Remote 命令访问时进行循环工作,但它不起作用。该命令在主机中直接运行时有效。
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...
谢谢你。
解决方案
您可能希望使用单引号逐字传递远程命令:
scp file root@${SERVER}:/tmp
ssh ${SERVER} '
while IFS= read -r line; do
echo "$line"
done </tmp/file
'
确保您使用的是</tmp/file
,而不是<</tmp/file
. 该序列<<
用于启动此处的文档,这在这种情况下不是您想要的。
推荐阅读
- python - 如何在python中计算双元音?
- video - FFMPEG 从文件目录中对每个视频文件进行 1 秒剪辑,直到达到总播放时间
- swift - 如何关闭已打开以供阅读的 AKAudioFile(或 AVAudioFile)?
- python - 如何将值作为一组来制作字典?
- node.js - 在 node.js 中编译依赖项
- excel - 添加数据时添加边框
- javascript - 在带有 v-for 的模板上使用 :key
- spring - Spring Reactor Retry.retryWhen() 不起作用
- blazor - 客户端 blazor 无法加载两个隔离的 js 文件
- shopify - Google Ads 和 Tag Assistant 将我的再营销事件代码段显示为无效