Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Using sudo in a script that is run as root?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
pcalvert
Posts: 1939
Joined: 2006-04-21 11:19
Location: Sol Sector
Has thanked: 1 time
Been thanked: 2 times

Using sudo in a script that is run as root?

#1 Post by pcalvert »

I found a bash script that formats and prepares a USB stick for booting from multiple ISO files.

Here is the script:

Code: Select all

#!/bin/bash
# Bash Script for formatting USB drives by GNUger

clear

echo "WARNING: THIS SCRIPT CAN FORMAT THE WRONG DRIVE If USED INCORRECTLY!"
echo "USE AT YOUR OWN RISK"
echo

if [[ $EUID -ne "0" ]]
then
 echo "You must be root - try:   sudo $0"
exit 1
fi

script="$0"
basename="$(dirname $script)"

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "$script resides in directory $DIR"

echo "Looking for _ISO/e2b folder - please wait..."

COUNTER=0

if [ -e "$DIR/../../../_ISO/e2b" ]
then
  let COUNTER=20
SRC="$DIR/../../../"
echo Found SOURCE at $SRC
echo ""
ls $SRC
fi

 while [  $COUNTER -lt 10 ]; do
 echo "Looking for e2b.ico file under / - please wait..."
 find / -name 'e2b.ico' 2>/dev/null 
 echo ""
 echo "Example: /home/mint/Documents/E2B"
 echo ""
 read -p  "Enter source folder for E2B files : " SRC

 echo ""
 echo "Source folder is $SRC"
 ls $SRC
 echo ""

if [ -e "$SRC/_ISO" ]
then
  let COUNTER=20
fi

if [ $COUNTER -lt 10 ]
then
echo ""
echo "ERROR: No _ISO folder!"
read -p "Is this folder correct (y/n) : " opinion2

if [[ $opinion2 == 'y' ]] || [[ $opinion2 == 'yes' ]]
 then
   let COUNTER=COUNTER+20
 else
   clear
 fi

fi
done




echo ""
df | grep '/dev/sd'
echo ""

device=$(df | grep '/dev/sd' | tail -n1 | awk '{ print $1; }')
read -p "Is device ($device) correct (y/n) : " opinion1
if [[ $opinion1 == 'n' ]] || [[ $opinion1 == 'no' ]]
then
 read -p "Enter partition name (e.g. /dev/sdc1 ) : " device
 fi
echo

DD="${device:0:8}"
if [ "$DD" == "/dev/sda" ];
then
echo "WARNING: $device may be your internal hard disk!"
fi

DD="${device:0:7}"
if [ "$DD" == "/dev/sd" ];
then
echo ""
else
echo "Bad device - must start with /dev/sd"
exit 1
fi

if [ ${device:8:1} -lt 3 ]
then
echo Partition = ${device:8:1}
else
echo "Bad Partition! should end in the number 1 or 2 - e.g. /dev/sdc1"
exit 1
fi

if [ ${device:8:1} == 0 ]
then
echo "Bad Partition! should end in the number 1 or 2 - e.g. /dev/sdc1"
exit1
fi

echo "Formatting: $device"
echo
read -p "Proceed with formatting (y/n): " opinion2
if [[ $opinion2 == 'y' ]] || [[ $opinion2 == 'yes' ]]

then

# This wipes the drive but then mkfs.vfat creates a superfloppy and bootlace fails - use fdisk manually to wipe a USB drive!
#read -p "Do you wan to erase all partitions on ${device:0:-1} first (y/n) : " opinion2
#if [[ $opinion2 == 'y' ]] || [[ $opinion2 == 'yes' ]]
#then
#  dd if=/dev/zero of=${device:0:-1}  bs=512  count=1
#fi

 sudo umount $device  2>/dev/null
 sudo umount /mnt/myusb  2>/dev/null
 label="EASY2BOOT"
 /sbin/mkfs.vfat -F32 -n "$label" $device

 echo Mounting $device as /mnt/myusb
 sudo mkdir /mnt 2>/dev/null
 sudo mkdir /mnt/myusb 2>/dev/null
 sudo mount $device /mnt/myusb

 echo Copying $SRC to /mnt/myusb...
 cp -r -i  $SRC /mnt/myusb

 echo Installing grub4dos MBR
 sudo chmod +rwx $DIR/bootlace.com
 sudo $DIR/bootlace.com --time-out=0 ${device%?}
 echo ""
 echo "Easy2Boot USB Drive contents..."
 ls /mnt/myusb/
echo ""
# commit writes when it unmounts before user pulls it out!
sudo umount /mnt/myusb

echo ""
echo "If this fails, you may need to use fdisk to delete the drive contents"
echo "See www.rmprepusb.com - Tutorial 114 for details"
sudo fdisk -l ${device%?}


exit 1

fi
I noticed the use of sudo multiple times within the script. Although I am not good enough to consider myself a programmer, that seems a bit strange to me since the instructions say that the script should be run as root. If a script is normally run by a non-root user, I could understand why using sudo within the script might be necessary, but using sudo in a script that is supposed to be run as root (like the above script) doesn't make sense to me. Am I missing something?

Phil
Freespoke is a new search engine that respects user privacy and does not engage in censorship.


User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Using sudo in a script that is run as root?

#3 Post by kiyop »

pcalvert wrote:I found a bash script that formats and prepares a USB stick for booting from multiple ISO files.

Here is the script:

Code: Select all

(snip by kiyop)
if [[ $EUID -ne "0" ]]
then
 echo "You must be root - try:   sudo $0"
exit 1
fi
(snip by kiyop)
 sudo umount $device  2>/dev/null
 sudo umount /mnt/myusb  2>/dev/null
(snip by kiyop)
 sudo mkdir /mnt 2>/dev/null
 sudo mkdir /mnt/myusb 2>/dev/null
 sudo mount $device /mnt/myusb
(snip by kiyop)
 sudo chmod +rwx $DIR/bootlace.com
 sudo $DIR/bootlace.com --time-out=0 ${device%?}
(snip by kiyop)
sudo umount /mnt/myusb
(snip by kiyop)
sudo fdisk -l ${device%?}
(snip by kiyop)
I noticed the use of sudo multiple times within the script. Although I am not good enough to consider myself a programmer, that seems a bit strange to me since the instructions say that the script should be run as root. If a script is normally run by a non-root user, I could understand why using sudo within the script might be necessary, but using sudo in a script that is supposed to be run as root (like the above script) doesn't make sense to me.
I agree with you. But I am not familiar with sudo. I do not use sudo in debian.
I am interested in whether sudo in a script executed with sudo is useful.
How about removing the "sudo" in all lines starting with sudo in the script, and then executing the revised script with sudo?
I wonder if the revised script works correctly even without sudo in it.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Using sudo in a script that is run as root?

#4 Post by GarryRicketson »

I don't usually use sudo either, but I believe the thing of it is,
is when sudo is used , after the 1 command is executed, it
reverts back to non root user , so then 'sudo" has to be used
again, on the next command, and so on.
The first part , saying "you must be root, try sudo", is what you
would get if you don't run the script as root, or sudo.
I don't see why any one would want to use the script though.
It is easy enough to format and burn a ISO to a usb stick,
to make a bootable usb stick,...I don't see the point or reason
for using a script.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Using sudo in a script that is run as root?

#5 Post by kiyop »

I have a ubuntu 14.04 DVD and so, I booted a PC with it.

Code: Select all

fdisk -l
did not display anything, because (I guess) it requires root privilege.

Code: Select all

sudo fdisk -l
did display the results.

Code: Select all

cat <<EOF > test1
#!/bin/bash
fdisk -l
fdisk -l
EOF
sudo bash ./test1
did display the results twice. Thus, "sudo" in a script is not required (is non-sense) if the script itself is executed with "sudo", as I (and pcalvert) guessed, at least on Ubuntu 14.04 live. ;) I guess it is similar on debian.

Of course,

Code: Select all

chmod +x ./test1
sudo ./test1
did display the results twice too. ;)
GarryRicketson wrote:when sudo is used , after the 1 command is executed, it
reverts back to non root user , so then 'sudo" has to be used
again, on the next command, and so on.
Of course, I know it. ;)
The question is not such a thing. ;)

ADDED at Sat Jul 16 14:36:38 JST 2016,

I confirmed that "sudo" in a script is not required (is non-sense) if the script itself is executed with "sudo", on one of my debian sid, where my non-root user is in sudoers group.
Last edited by kiyop on 2016-07-16 05:38, edited 1 time in total.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

reinob
Posts: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 47 times

Re: Using sudo in a script that is run as root?

#6 Post by reinob »

pcalvert wrote:I noticed the use of sudo multiple times within the script. Although I am not good enough to consider myself a programmer, that seems a bit strange to me since the instructions say that the script should be run as root. If a script is normally run by a non-root user, I could understand why using sudo within the script might be necessary, but using sudo in a script that is supposed to be run as root (like the above script) doesn't make sense to me. Am I missing something?

Phil
The script is incorrect, or at least pretty hacky (probably grown over time, etc.)

As long as root is allowed to use sudo (not by default AFAIK) it will work OK.
I use sudo as root to e.g. run something as the www-data user.

If you include this in your /etc/sudoers file (*please* use visudo for this)

Code: Select all

root	ALL=(ALL:ALL) ALL
then root will be able to use sudo without password (otherwise the script would ask you to input root's password, which may be inconvenient, especially knowing that the script is already running as root :)

But normally you'd want the script to either:
a) allow execution only if root
b) use sudo whenever required

but not both.

Post Reply