SSH会话-为什么bash FOR循环甚至不显示值并且还使用本地机器 [英] SSH session - why bash FOR loop is not even showing value and also using LOCAL machine
问题描述
我正在运行以下SSH命令:
I'm running this SSH command:
ssh -p 2001 -q root@12.13.14.15 \
" echo
echo Now On Target Server, I see
ls -l /tmp/Folder-1.1.0.1053/*
for v in A B C ; do
echo === $v===
done
echo
for f in /tmp/Folder-1.1.0.1053/* ; do
echo File is == $f
done
"
它正在打印:
Now On Target Server, I see
-rw------- 1 root root 159790 Jan 23 17:03 /tmp/Folder-1.1.0.1053/file1-1.8.30.tar.gz
-rw------- 1 root root 116731 Jan 23 17:03 /tmp/Folder-1.1.0.1053/file2-2.7.49.tar.gz
=== ===
=== ===
=== ===
File is ==
File is ==
我有几个问题:
- 为什么第一个
for
循环不打印变量$v
的值? - 为什么(当我看到目标服务器的
/tmp/<folder>
中有有效文件时),第二个for
循环没有打印$f
变量的值? - 如何获得第二个
for
循环以打印两个.tar.gz
文件? - 我尝试使用
$(...)
或使用反引号将输入值(对于for
循环)包装在第二个for
循环中,但没有效果.由于某些原因,它期望这些*
文件使用本地计算机.为什么?
- Why doesn't the first
for
loop print the values of the variable$v
? - Why (when I see the target server have valid files in
/tmp/<folder>
) the secondfor
loop did not print the values of the$f
variable? - How can I get the second
for
loop to print the two.tar.gz
files? - I tried
$(...)
or using back quotes to wrap the input values (forfor
loop) in the secondfor
loop but it has no effect. For some reason, it's expecting those*
files using the local machine. Why?
如果相关,则我的Bash版本是"GNU bash,版本4.1.2(1)-发行版(x86_64-redhat-linux-gnu)",而我的Linux版本是"Red Hat Enterprise Linux Server 6.9版(圣地亚哥) )".
If it's relevant, my Bash version is "GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)", and my Linux version is "Red Hat Enterprise Linux Server release 6.9 (Santiago)".
推荐答案
由于要用双引号包装ssh命令,因此所有$
变量都将由本地shell内插,然后再通过电线传递到远程壳.使用单引号或转义$
信号.
Because you're wrapping your ssh command with double quotes, all your $
variables are being interpolated by the local shell before going over the wire to the remote shell. Either use single quotes or escape your $
sigils.
例如:
ssh -p 2001 -q root@12.13.14.15 "echo; echo Now On Target Server, I see; ls -l /tmp/Folder-1.1.0.1053/*; for v in A B C; do echo === \$v===; done; echo; for f in /tmp/Folder-1.1.0.1053/*; do echo File is == \$f; done"
或
ssh -p 2001 -q root@12.13.14.15 'echo; echo Now On Target Server, I see; ls -l /tmp/Folder-1.1.0.1053/*; for v in A B C; do echo === $v===; done; echo; for f in /tmp/Folder-1.1.0.1053/*; do echo File is == $f; done'
请参阅以下非常重要的说明.仅转义在SSH会话(例如此处的for
循环)或SSH会话内的任何位置创建和使用的变量. 不不要转义在脚本中和SSH会话外部定义的变量,例如:for
循环的命令/输入部分中使用的变量.即for v in ${staging_area}/*.tar.gz; do echo ==\$v;done"
在这里我逃脱了\$v
但没有逃脱${staging_area}
See very Important note below. Escape only the variable created and used within the SSH session (ex: for
loop here) or any where inside SSH session. Do NOT escape a variable which was defined in your script and outside of SSH session, ex: as used in the command / input part in for
loop. i.e. for v in ${staging_area}/*.tar.gz; do echo ==\$v;done"
here I escaped \$v
but did not escape ${staging_area}
这篇关于SSH会话-为什么bash FOR循环甚至不显示值并且还使用本地机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!