Contents
How to detect if stdin is a terminal or pipe?
If the interactive session needs certain capabilities, you can open the terminal device and (temporarily) set terminal attributes you need via tcsetattr (). The Python code that decides whether the interpreter runs interactively uses isatty (). The Function PyRun_AnyFileExFlags ()
How to close stdout and stdin file descriptors?
To do this I close the file descriptor associated with the stdout or stdin, so that when I open the file it opens under the descriptor that I just closed. This works, but the problem is that once this is done, I need to restore the stdout and stdin to what they should really be.
How to clone a stdin or stdout file?
What I can do for stdout is open (“/dev/tty”,O_WRONLY); But I’m not sure why this works, and more importantly I don’t know of an equivalent statement for stdin. You should use dup () and dup2 () to clone a file descriptor.
Do you do error checking on stdin descriptors?
I am not doing any error checking in that example – you should. You could create a child process, and set up the redirection inside the child only. Then wait for the child to terminate, and continue working in the parent process. That way you don’t have to worry about reversing your redirection at all.
How to read command line arguments from a pipe?
A smart script that can both read data from PIPE and command line arguments: Explanation: When a script receives any data via pipe, then the /dev/stdin (or /proc/self/fd/0) will be a symlink to a pipe. The bash [ [ -p option can check it it is a pipe or not.
How to pipe data into a bash variable?
The syntax for an implicit pipe from a shell command into a bash variable is In your examples, you are piping data to an assignment statement, which does not expect any input. A smart script that can both read data from PIPE and command line arguments:
How to read a symlink from a pipe?
Explanation: When a script receives any data via pipe, then the /dev/stdin (or /proc/self/fd/0) will be a symlink to a pipe. The bash [ [ -p option can check it it is a pipe or not. cat – reads the from stdin. If we use cat – when there is no stdin, it will wait forever, that is why we put it inside the if condition.