Reputation: 173
I am writing a shell with both interactive and batch modes in C. I would like to print the prompt in interactive mode and don't show it in batch mode.
i.e.
bash> ./myshell
should show prompt, and
bash> ./myshell < sample.txt
should only show the output from the commands in "sample.txt", but not the prompts.
Since the parent process (which is bash) made the stdin redirection, I am not sure how can myshell tell if the input stream was from stdin or from a file?
Thanks very much in advance for the help.
Upvotes: 3
Views: 1878
Reputation: 137438
isatty(3)
can be used to determine if a given file descriptor is a tty. Shells will use this to determine what kind of input to expect.
For example:
if (isatty(STDIN_FILENO)) {
// Interactive shell
}
else {
// Redirected stdin
}
Upvotes: 4