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…') |
Nancyvarkey (Talk | contribs) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 109: | Line 109: | ||
|- | |- | ||
| 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;"| Press '''Ctrl+S''' | | 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;"| Press '''Ctrl+S''' | ||
− | | 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;"| Save the 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;"| Now Save the file. |
|- | |- | ||
Line 115: | Line 115: | ||
| 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;"| Let us run the file''' redirect.sh.''' | | 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;"| Let us run the file''' redirect.sh.''' | ||
− | + | Open the terminal using '''CTRL+ALT+T keys''' | |
+ | |||
+ | simultaneously on your keyboard | ||
|- | |- | ||
Line 236: | Line 238: | ||
| 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 |
* It is also called as '''bit bucket.''' | * It is also called as '''bit bucket.''' | ||
Line 280: | Line 282: | ||
|- | |- | ||
| 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;"| Press '''Ctrl+S''' | | 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;"| Press '''Ctrl+S''' | ||
− | | 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;"| Save the code. | + | | 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;"| Now click on Save. Save the code. |
|- | |- | ||
Line 315: | Line 317: | ||
'''command >> filename''' | '''command >> filename''' | ||
− | |||
Line 343: | Line 344: | ||
'''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 | + | | 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 '''system date '''as output. |
− | |||
− | + | We can check this command on the '''terminal''' by typing ''''date'.''' | |
− | |||
− | + | Come back to the terminal. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Type: '''date''' | ||
You can see that the '''system date '''i.e. the '''current date '''is displayed. | You can see that the '''system date '''i.e. the '''current date '''is displayed. | ||
Line 370: | Line 365: | ||
We are using this file to capture''' '''standard output and error of ''''ls' '''command. | We are using this file to capture''' '''standard output and error of ''''ls' '''command. | ||
+ | |||
|- | |- | ||
| 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;"| | | 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;"| | ||
− | | 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;"| Switch to the '''terminal.''' | + | | 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;"| Click on Save |
+ | |||
+ | Switch to the '''terminal.''' | ||
|- | |- | ||
Line 383: | Line 381: | ||
− | Recall the previous command and press '''Enter.''' | + | Recall the previous command |
+ | |||
+ | '''./redirect.sh''' | ||
+ | |||
+ | and press '''Enter.''' | ||
|- | |- | ||
Line 414: | Line 416: | ||
* Redirection of both standard error and output | * Redirection of both standard error and output | ||
* And to append the redirected output''' ''' | * And to append the redirected output''' ''' | ||
− | |||
Line 421: | Line 422: | ||
'''Assignment''' | '''Assignment''' | ||
− | | 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;"| As an assignment,# Create 'X_file.txt' file with some content. | + | | 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;"| As an assignment, |
+ | # Create 'X_file.txt' file with some content. | ||
# Redirect the content of both 'out_file.txt' and 'X_file.txt' to a new file. | # Redirect the content of both 'out_file.txt' and 'X_file.txt' to a new file. | ||
− | |||
Latest revision as of 17:48, 8 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 | 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. |