Added some examples for redirection

This commit is contained in:
Benjamyn Love 2019-02-26 00:06:04 +11:00
parent 3b7fc94f08
commit 923250a570
2 changed files with 72 additions and 9 deletions

View File

@ -17,28 +17,44 @@ STREAM ID STREAM NAME STREAM USE
2 STDERR Used to receive errors from the program
```
### Standard Input
#### Standard Input
Standard input is how we send input to an application it is also how we interact with the command line shell (Usually [`bash`](/Linux/bash.md))
Standard input is how we send input to an program it is also how we interact with the command line shell (Usually [`bash`](/Linux/bash.md))
Whenever a linux program asks for input this is sent to the program using the STDIN stream
The STDIN stream can also be refrenced by the '-' char in a command line application `cat -` will print what comes into STDIN (The terminal)
The STDIN stream can also be refrenced by the '-' char in a command line program `cat -` will print what comes into STDIN (The terminal)
One thing that makes the linux command line so powerfull is the suite of commands that is provided all work using STDIN, STDOUT and STDERR you can pipe information from one stream to another (i.e the STDOUT from one program can be in STDIN to another)
#### Input Redirection
### Input Redirection
You can use redirection to send information to a program using the '<' symbol i.e `cat < file` this will push the contents of 'file' to cat using STDIN
### Standard Output
#### Standard Output
Standard output is how we get the output from a program, usually this is printed directly to the terminal
#### Output Redirection
### Output Redirection
You can redirect the STDOUT of a application is a few ways you can use the '>' symbol to push the information from the programs output and saves it in a file for example `ls > filelist` will take the output of ls and saves it in the filelist file
You can redirect the STDOUT of a program is a few ways you can use the '>' symbol to push the information from the programs output and saves it in a file for example `ls > filelist` will take the output of ls and saves it in the filelist file
You can also redirect the STDOUT of one program to another programs STDIN using the '|' symbol doing this you can combine muliple programs in different ways
You can also redirect the STDOUT of one program to another programs STDIN using the '|' symbol, this is called a pipe doing this you can combine muliple programs in different ways
For example you can use `ls -alh | grep filename` this will list the files in the current directory and then passes the STDOUT to the STDIN for grep, this causes grep to use the output of ls as the input data to work with so the output of ls is searched for the string 'filename'
For example you can use `ls -alh | grep filename` this will list the files in the current directory and then passes the STDOUT to the STDIN for grep, this causes grep to use the output of ls as the input data to work with so the output of ls is searched for the string 'filename'
#### Standard Error
Standard error is how error messages are sent between programs or displayed to the user
As this is a different to STDOUT you will still get output to the terminal if you redirect using the '>' symbol
### Error Redirection
Errors can be redirected using the following '2>' this specifies the stream to redirect and the direction
For example you can redirect errors of an program to an error log file using '2> error.log' so a command that uses this would look like `find / -name "filename" 2> errors.log` this would make an error.log in the directory you are in and save the errors in a log and show the results from STDOUT on the screen (In this example the errors that you would see are `Permission Denied` errors)
#### More redirection tips
You can do quite a bit with piping and redirection, using these basics you will be able to do quite a bit in the bash shell I have created some examples both to show how programs can work together and provide some real world examples on what it can be used for [here](/examples/redirection)

View File

@ -0,0 +1,47 @@
# Redirection Examples
This page has a few examples of some things that can be done using redirection and pipes
## Pipes
Pipes are usefull when you need to do something with an programs output
In the following command output we may be looking for a file that is starting with 'pas'
```bash
[root@gunicorn paste]# ls -alh
total 16K
drwxr-xr-x. 6 root nginx 158 Feb 20 06:38 .
drwxr-xr-x. 4 root nginx 30 Feb 19 09:32 ..
drwxr-xr-x. 8 root nginx 163 Feb 19 09:29 .git
-rw-r--r--. 1 root nginx 20 Feb 19 09:29 .gitignore
-rwxr-xr-x. 1 root nginx 2.4K Feb 19 09:36 paste.py
drwxr-xr-x. 2 root nginx 6 Feb 19 09:33 pastes
drwxr-xr-x. 2 root nginx 61 Feb 19 09:36 __pycache__
-rw-r--r--. 1 root nginx 0 Feb 19 09:29 README.md
-rw-r--r--. 1 root nginx 6 Feb 19 09:29 requirements.txt
drwxr-xr-x. 2 root nginx 40 Feb 20 05:23 templates
-rw-r--r--. 1 root nginx 77 Feb 19 09:36 wsgi.py
```
We could just look through the output but that can take some time with larger file lists, in this case we can use [`grep`](/Commands/commands#grep)
```bash
[root@gunicorn paste]# ls -alh | grep pas
-rwxr-xr-x. 1 root nginx 2.4K Feb 19 09:36 paste.py
drwxr-xr-x. 2 root nginx 6 Feb 19 09:33 pastes
```
As you can see this has filtered the output of the command and is now just showing the output that matches the 'pas' string provided to grep, something to keep in mind is by default grep matches the entire line
Another example of piping is the following command, this will find the message ID of all frozen emails on a mail server running exim and then pass the ID one at a time to `exim -Mrm` which removes the message from the queue.
```bash
exim -iz | xargs exim -Mrm
```
## Redirection
There will be quite a few times when you will want to redirect the output of a command into a file, this could be a temporary file with some data in it that you need to work with
The Easiest way