How does "less" command get stdin input?
Asked Answered
P

3

6

I'm just wondering about this problem: if I can use something like "ls -al | less", less should have the ability of waiting for input from stdin. What I expected to happen is after running "less" command, the program hang up and wait for input(as a consequence of calling gets() or something like that.)

But why in fact it showed an error message "Missing filename ("less --help" for help)" end exited?

Thank you.

Pownall answered 28/3, 2013 at 0:34 Comment(1)
See also related question #1441751Vanessa
S
4

less command can check both argc>1 and stdin be associated with a file(not a tty).

Sublingual answered 28/3, 2013 at 0:40 Comment(2)
Could you explain how it checks whether stdin is associated with files? I thinks this is the part I wanna know. Thank you.Pownall
We C/Unix old-timers remember the rookie mistake of doing a printf between getting an error and trying to turn the errno into a message, because printf would do a call to determine whether it should do line buffering or not, so all your errno would tell you is "ENOTTY" (Not a TTY).Dianthe
R
0

The less command is not designed in that matter. When you execute a command in bash it will display all of the information at once. The less command is used to display the STDOUT of a command or the contents of a file one screen at a time.

Rheumatoid answered 28/3, 2013 at 0:40 Comment(0)
N
-1
$ ls -al | less
total 16
drwxrwxr-x  4 hdante hdante 4096 Nov 24 17:11 .
drwxr-xr-x 88 hdante hdante 4096 Mar 24 22:14 ..
drwxrwxr-x  2 hdante hdante 4096 Nov 25 01:55 new
drwxrwxr-x  3 hdante hdante 4096 Nov 24 18:27 old
(END)

It works. Something is wrong with your less. From less manual pages:

http://www.linuxmanpages.com/man1/less.1.php https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/less.1.html

The manual describes the filename as optional.

Hints to diagnose your problem:

  • try alias | grep less, to see if the command is being modified
  • try set | grep LESS, and check the scripts being run by LESSCLOSE and LESSOPEN
Nereid answered 28/3, 2013 at 0:56 Comment(1)
No that error message was the result of running "less" barely. I didn't make myself clear...Pownall

© 2022 - 2024 — McMap. All rights reserved.