- Standard Input and Output
- Redirecting Output to File
- Redirecting Output to Program
- Redirecting All Output
- Combining Outputs
- Redirecting to Multiple Target
- Redirecting STDIN from a file
- Sending Multiple Lines to STDIN
- For Loop - Shell Scripting
Standard Input and Output
- Linux provides three I/O channels to the programs.
- Standard Input (STDIN), File Descriptor Number 0, By Default Keyboard
- Standard Output (STDOUT), File Descriptor Number 1, By Default Screen or Terminal Window
- Standard Error (STDERR), File Descriptor Number 2, By Default Screen or Terminal Window
- > Redirect STDOUT to File
- 2> Redirect STDERR to File
- &> Redirect All Output (STDOUT and STDERR)
NOTE!: File contents are overwritten by default. Use >> to appends.
Redirecting Output to File
- STDOUT and STDERR can be redirected to files.
* command < file Send file as a Input to the command. * command > file Redirect STDOUT of command to file. * command >> file Append STDOUT of command to file. * command 2> file Redirect STDERR of command to file. * command 2>> file Append STDERR of command to file.
Run the following command as a non-root user
Note what happens when the same command is run but STDOUT is redirected to a file
NOTE!: The STDOUT and STDERR are distinct, Redirecting one does not affect the other.
The following command would redirect STDERR to a file
- The above command only show the STDOUT of the command.
- If you really do not care about the errors then why you waste a file (find.err)?
- There is a special file on your system that is very useful in this sort of situation,
/dev/nullis a black hole for data. Anything sent to is simply ignored.
Display only STDOUT
Store STDOUT but ignore STDERR
Guess what happen when run the following command
Redirecting Output to Program
- Linux and UNIX provides many small utilities that perform one task very well.
- A core design feature of Linux and UNIX is that the output of one command can be fed directly as a input for another command.
- Pipes can connect the commands.
NOTE!: STDERR is not forwarded across the pipes.
In the above example, All the lower case letters are converted to upper case letters
less - View input one page at a time
mail - Send input via email
lpr - Send input to a printer
Redirecting All Output
- Some operators affect both STDOUT and STDERR
&>Redirect All Output (STDOUT and STDERR)
Redirecting I/O Channels to Each Other
- Run the following command as a non-root user:
- You will find that while STDOUT is display through less, STDERR is not.
- This is because a pipe only redirect STDOUT.
- If you wanted to send all output to less you would needed to redirect STDERR to STDOUT first.
- You can redirect one I/O channel to another using
>and the channel’s file descriptor numbers.
- The above command simply redirect all STDERR to a file called 1 rather than to file descriptor 1 (STDOUT).
- To tell your shell that you are referring to a file descriptor, prepend the
- Suppose you wanted to run two command back to back and send their output through the pipe.
The output of these commands, you would find that only the calendar for 2015 was printed, while the calendar for 2014 went to the screen
This can be overcome by running the cal command in a subshell.
Redirecting to Multiple Target
- Stores STDOUT of command1 in filename, then pipes to command2
- Troubleshooting complex pipelines.
- Simultaneous viewing and logging of output
Redirecting STDIN from a file
- Redirect STDIN with
Sending Multiple Lines to STDIN
- Redirect Multiple line from keyboard to STDIN with <<WORD
- All text untill WORD is sent to STDIN.
- Sometimes called heretext
For Loop - Shell Scripting
- Performs actions on each member of a set of values.