挂钩脚本中未设置 Dpkg 环境变量 DPKG_HOOK_ACTION

挂钩脚本中未设置 Dpkg 环境变量 DPKG_HOOK_ACTION

根据man dpkgI can use DPKG_HOOK_ACTIONto get the current dpkg action is running in my dpkg hook script

--pre-invoke=command
--post-invoke=command
      Set an invoke hook command to be run via “sh -c” before or after the dpkg run for the unpack, configure, install, triggers-only, remove  and  purge  dpkg  actions.  This
      option  can be specified multiple times. The order the options are specified is preserved, with the ones from the configuration files taking precedence.  The environment
      variable DPKG_HOOK_ACTION is set for the hooks to the current dpkg action. Note: front-ends might call dpkg several times per invocation, which might run the hooks  more
      times than expected.

但它似乎在这个钩子命令中不起作用。知道这里出了什么问题吗?

$ cat /etc/apt/apt.conf.d/99testhook
DPkg::Pre-Invoke {"echo This is testhook. Current action is $DPKG_HOOK_ACTION; exit 0";};

$ sudo apt-get install screen
...
Fetched 628 kB in 0s (4,366 kB/s)
This is testhook. Current action is
Selecting previously unselected package screen.

答案1

这仅适用于--pre-invoke--post-invoke选项指定的命令,不适用于在配置中设置命令的情况。

这可以通过将 echo 命令放入脚本中来演示:

# cat > /tmp/pre-invoke.sh <<'EOF'
#!/bin/sh
echo This is testhook. Current action is $DPKG_HOOK_ACTION; exit 0
EOF
# chmod +x /tmp/pre-invoke.sh
# dpkg --pre-invoke=/tmp/pre-invoke.sh -i /var/cache/apt/archives/rsync_3.1.1-2+b1_amd64.deb
This is testhook. Current action is install
(Reading database ... 113857 files and directories currently installed.)
Preparing to unpack .../rsync_3.1.1-2+b1_amd64.deb ...
Unpacking rsync (3.1.1-2+b1) over (3.1.1-2+b1) ...
Setting up rsync (3.1.1-2+b1) ...
Restarting rsync daemon: rsync.
Processing triggers for man-db (2.6.7.1-1) ...

答案2

也可以使用以下方法来配置此行为DPKg::Options::(wurtel 解释了为什么DPkg::Pre-Invoke是错误的地方):

apt.conf这是演示脚本的示例:

DPKg::Options:: "--pre-invoke=/tmp/pre-invoke.sh";

结果例如:

After this operation, 17.4 kB disk space will be freed.
This is testhook. Current action is remove
This is testhook. Current action is remove
(Reading database ... 60383 files and directories currently installed.)

新答案,因为我无法发表评论[但由于声誉]

相关内容