Last active
August 29, 2015 14:15
-
-
Save gkazior/3b413cebdeaca05e7935 to your computer and use it in GitHub Desktop.
Check if the caller of a script is also an owner of the script
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
#!/bin/bash | |
true=0 | |
false=1 | |
## | |
# check if the caller is the same as the owner of the current script | |
# otherwise the following problem is possible: | |
# root calls you script and creates a file with root ownership | |
# then you need root to do anythink with the file | |
checkCaller() | |
{ | |
correctUser=`ls -nl $0 | awk '{print $3}'` | |
echo $correctUser | |
echo $UID | |
[[ $correctUser == $UID ]] | |
} | |
# sample usage | |
checkCaller | |
if [[ $? -eq $false ]]; then | |
echo "Error. Only owner of the script may run it. Please use su command!" | |
exit $false | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment