Bash Handling Complex Commands as Strings: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * bash =Overview=")
 
Line 2: Line 2:
* [[Bash#bash_Script_Library|bash]]
* [[Bash#bash_Script_Library|bash]]
=Overview=
=Overview=
<syntaxhighlight lang='bash'>
some_header_value='blue'
some_security_sensitive_header_value='red'
mode=POST
post_body='{"color": "green", "size": 10}'
url=https://example.com
command='curl -s -H "X-SOME-HEADER: '${some_header_value}'" -H "X-SOME-SECURITY-SENSITIVE-HEADER: '${some_security_sensitive_header_value}'" -H "Content-Type: application/json" -X '${mode}
if [[ ${mode} == 'POST' ]]; then
  command="${command} -d '${post_body}'"
fi
command="${command} ${url}"
command_with_masked_token=$(echo "${command}" | sed -e 's/X-SOME-SECURITY-SENSITIVE-HEADER: .*-H/X-SOME-SECURITY-SENSITIVE-HEADER: ***" -H/')
echo "[debug]: ${command_with_masked_token}"
output=$(sh -c "${command}") || { echo "command failed" 1>&2 2>/dev/stderr; }
echo "${output}"
</syntaxhighlight>

Revision as of 19:09, 14 August 2023

Internal

Overview

some_header_value='blue'
some_security_sensitive_header_value='red'
mode=POST
post_body='{"color": "green", "size": 10}'
url=https://example.com

command='curl -s -H "X-SOME-HEADER: '${some_header_value}'" -H "X-SOME-SECURITY-SENSITIVE-HEADER: '${some_security_sensitive_header_value}'" -H "Content-Type: application/json" -X '${mode}
if [[ ${mode} == 'POST' ]]; then
  command="${command} -d '${post_body}'"
fi
command="${command} ${url}"

command_with_masked_token=$(echo "${command}" | sed -e 's/X-SOME-SECURITY-SENSITIVE-HEADER: .*-H/X-SOME-SECURITY-SENSITIVE-HEADER: ***" -H/')
echo "[debug]: ${command_with_masked_token}"
output=$(sh -c "${command}") || { echo "command failed" 1>&2 2>/dev/stderr; }
echo "${output}"