2

I have script that prints out few lines of text. This text is used as configuration by other process.

When I normally run program I redirect standard output to file which is fine:

./generateConfig.sh > normal.cfg

But the problem is that normal.cfg is really constant and I would like to give user a choice to print to file or on the screen by providing additional parameter (like -o, without parameter arguments).

if the script content is:

#!/bin/bash
echo "8.8.8.8"

How would I parametrize output so that depending on args it will print out to file or stdout?

This does not seem to work:

#!/bin/bash
output="/dev/stdout"
if [ $# -gt 0 ]; then
  output="normal.cfg"
fi
echo "8.8.8.8" > $output
Kamil
  • 145

3 Answers3

3

You can redirect the output of the whole script using exec.

#! /bin/bash

if [[ $1 = -o ]]
then
    exec > "$2"
fi

echo "8.8.8.8"

Now, if you run the script with -o foo, the output will be in the file foo.

$ ./foo.sh 
8.8.8.8
$ ./foo.sh -o bar
$ cat bar
8.8.8.8
muru
  • 72,889
3

Several approaches:

A function

 #! /bin/sh -
 [ "$#" -gt 0 ] && redir=true || redir=false
 redir() if "$redir"; then "$@" > normal.cfg; else "$@"; fi

 redir echo 8.8.8.8

An alias:

 #! /bin/sh -
 [ -z "$BASH_VERSION" ] || shopt -s expand_aliases

 [ "$#" -gt 0 ] && alias redir='>normal.cfg' || alias redir=

 redir echo 8.8.8.8

(won't work with posh which disabled aliases, or if sourced (with the . command) in AT&T implementations of ksh).

A dedicated file descriptor:

 #! /bin/sh -
 [ "$#" -gt 0 ] && exec 3> normal.cfg || exec 3>&1

 echo 8.8.8.8 >&3

(beware that apart from ksh93, not many shells set the close-on-exec flag on that fd 3, so you may want to close it for commands you execute by hand)

  • I'm curious: why would you use sh as for the shebang in the alias examlpe? – muru Feb 25 '16 at 14:19
  • @muru, because that would work with most sh and all systems have a sh, while not so many have bash. For those unlucky enough to have bash as their sh, I add the workaround for bash that doesn't expand aliases in non-interactive shells. – Stéphane Chazelas Feb 25 '16 at 14:22
  • So aliases are on by default in noninteractive ksh, zsh, dash, etc.? – muru Feb 25 '16 at 14:31
  • @muru, yes. There are some contexts where some aliases are not expanded in some shells (like in sh -c '...' or sourced files), but the above should work on most systems. Some shells even allowed exporting aliases. POSIX leaves it unspecified whether aliases are expanded or not in non-interactive shells. – Stéphane Chazelas Feb 25 '16 at 14:46
-1

Try with:

#!/bin/bash
output="normal.cfg"
if [ $# -gt 0 ]; 
 then
  echo "8.8.8.8" > $output
 else
  echo "8.8.8.8"
fi

If you want to do something more complex, you may try tee command: see example here.

fireto
  • 81
  • Appreciate but I am looking for something more advanced. The script above is just example, in fact it's more complex. – Kamil Feb 25 '16 at 13:08