[英]bash variable substitution from remote command using sed
我试图将用户“ demouser”添加到远程服务器的/ etc / sudoers中,并且我想从变量中传递用户名。
这可行,但是我想使用变量$USERNAME
代替demouser
ssh centos@$remote_host -t 'sudo sed -i "\$ademouser ALL=(ALL) NOPASSWD:ALL" /etc/sudoers'
我尝试使用此功能,但无法正常工作。
export USERNAME=demouser
ssh centos@remote_host bash -c "'sudo sed -i "\$a$USERNAME ALL=(ALL) NOPASSWD:ALL" /etc/sudoers'"
Error: -bash: syntax error near unexpected token `('
参数将不会以单引号引起来,可以将其关闭,而改为以双引号引起来:
ssh user@host 'sed "s/'"$localVar"'/replacement/" file'
^^
|Enter double quotes to avoid word splitting and globbing
Exit single quotes to expand on client side.
但是,您应该知道发送到服务器的命令是:
sed "s/abc/replacement/" file
这可能会导致问题,因为我们现在在服务器上使用双引号,一个也可以发送单引号,但很快就会变得一团糟:
ssh user@host 'sed '\''s/'"$localVar"'/replacement/'\'' file'
^ ^
| Escaped remote single quote
Close local single quote
这将变为:
sed 's/abc/replacement' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.