Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created February 21, 2021 01:51
Show Gist options
  • Select an option

  • Save mttjohnson/b8b5a4c387b459e6f2724be34662dcf2 to your computer and use it in GitHub Desktop.

Select an option

Save mttjohnson/b8b5a4c387b459e6f2724be34662dcf2 to your computer and use it in GitHub Desktop.
JSON Encode Complex Commands
# Capture complex commands using HEREDOC
set +H
COMMAND=$(cat <<'COMMAND_HEREDOC'
# disable history expansion
set +H
PHP_CODE=$(cat <<'PHP_CODE'
<?php
print 'hello ';
print "world!";
print "\n";
PHP_CODE
)
# re-enable history expansion
set -H
echo "${PHP_CODE}" | php
COMMAND_HEREDOC
)
set -H
# Confirm commands
printf "%s\n" ${COMMAND}
# JSON escapped string
JSON_ESCAPED_STRING=$(printf "%s\n" ${COMMAND} | jq -aRs .)
printf "%s\n" ${JSON_ESCAPED_STRING}
# Raw output of JSON string
RAW_OUTPUT_JSON_STRING=$(printf "%s\n" ${JSON_ESCAPED_STRING} | jq . -r)
printf "%s\n" ${RAW_OUTPUT_JSON_STRING}
# Send commands to remote system
printf "%s\n" ${RAW_OUTPUT_JSON_STRING} | ssh example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment