.sh file for apt-get update/apt-get upgrade

18,749

Solution 1

The way sudo works is that by default it gives you 15 minutes before it will ask for password again. So yes, using the two commands, will work perfectly fine, and depending on how long apt-get update runs you will need to enter password once.

However, this is not the best practice. Instead, I would suggest making your whole script run with sudo instead of placing it in front of each command in file. In other words, the script should be:

#!/bin/bash
apt-get update
apt-get upgrade

And run it as

sudo  ./my_script.sh

Notice that here we're using #!/bin/bash line to use bash shell as interpreter. This is very different from /bin/sh. For what you're doing it makes no difference, but if you need to be aware that they differ in syntax of some of the operators.

However, even better approach would be to get rid of script altogether and use function or alias that does what you want. Scripts are really for large amounts of code that you want to reuse. For 2 - 3 lines a function or alias in ~/.bashrc file that's going to be sufficient.

Personally I'd make something like this in my ~/.bashrc

alias agu="sudo bash -c 'apt-get update && apt-get upgrade'"

Solution 2

Don't forget to set DEBIAN_FRONTEND=noninteractive for those pesky prompts about which version to maintain:

sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y;

If you don't do this your script will hang in patching limbo. So if you were going to do this in a script I would just do the following:

#!/usr/bin/env bash
set -e

patching(){
    sudo apt-get update -y;
    sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y;
};

patching;

The previously stated methods would work most of the time (if they had the -y flag), until they wouldn't work due to an interactive prompt.

Hopefully, it helps someone else with their patching/bootstrap script.

Share:
18,749

Related videos on Youtube

snovosel
Author by

snovosel

Updated on September 18, 2022

Comments

  • snovosel
    snovosel over 1 year

    This may be a simple question but...

    I have an update.sh file with the following:

    sudo apt-get update
    sudo apt-get upgrade
    

    When I run this using ./update.sh (after setting it to executable), it asks me to enter my sudo password. Will this run both commands, and I can essentially only run this script once and have my apt-get update/upgrade commands taken care of?

    Thanks everyone

    • user535733
      user535733 about 7 years
      Your script should ask for your password only once while runing both commands. Note that as written, the script is interactive - it will ask you questions and wait for your response, even if you're not there. Er, Ubuntu does ship with unattended-upgrades, which does all this for you, if you wish.
    • snovosel
      snovosel about 7 years
      could you give some more details on what you mean? I'm just trying to only have to run one command for both update/upgrade. I'm not too familiar with bash scripts and the like.
    • user535733
      user535733 about 7 years
      apt already has a feature to update/upgrade daily. It's called unattended-upgrades, and it's included with all flavors with Ubuntu. You need to understand a bit about how packages and repositories work (which is good - you need to know that anyway). The settings for unattended-upgrades are within apt: /etc/apt/apt.conf.d/
    • snovosel
      snovosel about 7 years
      is there documentation for this?
    • user535733
      user535733 about 7 years
      Yes, each setting is explained within the files themselves. Be aware that apt will do exactly what you tell it to do, regardless of your actual intent. For example, updating from non-Ubuntu repos and PPAs can be quite unwise.
    • snovosel
      snovosel about 7 years
      sorry, I'm a complete beginner when it comes to this. I've usually just run sudo apt-get update/upgrade every time I start up my computer.. is this unwise? Where would I be in a situation where I'm updating from non-Ubuntu repos and PPAs?
    • user535733
      user535733 about 7 years
      If you are a complete beginner, then one step at a time. Start with the command man unattended-upgrades to learn what to do next.