Skip to main content
deleted 3 characters in body
Source Link
Sildoreth
  • 2k
  • 8
  • 25
  • 42

Detect whether data exists in standard input is coming from the terminal

I have a shell script that can read from a file or from standard input. Currently, if you don't give it a file or pipe text into it, it starts accepting input at the command prompt.

Instead, I would like to to print "help" information in this scenario. Is there a standard way to do this? And if not, is there a way to detect standard input without affecting it? Or is there a way to detect where the would-be input is coming from?

I know that the read command can tell me whether there's input via its exit status. But it also acts on the input, affecting subsequent reads.

Detect whether data exists in standard input

I have a shell script that can read from a file or from standard input. Currently, if you don't give it a file or pipe text into it, it starts accepting input at the command prompt.

Instead, I would like to to print "help" information in this scenario. Is there a standard way to do this? And if not, is there a way to detect standard input without affecting it? Or is there a way to detect where the would-be input is coming from?

I know that the read command can tell me whether there's input via its exit status. But it also acts on the input, affecting subsequent reads.

Detect whether input is coming from the terminal

I have a shell script that can read from a file or from standard input. Currently, if you don't give it a file or pipe text into it, it starts accepting input at the command prompt.

Instead, I would like to print "help" information in this scenario. Is there a standard way to do this? And if not, is there a way to detect standard input without affecting it? Or is there a way to detect where the would-be input is coming from?

I know that the read command can tell me whether there's input via its exit status. But it also acts on the input, affecting subsequent reads.

Source Link
Sildoreth
  • 2k
  • 8
  • 25
  • 42

Detect whether data exists in standard input

I have a shell script that can read from a file or from standard input. Currently, if you don't give it a file or pipe text into it, it starts accepting input at the command prompt.

Instead, I would like to to print "help" information in this scenario. Is there a standard way to do this? And if not, is there a way to detect standard input without affecting it? Or is there a way to detect where the would-be input is coming from?

I know that the read command can tell me whether there's input via its exit status. But it also acts on the input, affecting subsequent reads.