Update gravity.sh sudo errors as root

When gravity.sh is run as the root user, it errors out on the sudo command.
These changes make the script check to see if it is invoked by the root and sets the sudo variable to handle root and non-root runs.
This commit is contained in:
Mel 2015-10-27 20:35:37 -05:00
parent d9aa19d2cd
commit fd21237b27

View file

@ -5,6 +5,11 @@
# This script should only be run after you have a static IP address set on the Pi # This script should only be run after you have a static IP address set on the Pi
piholeIP=$(hostname -I) piholeIP=$(hostname -I)
#Checks if the script is being run as root and sets sudo accordingly
SUDO=''
if (( $EUID !=0 )); then SUDO='sudo'
fi
# Ad-list sources--one per line in single quotes # Ad-list sources--one per line in single quotes
sources=('https://adaway.org/hosts.txt' sources=('https://adaway.org/hosts.txt'
'http://adblock.gjtech.net/?format=unix-hosts' 'http://adblock.gjtech.net/?format=unix-hosts'
@ -45,7 +50,7 @@ if [[ -d $piholeDir ]];then
: :
else else
echo "** Creating pihole directory..." echo "** Creating pihole directory..."
sudo mkdir $piholeDir $SUDO mkdir $piholeDir
fi fi
# Add additional swap to prevent the "Error fork: unable to allocate memory" message: https://github.com/jacobsalmela/pi-hole/issues/37 # Add additional swap to prevent the "Error fork: unable to allocate memory" message: https://github.com/jacobsalmela/pi-hole/issues/37
@ -53,10 +58,10 @@ function createSwapFile()
######################### #########################
{ {
echo "** Creating more swap space to accomodate large solar masses..." echo "** Creating more swap space to accomodate large solar masses..."
sudo dphys-swapfile swapoff $SUDO dphys-swapfile swapoff
sudo curl -s -o /etc/dphys-swapfile https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dphys-swapfile $SUDO curl -s -o /etc/dphys-swapfile https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dphys-swapfile
sudo dphys-swapfile setup $SUDO dphys-swapfile setup
sudo dphys-swapfile swapon $SUDO dphys-swapfile swapon
} }
@ -138,7 +143,7 @@ function gravity_advanced()
echo "** Formatting domains into a HOSTS file..." echo "** Formatting domains into a HOSTS file..."
cat $origin/$eventHorizon | awk '{sub(/\r$/,""); print "'"$piholeIP"'" $0}' > $origin/$accretionDisc cat $origin/$eventHorizon | awk '{sub(/\r$/,""); print "'"$piholeIP"'" $0}' > $origin/$accretionDisc
# Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it # Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it
sudo cp $origin/$accretionDisc $adList $SUDO cp $origin/$accretionDisc $adList
kill -HUP $(pidof dnsmasq) kill -HUP $(pidof dnsmasq)
} }