1

coding virgin needs help please - once package pv is installed this code doing always does else rather than then. Why?

# First check if pv package is installed, if not, install it first
PACKAGESTATUS=`dpkg -s pv | grep Status`;

if [[ $PACKAGESTATUS == S* ]]
   then
      echo "Package 'pv' is installed."
   else
      echo "Package 'pv' is NOT installed."
      echo "Installing package 'pv'. Please wait..."
      apt-get -y install pv
fi

nb it is part of a backup script that backs up a raspberry pi running Raspbian wheezy raspbian/2013-02-09 and pv is version 1.2.0 and the author is unavailable.

Thanks.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user1613312
  • 374
  • 2
  • 15

2 Answers2

1

Instead of capturing the output of the dpkg | grep pipeline, just check its exit status:

if dpkg -s pv | grep -q Status; then
   then
      echo "Package 'pv' is installed."
   else
      echo "Package 'pv' is NOT installed."
      echo "Installing package 'pv'. Please wait..."
      apt-get -y install pv
fi

The -q option to grep is used to suppress the output of any matched lines, since you don't need to see them.

chepner
  • 497,756
  • 71
  • 530
  • 681
1

There is a minor syntax error in chepners answer, due to the double 'then'.

I had better luck with this version

if `dpkg -s pv | grep -q Status;`
   then
      echo "Package 'pv' is installed."
   else
      echo "Package 'pv' is NOT installed."
      echo "Installing package 'pv'. Please wait..."
      apt-get -y install pv
fi
AndersW
  • 58
  • 7