Prompt for `sudo` only if Bash script runs into "Permission denied"
Let's say I have a very simple script which creates a link in a certain
directory, and kills the script if it fails.
ln -s "/opt/myapp" "${1}/link" || exit 1;
Right now it just quits if it runs into errors. I want to change it so
only if it runs into permission errors when creating the link, it will
execute the following lines instead of exiting:
echo "The target directory requires root privileges to access."
sudo ln -s "/opt/myapp" "${1}/myapp" || exit 1;
I don't want to prompt the users to run as root unless they absolutely
have to.
ln seems to retun exit code 1 on failure regardless of whether it was a
problem with permissions or any other errors such as a directory not
existing, so I can't use that to detect which problem it ran into.
And if I instead store search through the output of ln for the string
"Permission denied", I'm assuming it will fail on non-english operating
systems.
No comments:
Post a Comment