PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 6, 2022

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

 October 06, 2022     buffer, command, command-line-interface, shell, terminal     No comments   

Issue

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?


Solution

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


Answered By - jhnc
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing