为什么apt-get在安装某些东西时会消耗stdin而不是其他东西?

时间:2019-02-17 17:56:59

标签: shell terminal command command-line-interface buffer

我通过macOS终端连接到远程Debian系统。

如果apt-get安装了某些内容,则apt-get之后的命令将永远不会运行

首先,我从macOS上的文本文件复制这三个命令,然后按一次命令 + v 将其粘贴到终端中:

sudo apt-get -y remove tree
sudo apt-get -y install tree
echo hi

这是我在终端机中看到的内容。

lone@lone:~$ sudo apt-get -y remove tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'tree' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.
lone@lone:~$ sudo apt-get -y install tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 17 not upgraded.
Need to get 0 B/46.1 kB of archives.
After this operation, 106 kB of additional disk space will be used.
Selecting previously unselected package tree.
(Reading database ... 31853 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.7.6.1-2) ...
lone@lone:~$ 

第三个命令echo hi从未执行过。为什么?

如果apt-get没有安装任何东西,则apt-get之后的命令会运行

下一次,我只需按一次命令 + v 粘贴以下两个命令即可:

sudo apt-get -y install tree
echo hi

这次,由于已经安装了tree,因此apt-get不需要再次安装。这是我看到的输出:

lone@lone:~$ sudo apt-get -y install tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
tree is already the newest version (1.7.0-5).
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.
lone@lone:~$ echo hi
hi

这次执行echo hi。为什么?

每次执行这两套操作时,两个结果都是可重复的。为什么echo hi命令在第一个示例中不运行,而在第二个示例中运行?

1 个答案:

答案 0 :(得分:1)

apt-get或由apt-get调用的程序正在清空其stdin(恰好与您的命令列表所在的shell相同)。

由于您不知道需要从用户那里读取任何内容,因此可以从/ dev / null重定向stdin:

sudo apt-get -y remove tree </dev/null
sudo apt-get -y install tree </dev/null
echo hi