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:

#!/bin/bash
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?

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?
–
poz2k4444Oct 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.
–
zmodeOct 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??
–
poz2k4444Oct 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?
–
UtahJarheadOct 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.
–
zmodeOct 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:

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.

So, it's a bad idea use apt-get inside a script??? I don't understand that last part of your answer
–
poz2k4444Oct 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.
–
PatrickOct 19 '12 at 19:17