How to check software version in shell script?

I have two software version checks in my bash script that do not work as I expected.

DRUSH_VERSION="$(drush --version)"
echo ${DRUSH_VERSION}
if [[ "$DRUSH_VERSION" == "Drush Version"* ]]; then
    echo "Drush is installed"
  else
    echo "Drush is NOT installed"
fi
GIT_VERSION="$(git --version)"
echo ${GIT_VERSION}
if [[ "GIT_VERSION" == "git version"* ]]; then
    echo "Git is installed"
  else
    echo "Git is NOT installed"
fi

Answer:

Drush Version : 6.3.0
Drush is NOT installed
git version 1.8.5.2 (Apple Git-48)
Git is NOT installed

Meanwhile, if I change

DRUSH_VERSION = "$ {drush --version)"

to

DRUSH_VERSION = "Drush Version: 6.3.0"

is responsible

Drush Installed

Now i will use

if type -p drush;

but I would still like to get the version number.

+4
source share
1 answer

, . -, , =~ ==. git version git version 1.8.5.2 (Apple Git-48). -, $ [[ "GIT_VERSION" == "git version" ]].

, , , . ( : =~ [[ ]], *).

if [[ "$DRUSH_VERSION" =~ "Drush Version" ]]; then
...
if [[ "$GIT_VERSION" =~ "git version" ]]; then
...

, , , , , :

if which $prog_name 2>/dev/null; then...

:

which $prog_name && do something found || do something not found

. git:

if which git 2>/dev/null; then
...

which git && echo "git found" || echo "git NOT found"

. stderr /dev/null , $prog_name .

+2

All Articles