开发者

how to create the option for printing out statements vs executing them in a shell script

开发者 https://www.devze.com 2023-04-01 02:34 出处:网络
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

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:开发者_StackOverflowtestcurl.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:

pax> cat qq.sh
${PAXPREFIX} ls /tmp
${PAXPREFIX} printf "%05d\n" 72
${PAXPREFIX} echo 3

What this will do is to insert you specific variable (PAXPREFIX in this case) before the commands. If the variable is empty, it will not affect the command, as follows:

pax> ./qq.sh
my_porn.gz  copy_of_the_internet.gz
00072
3

However, if it's set to echo, it will prefix each line with that echo string.

pax> PAXPREFIX=echo ./qq.sh
ls /tmp
printf %05d\n 72
echo 3

(a) The reason why it will only work for simple commands can be seen if you have something like:

${PAXPREFIX} ls -1 | tr '[a-z]' '[A-Z]'

When PAXPREFIX is empty, it will simply give you the list of your filenames in uppercase. When it's set to echo, it will result in:

echo ls -1 | tr '[a-z]' '[A-Z]'

giving:

LS -1

(not quite what you'd expect).

In fact, you can see a problem with even the simple case above, where %05d\n is no longer surrounded by quotes.

If you want a more robust solution, I'd opt for:

if [[ ${PAXDEBUG:-0} -eq 1 ]] ; then
    echo /usr/bin/curl -c $PROXY --certkey $CERT --header ...
else
         /usr/bin/curl -c $PROXY --certkey $CERT --header ...
fi

and use PAXDEBUG=1 myscript.sh to 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 -x or put set -x in your script to turn it on at a specific point (and, of course, turn it off with set +x).


#!/usr/local/bin/bash

if [[ "$1" == "--dryrun" ]]; then
    echoquoted() {
        printf "%q " "$@"
        echo
    }
    maybeecho=echoquoted
    shift
else
    maybeecho=""
fi

if [ $# != 2 ]; then
    echo "Usage: testcurl.sh <localfile> <projectname>" >&2
    echo "sample:testcurl.sh /share1/data/20110818.dat projectZ" >&2
    exit 1
fi

$maybeecho /usr/bin/curl "$1" -o "$2"


Try something like this:

show=echo
$show /usr/bin/curl ...

Then set/unset $show accordingly.


This does not directly answer your specific question, but I guess you're trying to see what command gets executed for debugging. If you replace #!/usr/local/bin/bash with #!/usr/local/bin/bash -x bash will run and echo the commands in your script.


I do not know of a way for "print vs execute" but I know of a way for "print and execute", and it is using "bash -x". See this link for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号