How could I make a bash script to execute apt-get?

6,393

Solution 1

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.

Solution 2

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:

#/usr/bin/bash
#
# 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.

Solution 3

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:

#!/bin/bash
sudo apt-get purge postfix -y
Share:
6,393

Related videos on Youtube

poz2k4444
Author by

poz2k4444

Updated on September 18, 2022

Comments

  • poz2k4444
    poz2k4444 over 1 year

    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?

  • poz2k4444
    poz2k4444 over 11 years
    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
    poz2k4444 over 11 years
    this will not generate security issues?
  • UtahJarhead
    UtahJarhead over 11 years
    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?
  • dset0x
    dset0x over 11 years
    @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.
  • Niels van Reijmersdal
    Niels van Reijmersdal over 11 years
    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
  • ghoti
    ghoti over 11 years
    @poz2k4444 - you can sudo -k to clear the cached password, so you can test whether your script will run sudo without asking for a password. Then you can man sudoers to see how to construct the necessary permission for you to do this automatically in the future.
  • UtahJarhead
    UtahJarhead over 11 years
    I still think it's a terrible idea to have to purge with apt-get so often that it's being scripted. Why don't you tell us what's really going on?
  • poz2k4444
    poz2k4444 over 11 years
    @UtahJarhead I will not purge anything, but I'll use apt-get to restore services just in case something bad happens with my actual services, as I said, this is just a test I made to see what happen, but nothing is gonna be purged, what I really need it's knowing how to script my services' installation, NFS, LDAP, DNS, DHCP and so on, as I said, I'm new on scripting and I don't want to get a script from internet, I want to do it by myself ;)
  • poz2k4444
    poz2k4444 over 11 years
    So, it's a bad idea use apt-get inside a script??? I don't understand that last part of your answer
  • Patrick
    Patrick over 11 years
    @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.