6 File-descriptor redirect Flashcards

1
Q

Start a program and connect the three default file descriptors stdin, stdout, and stderr to the terminal:

A

$ command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Connect stdout of command1 to stdin of command2 and stdout of
command2 to stdin of command3 by forming a pipe:

A

$ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advantages of pipe syntax over function syntax

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Execute several commands (or entire pipes) in sequence:

A

$ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Conditional execution depending on success of previous command (as in logic-expression short-cut):

A

$ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Send stdout to file

A

$ command >filename

if existed before, will be truncated to zero length by opening in this way .. use&raquo_space; for appending

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Append stdout to file

A

$ command&raquo_space;filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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):

A

$ 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Feed stdin from file rather than from keyboard input

A

$ command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Open other file descriptors for input, output, or both

A

$ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

“Here Documents” allow us to insert data into shell scripts directly such that the shell will feed it into a command via standard input.

A

$ tr < THEEND

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Redirecting to or from /dev/tcp/hostname /port will open a TCP socket connection:

A

{ 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly