I've got a script that calls out to a bunch of commands, some of which are noisy to stdout, some to stderr, some to both. I intend the script to be run by cron, so I don't want it to be noisy and mail me every day -- only in error conditions. So I do:
be_quiet() {
# save stderr in FD 3
exec 3>&2
exec &> /dev/null
}
die() {
# restore stderr
exec 2>&3
echo $* > /dev/stderr
exit 1
}
Then, i.e.
be_quiet
mkdir -p $CLIENT_ROOT || die "Could not create client root."
cd $CLIENT_ROOT || die "Could not cd to client root."
The intent being that I get specific and meaningful-to-me messages if there is an error, and nothing otherwise. But what I'm seeing now is just
line 48: /dev/stderr: Permission denied
When run from the command line, this works. When run via cron, it gives the permission denied message. I'm unclear why.