bash - Running multiple commands through ssh -
i trying run multiple commands using ssh not getting syntax right. please give me help?
ssh "$user"@"$ip" " \ current_hostname=$(hostname); \ sed -c -i 's/\($target_key *= *\).*/\1$replacement_value/' $distro_file; \ sed -c -i 's/$current_hostname/$replacement_value/' $config_file; \ hostname $replacement_value"
those commands work fine if run locally, getting following error when trying run them through ssh:
sed: -e expression #1, char 0: no previous regular expression
it seems error comes from
sed -c -i 's/$current_hostname/$replacement_value/' $config_file; \
as trying read $current_hostname set before in ssh itself. not sure how read variable back. clue please?
edited code:
ssh "$user"@"$ip" "current_hostname=\$(hostname); sed -c -i \"s/\($target_key *= *\).*/\1$replacement_value/\" $distro_file; sed -c -i \"s/\$current_hostname/$replacement_value/\" $config_file; hostname $replacement_value"
this works! lot!
thanks.
all variables double quotes evaluated bash before sending parameter remote machine.
if want preserve $variable
need use single quotes instead (escaping single quotes within commands), or escape dollar signs want evaluated on server side.
you don't need escape line break , use semicolon. line breaks included in parameter.
Comments
Post a Comment