0

To keep it simple, at the top of my bash script is the following code

#!/bin/bash
#if no arguments passed
if [ -z "$1" ]; then
    DEBUG='1>/dev/null'
else
    DEBUG='1>&1'
fi

Then throughout the rest of the script are calls like this

echo Doing something here! $DEBUG

The point being if someone calls the script without arguments (./myScript.sh) it wont echo anything.
But if they do pass in a parameter (./myScript.sh whathaveyou) the echo statements should work normally.

Unfortunately, however echo Doing something here! $DEBUG is being evaluated as

echo Doing something here! "1>&1"

instead of

echo Doing something here! 1>&1

How can I make this work?

3 Answers 3

2

Instead of trying to make your command evaluate text data as command syntax, you can make a conditional function:

if [ -z "$1" ]; then
  report() {
    true
  }
else
  report() {
    echo "$@"
  }
fi
report "Doing something here!"

If you want to run generic commands instead of just printing things, you can similarly use

if [ -z "$1" ]; then
  report() {
    "$@" > /dev/null
  }
else
  report() {
    "$@"
  }
fi

report echo "Here are you files:"
report ls

To implement it exactly as you specified, you'd have to use eval which has a number of security issues and isn't as convenient.

Sign up to request clarification or add additional context in comments.

6 Comments

So it's not possible? (well, without using eval) Thanks for an equally simple alternative!
Not as described, no. But there are plenty of alternatives, such as this one or writing all your debug info to fd 42 and conditionally redirect that.
yeah I was just asking in case someone comes here because of the title, and this work around isn't useful for them. I, myself, will be using your first example, definitely
You could always edit your question to say something like "Adding a verbose/debug mode to a script" :P
that other guy - big like to your solution. Hashbrown - why not using eval? it's kind of panacea to many similar shell problems
|
0

Why not just do:

#!/bin/bash

test -z "$1" && exec > dev/null

This will redirect the output of the script to the bit bucket if the first argument is empty or not passed. All commands (unless redirected) will then discard their output.

4 Comments

Perhaps because this would also silence all other commands in the script
If the commands to be used as debug are selective: if test -z "$1; then exec 3> /dev/null; else exec 3>&1; fi; ... debug-cmd >&3
@Hashbrown, huh? This does not re-evaluate anything. This only requires you to simplify the top portion of your code, but you've just selected a solution that requires you to modify your entire script and adds quite a bit of syntactic noise. Perhaps you commented on the wrong solution?
sorry, yes, I mis-interpreted. In response to your answer; the reason I had done it this way was to make sure other echo lines would write normally. Only those denoted with $DEBUG would be moot under argumentless invokation
0

I tend to do something like this:

#!/bin/bash

# if $1 is not empty, set $enable_trace to 'true'
enable_trace=${1:+true}

function trace()
{
  if [ "$enable_trace" = 'true' ]; then
    echo "$@"
  fi
}

trace Doing something here

Introducing the $enable_trace parameter also allows you to turn the output on and off easily at different points within your script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.