[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to check that if a package is installed?



S Mathias wrote:
> if [ "$(dpkg -l | awk '/tofrodos/ && /^ii/ {print $2}' | awk '/^tofrodos/')" == "" ]; then echo -e "error: no zip package detected\!\n\nYou could install it like e.g.: \"apt-get install tofrodos\""; fi
>
> Is there a better way then this?

Invoking dpkg can be a somewhat slow and heavy task.  It is much
faster to simply look to see if /usr/share/doc/tofrodos exists.
Debian Policy says that every package must have include a copyright
notice in that directory and therefore if the package is installed
then that directory will exist.

if [ ! -d /usr/share/doc/tofrodos ]; then
  echo "Error: missing tofrodos package"
  echo "You could install it with \"apt-get install tofrodos\""
fi

That is a very fast operation.  It is what I do in my own scripts.  I
definitely recommend doing that over running 'dpkg' each and every
time.  However I don't know if some of the newer dpkg exclusion
features now changes that assumption.  Dear lazyweb, does anyone know?

Using 'echo -e' isn't portable.  Ironically 'echo' is one of the least
portable commands.  I strongly recommend just working around it by
restructuring your statements.  Or if you must use escape sequences in
your string then use 'printf' instead.  The 'printf' command is
standard and avoids the problem.

If you decide you should use dpkg directly to query for the existence
of the package then it is better to use dpkg --status.  Sorry but that
awk piped to awk pipeline isn't particularly attractive.

if ! dpkg --status tofrodos | grep -q "^Status: install ok installed$"; then
  echo "Error: Missing package: tofrodos"
  echo "You could install it with: apt-get install tofrodos"
fi

Also, you are looking for 'tofrodos' but your error message said
'zip'?  I assume one or the other was a typo.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: