Difference between revisions of "BASH/C3/More-on-Redirection/English"
(Created page with ''''''' '''Title of script: More on redirection''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, Error and output redirection, Append output''' {| style="bord…') |
|||
Line 236: | Line 236: | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * It is a special kind of file | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * It is a special kind of file | ||
* It is a null file or a place where we can dump anything. | * It is a null file or a place where we can dump anything. | ||
− | * It includes the output and error messages ' | + | * It includes the output and error messages '''<br/> It depends upon the user, if he wants to discard some error messages as well as useful log messages he can 'dump' to /dev/null. ''<br/> ''Yes, we can dump anything(also the entire disk partition). Please see appended text.''we want to discard |
* It is also called as '''bit bucket.''' | * It is also called as '''bit bucket.''' | ||
Line 343: | Line 343: | ||
'''date''' | '''date''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The ''''date'''' command will simply display the Is this better than “correct date”? | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The ''''date'''' command will simply display the Is this better than “correct date”? |
I m sorry, my bad. It was a typo. The word was “current” and not “correct” | I m sorry, my bad. It was a typo. The word was “current” and not “correct” | ||
− | <nowiki>;)</nowiki> | + | <nowiki>;)</nowiki> |
− | + | ||
So which word is more appropriate - | So which word is more appropriate - | ||
− | “system date” or “currnet date”? | + | “system date” or “currnet date”? |
− | + | ||
“System date” is OK'''system date '''as output. | “System date” is OK'''system date '''as output. | ||
Revision as of 18:10, 5 August 2014
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 | Save the file. |
Switch to terminal | Let us run the file redirect.sh.
Go to the terminal. |
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 | 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 Is this better than “correct date”?
I m sorry, my bad. It was a typo. The word was “current” and not “correct” ;) So which word is more appropriate - “system date” or “currnet date”? “System date” is OKsystem date as output.
|
Highlight the date command code line | Now come back to our code file.
|
Switch to the terminal. | |
dot slash redirect.sh
|
Now press the uparrow key.
|
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,# Create 'X_file.txt' file with some content.
|
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. |