Lone Learner
Lone Learner

Reputation: 20688

Why does apt-get consume stdin when it installs something but not otherwise?

I am connected to a remote Debian system via macOS Terminal.

Command after apt-get never runs if apt-get installs something

At first, I copy these three commands from a text file on my macOS and paste it into the terminal with a single command+v press:

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

Here is what I see in the Terminal.

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:~$ 

The third command, echo hi, was never executed. Why?

Command after apt-get does run if apt-get does not install anything

Next time, I simply paste these two commands with a single command+v press:

sudo apt-get -y install tree
echo hi

This time, since tree is already installed, apt-get does not need to install it again. This is the output I see:

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

This time echo hi was executed. Why?

Both results are reproducible every time I perform these two sets of operations. Why does the echo hi command not run in the first example but does in the second example?

Upvotes: 1

Views: 716

Answers (2)

youfu
youfu

Reputation: 1627

Append -o=DPkg::FlushSTDIN=0 to your apt-get install command.

Ref: Source Code

Upvotes: 0

jhnc
jhnc

Reputation: 16817

apt-get or a program called by apt-get is emptying its stdin (which happens to be the same as the shell's, where your list of commands originates).

Since you know nothing needs to be to read from the user, redirect stdin from /dev/null:

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

Upvotes: 1

Related Questions