create shell script only run by root user
#vim script.sh
#/bin/bash
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script."
else
echo "Script is running from root user "
exit 1
fi
#chmod o+x script.sh
Test script from root user
# ./check.sh
Script is run by root
Test script from non root user
$
./check.shError: You must be root to run this script.