I am trying to run a command-line remotely that contains some single, double quotes.
1) This is the command I want to have it running on remote host.
echo '{"id":12345,"name":"activate_cluster"}'
which should be of the exactly same format. Not any missing characters.
2) This is the full command I have used to trigger this command from my local host:
expect bashscript $hostname $user $pwd 'echo \'\{\"id\":12345\,\"name\":\"activate_cluster\"\}\'
3) But when it reaches the remote host, this command becomes,
echo {"id":12345,"name":"activate_cluster"}
The pair of single quotes is gone! Is there a way I can fix this?
You cannot embed a single quote within single quotes in bash: https://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes
You'll have to do something like this:
expect bashscript $hostname $user $pwd 'echo '\''{"id":12345,"name":"activate_cluster"}'\'
# ...........................................^^^^......................................^^^
'\\''
-- the first quote ends the opening quote from 'echo
, the escaped quote appends a literal quote, and the third one opens a new quoted string. Within single quotes, you don't need to go nuts with backslashes -- they're all literal characters in there.
Another approach would be to store the command in a separate variable:
cmd=$(cat <<'END'
echo '{"id":12345,"name":"activate_cluster"}'
END
)
expect bashscript $hostname $user $pwd "$cmd"
A little wordier but much tidier, no?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.