2
#!/bin/bash
if [ "$(Which gimp)" != ""]
then
{
  if [ "$(gimp -version)" != 2.8 ]
  then
{
sudo apt-get remove gimp
sudo add-apt-repository ppa:otto-kesselgulasch/gimp
sudo apt-get update
sudo apt-get install gimp
}
else 
  echo You already have gimp 2.8
fi  
}
else
{
  sudo add-apt-repository ppa:otto-kesselgulasch/gimp
  sudo apt-get update
  sudo apt-get install gimp
}
fi      

I am trying to make a gimp 2.8 installer in Bash. How can I get this to work?

0

5 Answers 5

7
if [ "$(which gimp)" != ""]

] must be the [ command's last argument, and it must be a separate argument, hence you need a space before it. See Bash Pitfall 10.

But, don't use which. It is a non-standard, external command that looks for a file in PATH. It behaves differently on different systems, and you can't really rely on a useful output or exit status. The shell already provides better ways of checking if a command exists and will work consistently on any system, so better learn those. See Bash FAQ 81. In this case though, you don't need to test if gimp exists, just running gimp -version, or querying dpkg about the version of the gimp package (see dpkg-query(1)), will already tell you whether it exists or not.

if [ "$(gimp -version)" != 2.8 ]

AndAC already gave a solution for this one, but I'll provide another one; comparing the version numbers. dpkg provides a way to compare two versions, namely dpkg --compare-versions ver1 op ver2. E.g. dpkg --compare-versions 2.6.12 '<' 2.8.0-1ubuntu0ppa6~precise will return true since version 2.6.12 is older than 2.8.0-1ubuntu0ppa6~precise. See dpkg(1).

All the brackets ({ and }) in that script are pointless, they serve no purpose, so you might as well remove them.

Putting this all together:

#!/usr/bin/env bash

# Query dpkg to get the version of the currently installed gimp package.
# The command returns false if the package is not installed.
if version=$(dpkg-query -W -f='${Version}' gimp 2>/dev/null); then 

    # Check if it's older than 2.8
    if dpkg --compare-versions "$version" '<' 2.8; then
        apt-get remove gimp || exit
    else
        printf 'Looks good.\n'
        exit
    fi
fi

add-apt-repository ppa:otto-kesselgulasch/gimp &&
apt-get update &&
apt-get install gimp
1

First line, "which" is low-case:

if [ "$(which gimp)" != ""]

Here:

if [ "$(gimp -version)" != 2.8 ]

You can use:

gimp_ver=$(gimp -version)
gimp_ver=$(echo ${gimp_ver##* } | cut -f1-2 -d'.')
if [ $gimp_ver != "2.8" ]
1

Instead of capturing the output and seeing if it's empty

if [ "$(which gimp)" != ""] 

use this, which just considers the command's exit status (0 = success)

if which gimp &>/dev/null
then 

Your use of braces to group commands is not necessary, but will not hurt.

0
0

Since there is no information about what goes wrong it it is hard to help. But at the very least, you should fix the mis-spelling of "which", you have written it with a capital W and bash is case sensitive.

0

A few technical observations and concerns you'll run into:

  1. Everything that @AndAC has said.
  2. add-apt-repository may not be installed on everyone's system. On clean installations I've seen, 12.04 includes it by default but older ones do not. You may want a stop at the beginning to do
    if [ "$(which add-apt-repository)" != ""]
    and in instances where it does not exist, force installation of the parent package as a dependency before continuing on in the script (note: omit sudo if you take the third bullet point in this list as part of your script): sudo apt-get install python-software-properties
  3. You may want to utilize the methods described here to require your entire script to run as super user. And I say this because on some systems, there is sudoers policy to require a passcode for each and every instance of the use of sudo. I have a few scripts that needed that, and by adding in the information in the answer I linked, you can partially avoid that problem by requiring that you run the script as superuser (via sudo).

You must log in to answer this question.

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