#!/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?
]
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 runninggimp -version
, or querying dpkg about the version of the gimp package (see dpkg-query(1)), will already tell you whether it exists or not.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:
First line, "which" is low-case:
Here:
You can use:
Instead of capturing the output and seeing if it's empty
use this, which just considers the command's exit status (0 = success)
Your use of braces to group commands is not necessary, but will not hurt.
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.
A few technical observations and concerns you'll run into:
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 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
sudoers
policy to require a passcode for each and every instance of the use ofsudo
. 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 (viasudo
).