OPS145 Lab 7 Newversion: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
Line 38: Line 38:
* '''A file''', which is commonly called '''redirection'''
* '''A file''', which is commonly called '''redirection'''
* '''Another program''', which is commonly called '''piping'''
* '''Another program''', which is commonly called '''piping'''
== Redirect STDOUT to a file ==
When the shell encounters the character '''>''' it will interpret that as an instruction to redirect the standard output from the application you're running into a file.
A simple example of when you might use this is to save a listing of all the files you have now, so that you can do something with that list later.
* In a terminal create the directory '''~/lab6''' but keep your PWD in your home directory.
* Then run ls twice. The first time on its own, the second time redirecting its output to a file:<syntaxhighlight lang="bash">
ls
ls > lab6/homecontents.txt
</syntaxhighlight>
Note that the second time there is no output on the terminal. That's because you instructed the shell to redirect the standard output from the ls command into the file lab6/homecontents.txt


= Also: =
= Also: =

Revision as of 18:45, 2 March 2024

Input and Output

You need to study system programming to fully understand how input and output works, but without being a programmer you can still learn most of what you need to control input and output on the command line.

The introduction in this lab will probably be a little dry, but hopefuly it will start making more sense when you get to apply these concepts later in the lab.

Each application on a POSIX-compatible system (such as Linux) is always started with three data pipes attached to it. These are:

  • Standard input (STDIN, file descriptor 0)
  • Standard output (STDOUT, file descriptor 1)
  • Standard error (STDERR, file descriptor 2)

The file descriptor numbers mostly won't matter to you in this course, but they're good to know for the future. You'll remember them if you just memorize STDIN, STDOUT, STDERR, and recall that the count starts from zero.

Default standard input/ouput

STDOUT

Standard output is the most straightforward to understand. Whatever normal output a command-line prints goes out through the standard output pipe.

By default the standard output pipe is connected to the terminal, and anything that goes into that pipe comes out as readable output on the screen.

STDERR

This is where things get complicated. The application can send output into STDOUT or STDERR. Both these pipes are connected to the terminal by default, and both outputs will look the same to the user. But they are different pipes - which means they are controlled separately.

One extra confusing factor is the definition of "error". Sometimes it's obvious what constitutes an error, but often it isn't.

The programmer who wrote the application had to make the decision which sort of output to send to STDOUT and which to send to STDERR. You can try and make an educated guess, but you'll have to confirm by experimenting.

STDIN

Standard input is only tricky until you become familiar with it. STDIN is also a data pipe, but by default instead of the pipe's input coming from the program and the output going to the terminal: the pipe's input comes from the terminal and the output goes into the program.

Up to this point in the course you haven't used STDIN, but it's used all the time and now is the time to learn about it.

Other pipes

A programmer can choose to open as many other pipes as they like in their application, but none of those extra pipes can be controlled using the mechanisms in this lab.

Redirecting input/output

The input/output pipes don't have to be connected to the default input/output (keyboard and terminal display). You can choose to connect one or two or all three of these pipes to something else. The something else can be:

  • A file, which is commonly called redirection
  • Another program, which is commonly called piping

Redirect STDOUT to a file

When the shell encounters the character > it will interpret that as an instruction to redirect the standard output from the application you're running into a file.

A simple example of when you might use this is to save a listing of all the files you have now, so that you can do something with that list later.

  • In a terminal create the directory ~/lab6 but keep your PWD in your home directory.
  • Then run ls twice. The first time on its own, the second time redirecting its output to a file:
    ls
    ls > lab6/homecontents.txt
    

Note that the second time there is no output on the terminal. That's because you instructed the shell to redirect the standard output from the ls command into the file lab6/homecontents.txt

Also:

  • New commands: grep, head, tail, wc
  • Redirect STDOUT to file
  • Redirect STDERR to file
  • Redirect STDERR to /dev/null
  • Many commands can read from STDIN. E.g.: cat, less, tar, grep, head, tail
  • The STDOUT from one command can be piped to the STDIN of another