This error message may appear if you are unlucky enough to install a damaged or badly designed package and then want to remove it. Now that your package manager is “stuck” you must fix the situation by hand.
Go to the directory
/var/lib/dpkg/info and list every scripts related to the faulty package
ls .* <package_name>.
Depending the precise error modify the appropriate script (prerm or postrm). Sometime changing
/bin/sh -e by
/bin/sh could be enough. Other time you must comment the ‘faulty’ line. Worst case scenario place an
exit 0 at the second line.