我创建了一个 deb 包,其中包含无法执行的 postrm 脚本。
错误消息说找不到脚本,但它位置很好,可以用 bash 手动运行,并且执行权限似乎没问题。我还应该检查什么来找出故障原因?
这里有一个最小 postrm 脚本的错误示例:
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
答案1
postrm 脚本无法工作,因为它是在带有 CRLF 行结尾的 Windows 计算机上创建的。
我原以为情况并非如此,因为我可以用 bash 运行它。但是我系统中的 /bin/sh 指向 dash,所以我无法直接运行 postrm 脚本。感谢@cas 评论,我做了适当的检查,发现了问题:
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