Issue
I have a bash program that will write to an output file. This file may or may not exist, but the script must check permissions and fail early. I can't find an elegant way to make this happen. Here's what I have tried.
set +e touch $file set -e if [ $? -ne 0 ]; then exit;fi
I keep set -e
on for this script so it fails if there is ever an error on any line. Is there an easier way to do the above script?
Solution
Rather than check $?
on a different line, check the return value immediately like this:
touch file || exit
As long as your umask
doesn't restrict the write bit from being set, you can just rely on the return value of touch
Answered By - SiegeX Answer Checked By - Terry (WPSolving Volunteer)