1

as far as i learned in programming, when i want to add the input redirection to my program, i write my program as when it executed, it waits for the user's input (with std::cin or something similar). but i got confused by less command.

We all know that we can do something like this:

ls -la | less

but when we try to execute

less

without any arguments we get an error. how come ?!

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Shnd
  • 1,846
  • 19
  • 35
  • for the sake of completeness, I asked more detailed about this on _Unix & Linux_: [How isatty function works in brief.](http://unix.stackexchange.com/questions/118156/how-can-we-distinguish-that-input-to-our-program-is-directed-or-it-is-just-a-use) – Shnd Mar 05 '14 at 00:53

2 Answers2

4

The less accepts input from stdin or from a file.

Since it doesn't make sense to accept input from a terminal (just to display the same input back to a terminal), the less program probably checks if stdin is a terminal (with isatty) and refuses to run.

See man 3 isatty

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
2

Less is a pager, a UNIX term for a program that shows output one screenful at a time. If you pipe a program's output to less, it shows the output one page a time. If you pass file names on the command line, it shows those files page by page.

If you do neither, there's nothing to page through. It throws an error because there's nothing sensible it can do. What do you want it to show?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • can you guide me to implement this in my programs. i mean how can i get the redirected input without waiting for user's input (like std::cin) ? – Shnd Mar 04 '14 at 23:44
  • 1
    That doesn't explain how `less` distinguishes between (a) being executed with no arguments with stdin redirected from a pipe or file, and (b) being executed with no arguments with stdin coming from the keyboard. – Keith Thompson Mar 04 '14 at 23:46