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
|
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 for practice. |
Display Slide 5
Input and Output |
|
Display Slide 6
Redirection and file descriptors |
|
Display Slide 7
Redirection and file descriptors |
|
Display slide 8
Standard Input |
|
Display slide 9
Input Redirection
|
|
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 |
|
Switch to terminal | Let us run the file redirection.sh.
Open the terminal using Ctrl, Alt and T simultaneously on your keyboard. |
Visual cue is missing | Before that, let us see the content of file.txt
Type cat space file.txt. You can see that the file contains series of numbers. |
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 the terminal after sorting.
The numbers are sorted in asscending order. <<PAUSE>> |
Display Slide 10
Standard Output
|
Come back to our slides.
|
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 the terminal | |
ls | Let me clear the prompt. First let us type 'ls' and see the output. |
dot slash redirection.sh
|
Now press the uparrow key thrice.
Recall the previous command dot slash redirection.sh. 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
|
Come back to our slides.
|
Display slide 13
Error Redirection
|
|
Let me go back to the file redirection.sh | |
# ls > ls_files.txt | We will comment the previous line.
i.e.ls |
rm /tmp/4815.txt 2> error.txt | Below it type rm space backslash tmp backslash 4815.txt space 2 greater than symbol space error.txt.
Click on Save and switch to the terminal. |
Type-
rm /tmp/4815.txt |
We will type a command to see the error.
Type rm /tmp/4815.txt Press Enter. |
The error displayed is
rm: cannot remove `/tmp/4815.txt': No such file or directory
| |
Now we will execute our file. | |
dot slash redirection.sh
|
So, press the uparrow key.
Recall the previous command dot slash redirection.sh 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. |