Postrm script fails to execute
I have created a deb package that includes a postrm script that fails to be executed.
The error message says that the script cannot be found but it is well placed, it can be run manually with bash and the execution permissions seem ok. What else should I check to find out the cause of the failure?
Here there is an example of the error with minimal postrm script:
user@linux:~$ sudo apt-get remove mypkg Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: mypkg 0 upgraded, 0 newly installed, 1 to remove and 79 not upgraded. After this operation, 0 B of additional disk space will be used. Do you want to continue? [Y/n] y (Reading database ... 123043 files and directories currently installed.) Removing mypkg (1.8.1) ... dpkg (subprocess): unable to execute installed mypkg package post-removal script (/var/lib/dpkg/info/mypkg.postrm): No such file or directory dpkg: error processing package mypkg (--remove): installed mypkg package post-removal script subprocess returned error exit status 2 dpkg: too many errors, stopping Errors were encountered while processing: mypkg Processing was halted because there were too many errors. E: Sub-process /usr/bin/dpkg returned an error code (1) user@linux:~$ more /var/lib/dpkg/info/mypkg.postrm #!/bin/sh echo "Post rm" user@linux:~$ bash /var/lib/dpkg/info/mypkg.postrm Post rm user@linux:~$ ls -l /var/lib/dpkg/info/mypkg.* -rwxr-xr-x 1 root root 30 Jun 15 17:39 /var/lib/dpkg/info/mypkg.conffiles -rw-r--r-- 1 root root 60 Jun 21 10:27 /var/lib/dpkg/info/mypkg.list -rw-r--r-- 1 root root 5052 Jun 21 10:21 /var/lib/dpkg/info/mypkg.md5sums -rwxr-xr-x 1 root root 107 Jun 20 10:06 /var/lib/dpkg/info/mypkg.postinst -rwxr-xr-x 1 root root 25 Jun 20 13:29 /var/lib/dpkg/info/mypkg.postrm -rwxr-xr-x 1 root root 0 Jun 15 17:39 /var/lib/dpkg/info/mypkg.preinst -rwxr-xr-x 1 root root 10 Jun 15 17:39 /var/lib/dpkg/info/mypkg.prerm
The postrm script was not working because it had been created on a windows machine with CRLF line-endings.
I had assumed that was not the case because I could run it with bash. However /bin/sh in my system is pointing to dash so I could not directly run the postrm script.
Thanks to @cas comment I did a proper check and I found out the issue:
user@linux:~$ file /var/lib/dpkg/info/mypkg.postrm /var/lib/dpkg/info/mypkg.postrm: POSIX shell script, ASCII text executable, with CRLF line terminators user@linux:~$ /var/lib/dpkg/info/mypkg.postrm -bash: /var/lib/dpkg/info/mypkg.postrm: /bin/sh^M: bad interpreter: No such file or directory user@linux:~$ ls -l /bin lrwxrwxrwx 1 root root 7 Aug 24 2021 /bin -> usr/bin user@linux:~$ ls -l /usr/bin/sh lrwxrwxrwx 1 root root 4 Aug 24 2021 /usr/bin/sh -> dash