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
echo > /dev/stdout
would reopen the file stdout goes to from scratch. For a regular file, that would overwrite it. For a socket, that would fail. (it could also be affected by the way shells perform redirection (like if they move fd 1 to something else before opening /dev/stdout). – Stéphane Chazelas Feb 25 '16 at 13:31