SSH会话-为什么bash FOR循环甚至不显示值并且还使用本地机器 [英] SSH session - why bash FOR loop is not even showing value and also using LOCAL machine

查看:139
本文介绍了SSH会话-为什么bash FOR循环甚至不显示值并且还使用本地机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下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 ==

我有几个问题:

  1. 为什么第一个for循环不打印变量$v的值?
  2. 为什么(当我看到目标服务器的/tmp/<folder>中有有效文件时),第二个for循环没有打印$f变量的值?
  3. 如何获得第二个for循环以打印两个.tar.gz文件?
  4. 我尝试使用$(...)或使用反引号将输入值(对于for循环)包装在第二个for循环中,但没有效果.由于某些原因,它期望这些*文件使用本地计算机.为什么?
  1. Why doesn't the first for loop print the values of the variable $v?
  2. Why (when I see the target server have valid files in /tmp/<folder>) the second for loop did not print the values of the $f variable?
  3. How can I get the second for loop to print the two .tar.gz files?
  4. I tried $(...) or using back quotes to wrap the input values (for for loop) in the second for 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆