I’m looking for a way to create a switch for this bash script so that I have the option of either printing (echo) it to stdout or executing the command for debugging purposes. As you can see below, I am just doing this manually by commenting out one statement over the other to achieve this.
Code:
#!/usr/local/bin/bash
if [ $# != 2 ]; then
echo "Usage: testcurl.sh <localfile> <projectname>" >&2
echo "sample:testcurl.sh /share1/data/20110818.dat projectZ" >&2
exit 1
fi
echo /usr/bin/curl -c $PROXY --certkey $CERT --header "Test:'${AUTH}'" -T $localfile $fsProxyURL
#/usr/bin/curl -c $PROXY --certkey $CERT --header "Test:'${AUTH}'" -T $localfile $fsProxyURL
I’m simply looking for an elegant/better way to create like a switch from the command line. Print or execute.
One possible trick, though it will only work for simple commands (e.g., no pipes or redirection (a)) is to use a prefix variable like:
What this will do is to insert you specific variable (
PAXPREFIXin this case) before the commands. If the variable is empty, it will not affect the command, as follows:However, if it’s set to
echo, it will prefix each line with thatechostring.(a) The reason why it will only work for simple commands can be seen if you have something like:
When
PAXPREFIXis empty, it will simply give you the list of your filenames in uppercase. When it’s set toecho, it will result in:giving:
(not quite what you’d expect).
In fact, you can see a problem with even the simple case above, where
%05d\nis no longer surrounded by quotes.If you want a more robust solution, I’d opt for:
and use
PAXDEBUG=1 myscript.shto run it in debug mode. This is similar to what you have now but with the advantage that you don’t need to edit the file to switch between normal and debug modes.For debugging output from the shell itself, you can run it with
bash -xor putset -xin your script to turn it on at a specific point (and, of course, turn it off withset +x).