I'm trying to automatize some configurations I have with bash script, I've never done this before so I tried with something easy like a Hello World! and everything works just fine, but then I tried something like this:

sudo su
apt-get purge postfix

and it doesn't do anything, I check and postfix is still installed, and at any time it asks for any entry of mine, I just tried with apt-get but I'll do things like ssh-keygen or even write files I guess with cat or something, how could I do the script working and also seeing what's going on?


sudo su runs su for you, which in turn leaves you in a shell as root, waiting for your input.

What you want to do is sudo apt-get purge postfix. man sudo to see why.

You'll find plenty of bash script examples around for further reference.

  • I didn't find anything, it's my first time with bash and I didn't know how to ask, another quick question, if the bash finds something wrong, what does it do? – poz2k4444 Oct 18 '12 at 18:26
  • It tries to run everything that's in the file. If that something fails you will see the output (if any) but the script will continue running. You can use || as "OR" to run something if a command fails, or even end the script: cp /file/that/doesnt/exist /destination/ || exit 1. This tutorial will help you get started. – dset0x Oct 18 '12 at 18:41
  • if it try to do everything in the file, why doesn't ask for something or shows something afetr sudo su, when àpt-get purge postfix?? – poz2k4444 Oct 18 '12 at 19:45
  • sudo is a command in itself. So is su. When you type sudo su, the system executes su as root which pushes you into root's shell. The sudo command you used to execute su is still running, waiting for the su command to finish. When su finishes, apt-get will run. Type exit to get out of su. What you want os what @zmode suggested in his answer. sudo apt-get purge postfix which is a bad idea to do in a script. Why are you doing that so often that you have to script a purge command? – UtahJarhead Oct 18 '12 at 21:00
  • @poz2k4444 It probably doesn't ask for a password because sudo remembers your password for a while after you've used it once. If you type whoami after running the script it should tell you that you're actually root, until you exit, as @UtahJarhead said. – dset0x Oct 18 '12 at 21:08

This is best automated by the root/admin account and not left in the files of a standard user.

The best practice concerning what you want to accomplish is to become root sudo su - and then work in the /root user dir. Something like this is usually best kept in ~/bin or ~/scripts so if it doesn't exist you may need to create it and than verify that the new home is part of your PATH. It is always a good idea to keep a scripts directory of some sort to house the little scripts that you use to manage your system(s) over time you will end up with quite a few and every administrator has his/her "toolbox" that they keep for these things. The trick is to try to write code that is reusable and thereby make your life easier.

If you are going to utilize a number of these files I would create a special dir for them like ~/scripts/sysupdates and try to keep each file to doing only one thing. This mirrors the function of other *nix systems like init.d and will make maintaining the system easier and intuitive. This also has the purpose of limiting our entries in other places, as you will soon see.

if you create the files for what you want to update as mentioned above (eg. postfix, qmail, update) then you can loop through them with one script in ~/scripts which can be something like:

# Script: updater.sh
# Author: My Name  
# Date: yyyymmdd - ee   (ee = times edited)
# Purpose: run update scripts from ~/scripts/sysupdates
# Notes:

for SCRIPT in `ls ~/scripts/sysupdates`; do ${SCRIPT}; done

Do yourself a favor and get in the habit of making proper header for each file you create. It only takes a minute and will save headaches down the road, remember you may not remember why you created it later or what it did, or it may not be you who has to maintain it one day.

The last part of this to edit the root crontab. Cron is a program that runs a given script at a specified interval. So if we add our updater.sh script to the crontab all we have to do is add files to ~/scripts/sysupdates and they will get run automatically.

You access the crontab with crontab -e which will edit the crontab with the defined system editor (usually vim in linux).There is a crontab generator to help you at: http://www.thepcmanwebsite.com/crontab_generator.shtml for making your own. Add the line:

5 0 1 * * /root/scripts/updater.sh

You should be good to go. Have fun with these tools as they can make life of a SysAdmin much easier.

One word of caution; I would not add apt-get -y dist-upgrade to your scripts, run that one when you are prepared to deal with it, should it run awry.

  • So, it's a bad idea use apt-get inside a script??? I don't understand that last part of your answer – poz2k4444 Oct 19 '12 at 18:29
  • 1
    @poz2k4444 Let me clarify for you: Using apt-get in a script is typically fine. The caution I mention is for using it with "dist-upgrade" as this will attempt to upgrade the kernel and certain system binaries. While this is not usually dangerous, an error here can leave your system nonfunctional. So it is one of those updates that is better done when you are watching. I hope that helps you understand why I recommend doing it this way. – Patrick Oct 19 '12 at 19:17

apt-get purge only works as sudo or root, to run sudo from a script you need to enable sudo without passwords for the user you run the script as or else it will prompt for a password and wait.

Run visudo and add a line at the bottom reading:

yourusername ALL=NOPASSWD: ALL

Also apt-get gives a prompt, to remove the package without a prompt use the option -y

sudo apt-get purge package -y

The end result would be something like:

sudo apt-get purge postfix -y
  • this will not generate security issues? – poz2k4444 Oct 18 '12 at 19:42
  • 1
    Yes, this is pretty insecure. You could also limit the nopasswd option to a single executable/script, you should read the sudo documentation howto do this – Niels van Reijmersdal Oct 18 '12 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.