且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 ssh 传递外部 shell 脚本变量

更新时间:2023-12-04 22:46:10

你的问题:你的整个命令都放在单引号中——显然是为了 bash 表达式在服务器上而不是在本地展开.

Your problem: Your entire command is put into single quotes – obviously so that bash expressions are expanded on the server and not locally.

但这也适用于您的 $1.

简单的解决方案:通过将本地变量包装到单引号中来中断"引用.

Simple solution: "Interupt" the quotation by wrapping your local variable into single quotes.

ssh root@192.168.0.1 'echo "#date added $(date +%m/%d/%Y)" >> /var/named/chroot/etc/named.conf; echo "zone "'$1'" { type master; file "/etc/zone/dummy-block"; };" >> /var/named/chroot/etc/named.conf'

注意:"$1""'$1'".

注意:此解决方案是对上述问题中发布的单行的简单修复.如果这个脚本有一点可能被其他人执行,或者它可以处理任何类型的外部输出,请查看 Charles达菲的解决方案.

NOTE: This solution is a simple fix for the one-liner as posted in the question above. If there's the slightest chance that this script is executed by other people, or it could process external output of any kind, please have a look at Charles Duffy's solution.