April 22, 2017

Relevant Linux Features: Pipe

Relevant Linux Features: Pipe

The pipe is implemented with the  "|" symbol. It takes the output (stdout) of the command on the left and sends it as input (stdin)  for the command on the right:


In the example below, docker run --help is the first command. Its output is used as input to the more command, which displays the output, one screen at a time:

Note: stderr (standard error) is NOT passed through the pipe. I.e. we are not able to pass stderr through the pipe as we can stdout.

Relevant Linux Features: Command Substitution

Command substitution - $(command)

In command substitution, the shell runs command, however instead of displaying the output of command, it stores the output in a variable. You can then pass that variable as input to another command.

The syntax of command substitution is $(command) or the older `command`, using back-ticks.

Let's say you want to remove the most recent container running. You can use docker ps -a which lists all containers, starting with the most recent, then copy the Container ID into the docker rm <Container ID> command:

 
Alternatively, you can use Command Substitution and let the system do some of the work for you. The following command runs docker ps -lq which gets the ID of the most recent container, it passes that ID to the docker rm command: $ docker rm $(docker ps -lq):

Relevant Linux Features: Standard I/O

Relevant Linux Features: Standard I/O

stdin, stdout, stderr:
Linux recognizes three input/output streams:
  • STDIN
    • standard input
    • by default input to a command comes from the keyboard
    • STDIN can be redirected to come from other than the keyboard, e.g. a file or a device
      • use the "<" symbol to redirect input, e.g. command < file
      • e.g. to send the contents of file, text001 as INPUT to the command pr and offset each line by 5 spaces:
        $ pr --indent=5 < text001
  • STDOUT
    • standard output
    • by default, output of a command is sent to the terminal
    • STDOUT can be redirected to go to other than the terminal, e.g. a file or a device
      • use the ">" symbol to redirect output, e.g. command > file
      • e.g. send the output of command, ls to a file, text001 instead of the terminal:
        $ ls > text001
  • STDERR
    • standard error
    • by default, error from a command is sent to the terminal
    • STDERR can be redirected to go to other than the terminal, e.g. a file or a device
      • use the "2>" symbol to redirect error, e.g. command 2> file
      • e.g. send the error of command, ls to a file, capture.err instead of the terminal:
        $ ls file 2> capture.err

Relevant Linux Features: Redirection

Relevant Linux Features: Redirection

Linux allows I/O to be redirected away from the default source or target.

The default source of STDIN is the keyboard, i.e. by default a command expects to get its input from the keyboard. To send input to a command, from a file, use the "<" redirection symbol.

The default target of STDOUT is the terminal or screen, i.e. by default a command expects to send its output to the screen. To redirect it elsewhere, use the ">" symbol:

Note: "command > file" sends the output to a file, "file". If "file" already exists, any existing content is overwritten. To instead append the output to the end of the file, use ">>" instead, i.e. "command >> file".

The default target of STDERR is the screen, i.e. by default a command expects to send its error output to the screen. To redirect it elsewhere, use the "2>" symbol:

Note: "command 2> file" send the output to a new file, "file". If "file" already exists, any existing content is overwritten. To instead append any new output to the end of the file, use "2>>" instead, i.e. "command 2>> file".