bash - Bash script variable not being passed via ssh
问题描述
I have a bash script which ssh's to a server, and depending on the status of a variable performs a task:
#!/bin/bash
foo=$1
ssh user@host.com '
echo In host
if [ "$foo" == "yes" ]; then
echo "Foo!"
fi
'
When I run sh script.sh yes
, although the ssh
command works, the conditional evaluates to false. I can see this if I echo $foo
- it prints an empty line. How can I access the value of foo
within the ssh
command?
解决方案
变量不会传输到远程机器。您可以在通过 ssh 发送的代码中扩展该变量,但您必须非常小心,因为它为不受控制的代码执行打开了大门:
#!/bin/bash
foo=$1
ssh user@host.com '
echo In host
if [ "'"$foo"'" == "yes" ]; then
echo "Foo!"
fi
'
现在想象一下(不要尝试)如果foo='$(rm -rf /)'
.
推荐阅读
- ios - 如何在 iOS 中启用应用安装跟踪
- ios - 将两个 UIView 添加到 UIScrollView,如 Swift 中的 HORIZONTAL ViewPager
- java - 简单 for 循环上的 Java vs C++ 速度
- flutter - 检测小部件是否溢出
- python - TypeError:('关键字参数不理解:','子样本')
- ios - iOS - 如何在使用自定义 UIBezierPath 绘制的 CAShapeLayer 中设置 CATextLayer?
- powershell - 如何在 import vapp 命令中使用哈希表键值和数组?
- android - 禁用 HideBottomViewOnScrollBehaviour
- nginx - 为同一服务器上的多个应用程序配置 Nginx 的问题
- java - 将 Java 类型映射到数据库类型