title: Prompt for `sudo` only if Bash script runs into "Permission denied"
body: <p>Let's say I have a very simple script which creates a link in a certain directory, and kills the script if it fails.</p>
  
  <pre><code>ln -s "/opt/myapp" "${1}/link" || exit 1;
  </code></pre>
  
  <p>Right now it just quits if it runs into errors. I want to change it so <strong>only if it runs into permission errors</strong> when creating the link, it will execute the following lines instead of exiting:</p>
  
  <pre><code>echo "The target directory requires root privileges to access."
  sudo ln -s "/opt/myapp" "${1}/myapp" || exit 1;
  </code></pre>
  
  <p>I don't want to prompt the users to run as root unless they absolutely have to.</p>
  
  <hr>
  
  <p><code>ln</code> 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.</p>
  
  <p>And if I instead store search through the output of <code>ln</code> for the string "Permission denied", I'm assuming it will fail on non-english operating systems.</p>