recieving pipes from bash

This is a discussion on recieving pipes from bash within the Linux Programming forums, part of the Platform Specific Boards category; Hi! I read that programs read input from pipes through stdin. So that if I wrote this in bash: Code: ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    recieving pipes from bash

    Hi!
    I read that programs read input from pipes through stdin.
    So that if I wrote this in bash:
    Code:
    echo "Hei" | cat
    The cat-program finds the "Hei" text from reading stdin.


    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
     ...
    }
    Put simple: How can "more" know when to read stdin for piped info, and when not to?
    (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 08:20 AM.

  2. #2
    Registered User draugr's Avatar
    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.

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    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 10:10 AM.

  4. #4
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    Yes, that should work.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  6. #6
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    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

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 02:47 PM
  2. Replies: 3
    Last Post: 04-10-2009, 01:57 AM
  3. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 04:09 AM
  4. bash scripting?
    By Draco in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 06:15 AM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21