我可以看到一个老问题,但现在情况类似。通常我会使用sudo aptitude install -P PACKAGE_NAME
,安装前总是问什么。但是,现在Debian中的默认软件包管理器是apt|apt-get
并且它不具有此功能。当然我仍然可以安装aptitude
和使用它。但是我apt-get
在安装前写了一些小的sh / bash包装函数/脚本来询问。它真的很原始,我在终端中将其编写为函数。
$ f () { sudo apt-get --simulate install "$@" | grep -v '^Inst\|^Conf'; read -p 'Do You want to continue (y/N): ' ans; case $ans in [yY] | [yY][eE][sS]) sudo apt-get -y install "$@";; *);; esac; }
现在,让我们更清楚一点:
f () {
# Do filtered simulation - without lines contains 'Inst' and 'Conf'
sudo apt-get --simulate install "$@" | grep -v '^Inst\|^Conf';
# Interact with user - If You want to proceed and install package(s),
# simply put 'y' or any other combination of 'yes' answer and tap ENTER.
# Otherwise the answer will be always not to proceed.
read -p 'Do You want to continue (y/N): ' ans;
case $ans in
[yY] | [yY][eE][sS])
# Because we said 'yes' I put -y to proceed with installation
# without additional question 'yes/no' from apt-get
sudo apt-get -y install "$@";
;;
*)
# For any other answer, we just do nothing. That means we do not install
# listed packages.
;;
esac
}
要将此功能用作sh / bash脚本,只需创建脚本文件,例如my_apt-get.sh
包含内容(注意:清单不包含注释,使其简短一些;-)):
#!/bin/sh
f () {
sudo apt-get --simulate install "$@" | grep -v '^Inst\|^Conf';
read -p 'Do You want to continue (y/N): ' ans;
case $ans in
[yY] | [yY][eE][sS])
sudo apt-get -y install "$@";
;;
*)
;;
esac
}
f "$@"
然后将其放入例如~/bin/
并使用使其可执行$ chmod u+x ~/bin/my_apt-get.sh
。如果~/bin
您的PATH
变量中包含目录,则可以通过以下方式简单地执行它:
$ my_apt-get.sh PACKAGE_NAME(S)_TO INSTALL
请注意:
- 该代码确实使用
sudo
。如果您使用root
帐户,则可能需要对其进行调整。
- 该代码不支持外壳自动补全
- 不知道代码如何与外壳模式一起工作(例如“!”,“ *”,“?”,...)