Created
February 28, 2021 01:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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