[英]bash variable substitution from remote command using sed
I am trying to add a user "demouser" to /etc/sudoers of a remote server and I want to pass the username from a variable. 我试图将用户“ demouser”添加到远程服务器的/ etc / sudoers中,并且我想从变量中传递用户名。
This works, but I want to use a variable $USERNAME
instead of demouser 这可行,但是我想使用变量
$USERNAME
代替demouser
ssh centos@$remote_host -t 'sudo sed -i "\$ademouser ALL=(ALL) NOPASSWD:ALL" /etc/sudoers'
I tried using this but it's not working. 我尝试使用此功能,但无法正常工作。
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 `('
Parameters will not expand in single quotes, one can close them, and expand in double quotes instead: 参数将不会以单引号引起来,可以将其关闭,而改为以双引号引起来:
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.
You should however know that the command send to the server is: 但是,您应该知道发送到服务器的命令是:
sed "s/abc/replacement/" file
Which might cause problems as we are now using double quotes on the server, one can send single quotes as well, but it quickly becomes as mess: 这可能会导致问题,因为我们现在在服务器上使用双引号,一个也可以发送单引号,但很快就会变得一团糟:
ssh user@host 'sed '\''s/'"$localVar"'/replacement/'\'' file'
^ ^
| Escaped remote single quote
Close local single quote
This will become: 这将变为:
sed 's/abc/replacement' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.