How to catch the exit codes coming from preinst file while installing debian package?

I am trying to install a debian package on my box , through a shell script , i.e in the shell script I am giving the dpkg -i command . Internally there is a preinst file which is being used . Now in this file one of the condition is failing , let's say when x=0 I am exiting with exit 2

I want that whatever exit code that preinst file is exiting with , I should be able to catch that exit code and in my install.sh file, I will do a print statement based on it

Problem is when I am doing echo $? it always gives me 1 , stating that dpkg installation failed , but that specific return code I am not getting . Is there any way to this , Thanks in Advance


Solution 1:

I am assuming that some condition is not met in preinst file of the deb package. I think it would be better to move the checks from preinst to the wrapper script i.e. install.sh file.

eg:

#!/bin/bash

if [ "$x" -eq "0" ]; then
   echo Check x = 0 failed
   exit 1;
fi
#Put all other such checks here...

dpkg -i package.deb

This is assuming you can modify preinst and installer.sh!

This blog on install wrapper is a good 10-15 min read!

Key Point from blog:

Install wrappers are usually required for one of the following reasons (not limited to these):

  1. Pre / Post-requisites
  2. Pre / Post scripting, e.g: delete a file / shortcut, create a registry key
  3. Pre-install condition checking, e.g: correct OS, correct computer name (or part of name)
  4. Check the version of an installed pre-requisite, e.g: version of MS Office
  5. Check if a user is logged on
  6. Check if a program is in use by the user, e.g: in the event of an upgrade