-2

In bash, If I have a file called file1 how can I use stdin along with the binary corresponding to the following code in order to return the integer file descriptor corresponding to file1 ?

int main(){

     return fileno(stdin);

}

Thank you

p.s:

I'm looking for a bash expression like this:

user:~$expression | ./binary

p.s (bis):

I had some difficulties to make a synthetic title, please tell me if you find a better one

  • 3
    `0` is the file descriptor (number) corresponding to `stdin`. You'd use `./binary < file1` to make standard input correspond to `file1`. If neither of those is relevant, you need to ask a clearer question. – Jonathan Leffler Jan 22 '17 at 23:37
  • If this is about the bash, remove the C tag and show your bash code. Otherwise this is not related to any shell. – too honest for this site Jan 22 '17 at 23:48
  • Are you confusing file descriptors and inodes? As @Jonathan already was writing, the file descriptor of `stdin` always will be `0`, so your example program always has exit status `0`. If this indeed is your confusion, see http://stackoverflow.com/questions/9480568/find-inode-number-of-a-file-using-c-code. – Freek Wiedijk Jan 22 '17 at 23:51

1 Answers1

2

In bash, If I have a file called file1 how can I use stdin along with the binary corresponding to the following code in order to return the integer file descriptor corresponding to file1 ?

You have an apparent misconception. There is no single the file descriptor for any given file, nor indeed any file descriptor at all for a file that is not open. File descriptors are per-process resources associated in each process with that process's open files. The question is thus based on a false premise.

Furthermore, even if you redirect a process's standard input to come from file1, that does not identify stdin with file1 in that process. It merely means that you can read file1's contents via stdin, once. You cannot in that case gain any other information about file1 from stdin, so even in a more general sense, the methodology you propose to use to get information about file1 (other than its contents) is not viable.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157