Next revision | Previous revision |
ops102:redirection [2024/01/24 04:18] – created chris | ops102:redirection [2024/05/04 02:26] (current) – external edit 127.0.0.1 |
---|
===== Standard File Descriptors ===== | ===== Standard File Descriptors ===== |
| |
On Linux, other Unix-like systems, and on Windows, programs may open **file descriptors** (Linux terminology) or **handles** (Windows terminology). Each file descriptor/handle is a numbered channel connected to a file or device. | On Linux, other Unix-like systems, and on Windows, programs may open **file descriptors** (Linux terminology) or **file handles** (Windows terminology). Each file descriptor/handle is a numbered channel connected to a file or device. |
| |
By default, three channels are opened automatically by the shell when a process is started. These are: | By default, three channels are opened automatically by the shell when a process is started. These are: |
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 one | -rw-r--r--. 1 chris chris 0 Sep 26 13:17 one |
-rw-r--r--. 1 chris chris 0 Sep 26 13:17 two</code> | -rw-r--r--. 1 chris chris 0 Sep 26 13:17 two</code> |
| |
| Note that in this example, it is necessary to redirect stdout before redirecting stderr to the same location. |
| |
===== Piping ===== | ===== Piping ===== |
| |
Piping is a special case of redirection, where the output of one command is connected to the input of another command. This is set up using the vertical-bar (pipe) symbol: ''|'' (this may look like a solid or a dashed vertical line, depending on the terminal font in use). | Piping is a special case of redirection, where the output (stdout) of one command is connected to the input (stdin) of another command. This is set up using the vertical-bar (pipe) symbol: ''|'' (this may look like a solid or a dashed vertical line, depending on the terminal font in use). |
| |
For example, on Windows, the output of the ''help'' command is more than one screen long. You could pipe the output of the ''help'' command into the input of the ''more'' command to view one screen of text at a time: | For example, on Windows, the output of the ''help'' command is more than one screen long. You could pipe the output of the ''help'' command into the input of the ''more'' command to view one screen of text at a time: |
-- More --</code> | -- More --</code> |
| |
Press the ENTER key to scroll by one line, or the spacebar to scroll by one screen. | Press the ENTER key to scroll by one line, the spacebar to scroll by one screen, or "q" to quit. |
| |
Similarly, on Linux, a long directory listing could be redirected to ''more'', or better yet, the improved ''less'' command: | Similarly, on Linux, a long directory listing could be redirected to ''more'', or better yet, the improved ''less'' command: |
drwxr-xr-x. 2 root root 4096 Aug 24 20:00 bluetooth | drwxr-xr-x. 2 root root 4096 Aug 24 20:00 bluetooth |
drwxr-xr-x. 2 root root 4096 May 30 09:32 bonobo-activation | drwxr-xr-x. 2 root root 4096 May 30 09:32 bonobo-activation |
></code> | :</code> |
| |
Like ''more'', you can press ENTER to scroll by one line or SPACE to scroll by one screen; but you can also use the up/down arrow keys, or the PgUp/PgDn keys, to scroll in either direction by one line or one screen. | Like ''more'', you can press ENTER to scroll by one line or SPACE to scroll by one screen, or "q" to quit; but you can also use the up/down arrow keys, or the PgUp/PgDn keys, to scroll in either direction by one line or one screen. |
| |
On Windows, to see the ''help'' lines that mention color, you could feed the output of the ''help'' command into the ''find'' command: | On Windows, to see the ''help'' lines that mention color, you could feed the output of the ''help'' command into the ''find'' command: |
rw-r--r--</code> | rw-r--r--</code> |
| |
If we wanted to see the filenames displayed alongside the permissions, we could take the output of ''ls -l'', sliminate the first line (with ''tail'' as above), squeeze out multiple consecutive spaces so that they become a single space using ''tr -s " "'', then use ''cut -d " " -f 1,9'' to separate each line into fields delimited by the space (" ") character, selecting just fields 1 and 9: | If we wanted to see the filenames displayed alongside the permissions, we could take the output of ''ls -l'', eliminate the first line (with ''tail'' as above), squeeze out multiple consecutive spaces so that they become a single space using ''tr -s " "'', then use ''cut -d " " -f 1,9'' to separate each line into fields delimited by the space (" ") character, selecting just fields 1 and 9: |
| |
<code> | <code> |