Skip to content

Instantly share code, notes, and snippets.

@yuan3y
Created February 28, 2021 01:09
Show Gist options
  • Save yuan3y/018bf9f6420a6c35430be5adf8c7ac64 to your computer and use it in GitHub Desktop.
Save yuan3y/018bf9f6420a6c35430be5adf8c7ac64 to your computer and use it in GitHub Desktop.
How to ensure a script is run by the same user as the owner of a file
#!/usr/bin/env bash
script_runner=$(id -u -n)
file_owner=$(stat -c '%U' ./index.html)
[ $script_runner != $file_owner ] && { echo "please run script as $file_owner with 'sudo -u $file_owner $0'"; exit 1; }
echo "all good, do what you want $file_owner to do here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment