Hameed
Hameed

Reputation: 2277

Redirecting STDOUT and STDERR in Shell script

I have a script that uses the exec command to redirect STDOUT and STDERR to a file. The script works fine under Solaris 9. I am testing it on Solaris 10 on VMware and it fails at the exec command.

prog=`basename $0`
log="${LOCLOG}/${prog}$$"

if test -z "$LDDEBUG"
then
    exec > ${log} 2>&1

    chmod 644 "${log}"
else
    set -x
fi

In the above, the script ends right at that line without any error message.

I added quotes around variable exec > "${log}" 2>&1. This let the script run to the end, but it did not create the log file.

I then tried it with a file path:

exec > /tmp/blahblah1 2>&1

... and it worked. It created the file and wrote to it. But this doesn't solve my problem because the log filename has to be generated in the script.

I am lost for a solution here and I know it probably is very simple, but I can't think of anything else to do without having to make any major change to the script.

====== Output of sh -x

ld-test@lunar-tst[60] sh -x /home/hameed/test/local/bin/qikload
+ basename /home/hameed/test/local/bin/qikload
prog=qikload
datefmt=+%H:%M:%S
dayfmt=+%y%m%d
log=/home/hameed/test/local/etc/log/0508/qikload2199
+ test -z
+ exec

=======

Then I removed the 2>&1 and ran sh -x and it went passed that line.

+ basename /home/hameed/test/local/bin/qikload
prog=qikload
datefmt=+%H:%M:%S
dayfmt=+%y%m%d
log=/home/hameed/test/local/etc/log/0508/qikload27213
+ test -z
+ exec
+ chmod 644 /home/hameed/test/local/etc/log/0508/qikload27213
+ date +%H:%M:%S
now=10:44:23
+ echo Log: 10:44:23 Commencing loads

Upvotes: 1

Views: 2313

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754550

One very plausible cause of the failure is that $LOCLOG is unset on Solaris 10 under VMWare (but is set on Solaris 9), so the script is trying (but failing) to write in the root directory.

You can validate this by checking that $LOCLOG is set (for example, echo LOCLOG="$LOCLOG" without any redirection).

Upvotes: 1

Related Questions