BASH/C3/More-on-Redirection/English
Title of script: More on redirection
Author: Lavitha Pereira
Keywords: Video tutorial, Error and output redirection, Append output
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on More on 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 to practice this tutorial. |
In an earlier tutorial, we learned about standard output and standard errors. | |
Display Slide 5
Redirection of both stderr and stdout |
* Both stdout as well as the stderr can be redirected to the same file.
We will cover two of the most important methods of redirections in this tutorial. |
Display Slide 6
Redirection of both stderr and stdout Syntax: command &>filename
|
The first method to redirect both the standard output and error is by using
&> ampersand followed by greater-than sign
Command space ampersand greater than space filename |
Let me open a file named redirect.sh.
| |
Highlight
#!/usr/bin/env bash |
This is the shebang line. |
ls /usr /user &> out_file | ls lists the directory content of 2 directories namely '/usr' and '/user'
Hence the command 'ls' will throw an error.
|
Press Ctrl+S | Now Save the file. |
Switch to terminal | Let us run the file redirect.sh.
Open the terminal using CTRL+ALT+T keys simultaneously on your keyboard |
Type
chmod +x redirect.sh>>Press Enter dot slash redirect.sh
|
And type:chmod space plus x space redirect dot sh
Press Enter. Type dot slash redirect.sh Press Enter. |
Terminal
|
We can see the output by opening out_file.txt
|
Output
[Highlight] ls: cannot access /user: No such file or directory
arm-linux-gnueabi arm-linux-gnueabihf bin games i686-linux-gnu include lib local sbin share src |
We can see both the error and output.
The error for directory /user is recorded in this file. It says that there was no '/user' directory found.
|
Type rm out_file.txt | Now let us delete this file. So, on the terminal type
rm out_file.txt |
*
|
Come back to our slides. |
Display Slide 7
Redirection of both stderr and stdout Syntax:
|
Another method is to use
2>&1 2 greater than ampersand 1 after the filename
filename space 2 greater than ampersand 1We can also redirect to slash dev slash null (/dev/null) file. |
Let us learn a little more about slash dev slash null (/dev/null) file. | |
Display slide 8
/dev/null |
* It is a special kind of file
|
Let us now come back to our code in gedit. | |
Let us redirect both standard output and error to the null file. | |
ls /usr /user &> out_file.txt
|
I will copy this line of code and paste it below over here. |
And replace the out_file.txt with
/dev/null 2>&1
ls /usr /user >/dev/null 2>&1 |
I want both the output and error messages to be discarded.
Number “2” will redirect standard error to standard output, denoted by number “1”. |
Press Ctrl+S | Now click on Save. Save the code. |
Switch to terminal | Let us run the file redirect.sh.
Go to the terminal. |
dot slash redirect.sh
|
Recall the previous command with the up-arrow key.
and press Enter. |
Terminal | We can see the output by typing
|
Display Slide 9
Appen redirected output Synatx: command >> filename
|
Come back to our slides.
The syntax is:command space greater than greater than space followed by filename |
Let us understand this using an example.
| |
date >> out_file.txt | Now, here let's type
date space greater than greater than space out_file.txt |
Switch to the terminal and type:
date |
The 'date' command will simply display the system date as output.
Type: date You can see that the system date i.e. the current date is displayed. |
Highlight the date command code line | Now come back to our code file.
|
Click on Save
Switch to the terminal. | |
dot slash redirect.sh
|
Now press the uparrow key.
./redirect.sh and press Enter. |
cat out_file.txt | Let us check the output by opening out_file.txt
|
[Highligh]
Wed Jul 2 12:06:32 IST 2014 |
Observe that the output of 'date' command is appended to the end of the file. |
This brings us to the end of this tutorial. | |
Display Slide 14
Summary |
Summary
Let us summarise. In this tutorial we learnt
|
Display slide 15
Assignment |
As an assignment,
|
Display Slide 16
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 17
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 18
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 19 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |