Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
#!/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 Please help me?

share|improve this question

5 Answers 5

up vote 6 down vote accepted
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
share|improve this answer

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.

share|improve this answer
    
Thank you for this cool tip ^^ –  rahmu Jun 29 '12 at 13:15

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" ]
share|improve this answer

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.

share|improve this answer

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).
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.