Skip to content

Instantly share code, notes, and snippets.

@gkazior
Last active August 29, 2015 14:15
Show Gist options
  • Save gkazior/3b413cebdeaca05e7935 to your computer and use it in GitHub Desktop.
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
#!/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