6 File-descriptor redirect Flashcards
Start a program and connect the three default file descriptors stdin, stdout, and stderr to the terminal:
$ command
Connect stdout of command1 to stdin of command2 and stdout of
command2 to stdin of command3 by forming a pipe:
$ command1 | command2 | command3
Also connects terminal to stdin of command1, to stdout of command3, and to stderr of all three.
Buffers between them, no need for context switch after every byte exchanged
Advantages of pipe syntax over function syntax
Note how this function concatenation notation makes the addition of command arguments somewhat clearer compared to the mathematical notation command3(command2(command1(arg1), arg2), arg3):
$ ls -la | sort -n -k5 | less
Execute several commands (or entire pipes) in sequence:
$ command1 ; command2 ; command3
Sequential ordering of commands (2 waits until 1 finishes)
Reconnect terminal/keyboard to standard out / in after each command
e.g
$ date ; host ryath.ds.cam.ac.uk
Conditional execution depending on success of previous command (as in logic-expression short-cut):
$ make ftest && ./ftest
$ ./ftest || echo ‘Test failed!’
Return value 0 for success is interpreted as Boolean value “true”, other return values for problems or failure as “false”. The trivial tools true and false simply return 0 and 1, respectively.
Send stdout to file
$ command >filename
if existed before, will be truncated to zero length by opening in this way .. use»_space; for appending
Append stdout to file
$ command»_space;filename
Send both stdout and stderr to the same file. First redirect stdout to filename, then redirect stderr (file descriptor 2) to where stdout goes (target of file descriptor 1 = &1):
$ command >filename 2>&1
Should not redirect two separate file descriptors into the same file … file opened twice – two current locations in that file – two streams would interfere
First redirect stdout to filename … gets opened only once
2>&1 – redirect 2 stderr into the file already opened by stdout (&1)
Feed stdin from file rather than from keyboard input
$ command
Open other file descriptors for input, output, or both
$ command 0out 2»log 3auxout 5<>data
Can write commands which interact with file system from command line … we can execute commands that don’t have the ability to interact with the file system and write via the files we have opened for them from the command line.
“Here Documents” allow us to insert data into shell scripts directly such that the shell will feed it into a command via standard input.
$ tr < THEEND
Redirecting to or from /dev/tcp/hostname /port will open a TCP socket connection:
{ echo “GET /~mgk25/iso-paper.c” >&3 ; cat /dev/tcp/www.cl.cam.ac.uk/80
- HTTP protocol to load webpage from web server … across TCP connection www.cl.cam.ac.uk:80
- bash has ability to open arbitrary socket if you provide this file name /dev/tcp/…/port in a redirect command
- send the HTTP request to that file descriptor GET
- read out from that same file descriptor 3
- feed both of these command - put braces around them so they are treated as one unit
- outside these units open fd 3 for both reading and writing