Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 4768  / 2 Years ago, sat, august 13, 2022, 3:37:02
#!/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?


More From » bash

 Answers
6
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.
'
exit
fi
fi

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

[#37237] Sunday, August 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ameatoes

Total Points: 321
Total Questions: 106
Total Answers: 112

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
ameatoes questions
Tue, Aug 16, 22, 22:50, 2 Years ago
Fri, May 14, 21, 03:36, 3 Years ago
Sat, Oct 8, 22, 01:00, 2 Years ago
Fri, Feb 17, 23, 14:44, 1 Year ago
;