0

I have a 'simple' script which runs a few commands, I normally just want to see a few echo's letting me know where it is up to. But occasionally I need a verbose output which shows me the output of some of the commands being executed.

Example for normal use

echo "Simple message"
wget 'http://example.com' > /dev/null

Verbose option

echo "Simple message"
wget 'http://example.com'

I thought of using a variable to store the output redirection

output=""
if [ -z "$1" ]
  then
    output="> /dev/null"
fi
echo "Simple message"
wget 'http://example.com' $output

Problem is it didn't seem to be working, so I added -x to the shebang and the output shows me

wget 'http://example.com' '>' /dev/null

How can I stop the > from being single quoted?

There's more going on in the original script than a simple wget, this is just a short example.

1 Answer 1

0

Redirecting wget to stdout allows you to change your script to look like this:

#!/bin/bash
output="/dev/stdout"
if [ -z "$1" ]; then
output="/dev/null"
fi
echo "simple message"
wget -qO- 'http://example.com' > "$output"

This way, whenever your first parameter is empty it will always redirect to /dev/null, otherwise output to stdout.

If you didn't want it to output to stdout, I'd suggest creating a file (which will always be the same) and then reading from it after the wget.

3
  • when $output is empty, then this command would be wget 'example.com' > "" How does redirection work here, does it just assume stdout? Oct 17, 2017 at 11:23
  • Sorry, I didn't explain all of that. I've updated the answer
    – Robbie
    Oct 17, 2017 at 11:57
  • Thank you @robbie, that looks great, will test it out later. Oct 18, 2017 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .