Since you have sudo access, create a sudoers file for yourself, then delete it when the script is done:
# grant this user access to the sudo commands without passwords
# add all required cmds to the CMDS alias
sudo tee /etc/sudoers.d/$USER <<END
$USER $(hostname) = NOPASSWD: /usr/bin/apt-get, /usr/sbin/adduser, /bin/rm, ...
END
# your script goes here
sudo apt-get install ...
git clone ...
sudo adduser group2 $USER
sudo adduser group1 $USER
: ...
# then, remove the sudo access
sudo /bin/rm /etc/sudoers.d/$USER
sudo -k
I wrote myself a little script using the fdisk command, based on what I know plus bits of code found online, which does something very similar, namely, interactively format a drive into one or more partitions, asking if this is the correct drive, waiting for a set time, or input from the user, warning it is about to erase it, etc. One thing it cannot do, in its current form, is resume running after it's stopped for any reason, from getting the input "No, don't format!" from the user, or simply an error. Once it stops, it has to be run again.
Note: $(echo $Output_Device) is a shell variable, and can be replaced with e.g. sdb or any similar name for a disk device.
The actual fdisk command(s) I cobbled together are:
Formats the disk into two partitions, 1 of size 64Mb, and 2 the rest of the disk:
(echo o; echo n; echo p; echo 1; echo ""; echo +64M; echo n; echo p; echo 2; echo ""; echo ""; echo w; echo q) | fdisk /dev/$(echo $Output_Device)
Then, actually formats the partitions, 1 as FAT, 2 as ext3:
mkfs.vfat -n BOOT /dev/$(echo $Output_Device)1
mkfs.ext3 -L root /dev/$(echo $Output_Device)2
I can post the whole script, but it is messy, and I like to keep commands in it even when they're commented out, for my own reference.
The more general case, how to make it resume, I may or may not be able to do, haven't tried. Likewise, this works with fdisk, but other commands may/will need a different syntax.
Best Answer
It is rarely a good idea to have
sudo
inside scripts. Instead, remove thesudo
from the script and run the script itself withsudo
:That way, all commands within the script will be run with root privileges and you only need to give the password once when launching the script. If you need a particular command within the script to be run without
sudo
privileges, you can run it as a regular user with (thanks Lie Ryan):The space is irrelevant, it should not affect anything, there is always a space between a command and its arguments.