Created
February 21, 2021 01:51
-
-
Save mttjohnson/b8b5a4c387b459e6f2724be34662dcf2 to your computer and use it in GitHub Desktop.
JSON Encode Complex Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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