BASH/C3/Basics-of-Redirection-(error-handling)/English
Title of script: Basics of redirection
Author: Lavitha Pereira
Keywords: Video tutorial, redirection, file descriptors, standard input, standard output, standard error
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on Basics of redirection |
Display Slide 2 | In this tutorial, we will learn
With the help of some examples |
Display Slide 3Prerequisites
|
To follow this tutorial you should have knowledge of Shell Scripting in BASH.
If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org)
|
Display Slide 4
System requirements |
For this tutorial I am using
Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
Display Slide 5
Input and Output |
* In GNU/Linux we can send output to a file or read input from a file
|
Display Slide 6
Redirection and file descriptors |
* Changing the default path of input or output is called as redirection
|
Display Slide 7
Redirection and file descriptors |
* 0, 1, 2 are POSIX numbers and also known as file descriptors (FD).
|
Display slide 8
Standard Input
|
* Standard input is the default input method
|
Display slide 9
Input Redirection
|
* The default standard input is the keyboard.
|
Let me open a file named redirection.sh | |
Highlight
#!/usr/bin/env bash |
I have typed some code in this file.
This is the shebang line. sort < file.txt ls > ls_file.txt rm /tmp/4815.txt 2> error.txt |
sort < file.txt | * Type sort space lesser than space file.txt This is an example of input redirection.
|
Switch to terminal | Let us run the file redirection.sh.
Go to terminal. |
Visual cue is missing | Before that let us see the content of file.txt
You can see that the file contains series of numbers. |
Let's close the file. | |
Type
chmod +x redirection.sh>>Press Enter dot slash redirection.sh
|
Now type:chmod space plus x space redirection dot sh
Press Enter. Type dot slash redirection.sh Press Enter. |
Output:
0 2 3 7 8 9
|
We can see the output on terminal after sorting.
The numbers are sorted in asscending order.
|
Display Slide 10
Standard Output
|
* Standard output is used by a command to display output.
|
Display slide 11
Output Redirection
|
|
Let me go back to the file redirection.sh | |
# sort < file.txt | Comment the previous line ie. sort |
ls > ls_file.txt | Below it type ls space greater than space ls_file.txt
This is an example of output redirection. |
Continue previous highlight | The output of 'ls' will be stored in ls_file.txt file.
|
Save the file and switch to terminal | |
ls | First let us type 'ls' and see the output. |
dot slash redirection.sh
|
Now press the uparrow key.
Recall the previous dot slash command. Press Enter. |
Now let us check whether the output is redirected correctly. | |
To open the file, type
gedit ls_file.txt |
Type gedit ls_file.txt and press Enter.
We can now see the output in this file. So our redirect was successful.
|
Display Slide 12
Standard Error
|
Standard error
|
Display slide 13
Error Redirection
|
* The default standard error output is visible on the screen or monitor.
|
Let me go back to the file redirection.sh | |
# ls > ls_files.txt | We will comment the previous statement.
rm /tmp/4815.txt 2> error.txt |
rm /tmp/4815.txt 2> error.txt | Now type rm space backslash tmp backslash 4815.txt space 2 greater than space error.txt.
Click on save and switch to the terminal. |
Type-
rm /tmp/4815.txt |
We will type the following command to see the error.
rm /tmp/4815.txt |
The error displayed will be
rm: cannot remove `/tmp/4815.txt': No such file or directory
| |
Now we will execute our previous dot slash command with error redirection. | |
dot slash redirection.sh
|
So, press the uparrow key.
Recall the previous command. Press Enter. |
Now let us check whether the error is redirected. | |
To open the file, type
gedit error.txt |
Type gedit error.txt and press Enter.
|
We can now see the error redirected to file error.txt. | |
This brings us to the end of this tutorial. | |
Display Slide 14
Summary |
Summary
Let us summarise. In this tutorial we learnt
|
Assignment | As an assignment,
Write a program in any language like C, C++, java. And redirect the output or error to a new file. OR Create a text file with some content like your name, address. Redirect the content to a new file. |
Display Slide 15
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below.
|
Display Slide 16
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 17
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 18 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |