Bookmark this page

Guided Exercise: Controlling Command Output

Manipulate command output.

Outcomes

  • Run non-interactive commands and interactive commands.

  • Redirect standard input to another command.

  • Redirect standard output to a file and to a device.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start files-stream

Instructions

  1. On the command line, use the echo command as a non-interactive command to print a message to the command line:

    [student@workstation ~]$ echo "Hi there!"
    Hi there!
  2. Use the cat command as an interactive command:

    [student@workstation ~]$ cat
    1. Type a string into the running cat session and confirm that your standard input is repeated to standard output.

      [student@workstation ~]$ cat
      goodbye
      goodbye

      You typed the first string, and the second string is displayed by the cat command.

    2. Press Ctrl+C to interrupt the cat session.

      ...output omitted...
      goodbye
      ^C
  3. Redirect standard output to a file by using a greater than (>) symbol. Use the cat command to confirm the contents of the file:

    [student@workstation ~]$ echo test > example.txt
    [student@workstation ~]$ cat example.txt
    test
  4. Redirect standard output to a file again by using a greater than (>) symbol. Use the cat command to confirm that the contents of the file have been overwritten by the new value:

    [student@workstation ~]$ echo hello > example.txt
    [student@workstation ~]$ cat example.txt
    hello
  5. Redirect standard output to the end of a file by using two greater than (>>) symbols:

    [student@workstation ~]$ echo world >> example.txt
  6. Use the cat command to view the contents of the example.txt file that you created:

    [student@workstation ~]$ cat example.txt
    hello
    world
  7. Redirect standard output from the echo command to the input of the rev command:

    [student@workstation ~]$ echo hello | rev
    olleh
  8. In one line only, redirect standard output from the rev command to the input of the tee command, and use the tee command to write the output to both your terminal and to a file. Use the cat command to confirm success.

    [student@workstation ~]$ echo hello | rev | tee example.txt
    olleh
    [student@workstation ~]$ cat example.txt
    olleh
  9. Use the ls command on a directory that does not exist to generate an error message on standard error:

    [student@workstation ~]$ ls void
    ls: cannot access 'void': No such file or directory
  10. Redirect standard error to a file. Use the cat command to confirm the contents of the file.

    [student@workstation ~]$ ls void 2> example.txt
    [student@workstation ~]$ cat example.txt
    ls: cannot access 'void': No such file or directory
    1. Redirect standard error to a file using two greater than (>>) symbols. Use the cat command to confirm the contents of the file.

      [student@workstation ~]$ ls void 2>> example.txt
      [student@workstation ~]$ cat example.txt
      ls: cannot access 'void': No such file or directory
      ls: cannot access 'void': No such file or directory
  11. Redirect standard error to the /dev/null device.

    [student@workstation ~]$ ls void > /dev/null

    The command does not produce any output and the error messages are discarded.

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish files-stream

Revision: rh104-9.1-3d1f2bc