learning shell script prompt to run with superuser privileges (4)

Shell script prompt to run with superuser privileges

Purpose

       Check whether have root privileges to run script

 

Eevironment

       Ubuntu 16.04 bash env

 

Procdeure

  Source code:

#!/bin/bash
if [[ $EUID != 0 ]]; then
echo "This script requires root privileges, trying to use sudo"
sudo "./tmp.sh" "$@"
fi

  Test method:

vmuser@vmuser-virtual-machine:~/shell$ ./tmp.sh 
This script requires root privileges, trying to use sudo
[sudo] password for vmuser:

 

原文地址:https://www.cnblogs.com/lianghong881018/p/10314220.html