2

I want to write a bash script to download and install BleachBit from a .deb file only if the version already installed is not 1.0. I am using Ubuntu 12.04 and Bleachbit is only version 0.9 in the official repositories and I cannot find a PPA with version 1.0 for Ubuntu Precise. How can I do this?

2 Answers 2

2

Here's a simpler version of your script, (keeping the humor intact :) ):

#!/bin/bash

## The && means that the script will run the next command only if this one
## succeeds, in other words, only if the string `version 1.0` is found.
bleachbit --version | grep -q 'version 1.0' &&
 echo "$(tput setaf 2)The elves have verified the BleachBit version.$(tput sgr0)" &&
   exit 0
## This block will only be executed if the grep above failed
wget -P ~/Downloads http://katana.oooninja.com/bleachbit/sf/bleachbit_1.0_all_ubuntu1204.deb &&
sudo dpkg -i ~/Downloads/bleachbit_1.0_all_ubuntu1204.deb &&
 echo "$(tput setaf 2)The elves have installed BleachBit 1.0.$(tput sgr0)" 

Note that I added && to the end of each command, that way, you will avoid errors if any of the commands fail since the script will exit on the first failed command.

A safer approach would be to change the first command to:

bleachbit --version | awk '/version/{if($NF>=1){exit 0}else{exit 1}}' 

This has the advantage that it will work fine for future versions, when the version number will be greater than 1. The $NF in awk means the last field and /version/ means the script will run on lines matching version. So, since the first line is:

info: starting BleachBit version 1.0

awk will test whether the last field (1.0) here is greater than or equal to one and will exit with a 0 status (success), if it is which means that the next block (&&) will be executed, and your script will stop.

You could also condense the entire thing to:

bleachbit --version | head -n 1 | awk '{if($NF>=1){exit 1}else{exit 0}}' &&
wget -P ~/Downloads http://katana.oooninja.com/bleachbit/sf/bleachbit_1.0_all_ubuntu1204.deb &&
sudo dpkg -i ~/Downloads/bleachbit_1.0_all_ubuntu1204

But that comes at the expense of the poor elves.

3
  • Thank you for the suggestions, terdon! One issue I see with the use of && is that I need this code to be part of a larger script so I need things to move along if BleachBit is installed or not. So, for my purposes, I won't use &&. I like your idea of using awk to account for future versions but I have had a hard time understanding how to use awk; I'm still a scripting newbie! Mar 26, 2014 at 17:47
  • 1
    @HarlemSquirrel feel free to post a Q if you like, and I'd be happy to explain this particular one in more detail if needed. As for && that can be used in a script, that's how I normally do it but yes, not for larger blocks. I would certainly not use grep -c 1.0 though because that will also match version 0.8.1.0 for example and it will match 1.0 irrespective of whether that is actually even part of the version string. For example version 0.7 revision 1.0.
    – terdon
    Mar 26, 2014 at 18:04
  • Very good point about using 'version 1.0' instead of '1.0' so I am changing my code! Mar 27, 2014 at 12:48
1

I put this short script together that seems to work for me! This also include a bit of my own brand of elvish humor. I am using this code in a larger script so I can copy and paste this conditional anywhere I need it.

#!/bin/bash

# Check BleachBit Version and install if necessary
if [ "$(bleachbit --version | grep -c 'version 1.0')" = "0" ];
    then 
        wget -P ~/Downloads http://katana.oooninja.com/bleachbit/sf/bleachbit_1.0_all_ubuntu1204.deb
        sudo dpkg -i ~/Downloads/bleachbit_1.0_all_ubuntu1204.deb
        echo "$(tput setaf 2)The elves have installed BleachBit 1.0.$(tput sgr0)"
    else
        echo "$(tput setaf 2)The elves have verified the BleachBit version.$(tput sgr0)"
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .