Operating System - HP-UX
1748124 Members
3224 Online
108758 Solutions
New Discussion юеВ

control (enable/disable) redirect to /dev/null

 
SOLVED
Go to solution
Billa-User
Regular Advisor

control (enable/disable) redirect to /dev/null

hello,

how can i control (enable/disable) redirect to /dev/null in script ? for example, when a variable DEBUG hasn't value "Y", output redirect to /dev/null. if value "Y", i should see the output. i need this for disable/enable oracle "sqlplus" output in a ksh script.

i read forum entry "about redirect"
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=858713
and find two solutions :

1. script : based on forum entry:

DEBUG=""

if [ "${DEBUG}" = "Y" ]
then
OUTPUTDEVNULL="" # i should see the output
else
OUTPUTDEVNULL="/dev/null"
fi

echo "test" > ${OUTPUTDEVNULL} 2>&

2. script : example with exec
DEBUG=""

if [ "${DEBUG}" = "Y" ]
then
exec 3>&2 # i should see the output
else
exec 3>/dev/null
fi

echo "test" >&3

exec 3>&- # close the file descriptor

what i the best solution ? other way to solve this ?
regards
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: control (enable/disable) redirect to /dev/null

Hi:

I use something like this:

# cat ./debug.sh
#!/usr/bin/sh
DEBUG=0 && [ "${1}" = "-d" ] && DEBUG=1
echo "...this is output..."
[ ${DEBUG} = 0 ] && exec > /dev/null 2>&1
echo "...this you don't see if debugging off..."

...run:

# ./debug.sh

...and compare to:

# ./debug.sh -d

Regards!

...JRF...
Billa-User
Regular Advisor

Re: control (enable/disable) redirect to /dev/null

thx for the fast answer.
can i redirect to stdout maybe for a message which should always appear ?

[ ${DEBUG} = 0 ] && exec > /dev/null 2>&1
echo "...this you don't see if debugging off..."

echo "other output ..."

the command for redirect to stdout ?
exec >& -
echo "output allways appears"

regards
James R. Ferguson
Acclaimed Contributor

Re: control (enable/disable) redirect to /dev/null

Hi (again):

Why not use STDERR for what is is intended (?):

# cat ./debug.sh
#!/usr/bin/sh
DEBUG=0 && [ "${1}" = "-d" ] && DEBUG=1
echo "...this is output..."
[ ${DEBUG} = 0 ] && exec > /dev/null
echo "...this you don't see if debugging off..."
print -u2 "...STDERR reporting here..."

Regards!

...JRF...