![]() |
| | #1 |
| Shalom Join Date: Oct 2005 Location: Norway
Posts: 92
| recieving pipes from bash I read that programs read input from pipes through stdin. So that if I wrote this in bash: Code: echo "Hei" | cat But how does for example the more-program work? Because I can pipe things to more in bash, but if I write just "more" without anything else, it just says: " Illegal command blablab". Shouldnt the "more"-program be waiting from input from stdin? Or does it have some way of doing like this? Code: if(pipedInfoInStandardInput) {
readFromStandardInput();
} else { // Normal programflow
...
}
(Because when I write "more" and nothing else, it doesent accept input from stdin...it's only when I use pipes that it reads stdin) Last edited by Drogin; 04-12-2009 at 07:20 AM. |
| Drogin is offline | |
| | #2 |
| Registered User Join Date: Mar 2009
Posts: 9
| Hei Drogin ![]() It's actually simple and you pretty much guessed it: the "magic function" is isatty(int filedes). The function can tell you if stdin (file descriptor 0) comes from an interactive terminal or not, so you can do different things depending on what it tells you. |
| draugr is offline | |
| | #3 |
| Shalom Join Date: Oct 2005 Location: Norway
Posts: 92
| aah, thanks a lot! ![]() So we could do something like this then? Code: #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
if(isatty(fileno(stdin)) != 1) {
// Is it safe to assume that we can read info from stdin as if it was a piped, now?
} else {
// Programflow that does terminal things
}
return EXIT_SUCCESS;
}
Last edited by Drogin; 04-12-2009 at 09:10 AM. |
| Drogin is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 9
| Yes, that should work. |
| draugr is offline | |
| | #5 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| fileno(stdin) can be abbreviated as STDIN_FILENO (in unistd.h), which is defined by POSIX to be 0.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #6 |
| Shalom Join Date: Oct 2005 Location: Norway
Posts: 92
| It can, but don't you think isatty(fileno(stdin)) is more readable than a "magic" number 0? EDIT: Ah,nvm, understood what you meant now |
| Drogin is offline | |
| | #7 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| I find STDIN_FILENO to be more readable than fileno(stdin). Whether 0 is more readable is a question of how much you work with POSIX systems, I suppose. I wouldn't have any problems with it, but I suppose people less used to it would have.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pipes sometimes fail | EVOEx | Linux Programming | 2 | 05-02-2009 01:47 PM |
| argument with space passed to bash script and further to executable | lehe | Linux Programming | 3 | 04-10-2009 12:57 AM |
| Pipes | Martin Kovac | C Programming | 1 | 03-31-2009 03:09 AM |
| bash scripting? | Draco | Linux Programming | 1 | 01-08-2007 06:15 AM |
| Services and Pipes | nickname_changed | Windows Programming | 0 | 07-16-2003 06:46 AM |