BASH/C3/More-on-Redirection/English-timed

From Script | Spoken-Tutorial
Revision as of 17:18, 20 November 2014 by Pratik kamble (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Timee Narration
00:01 Dear friends, Welcome to the spoken tutorial on More on redirection
00:07 In this tutorial, we will learn Redirection of both standard error and output
00:13 Appending redirected output
00:15 With the help of some examples
00:19 To follow this tutorial you should have knowledge of Shell Scripting in BASH.
00:25 If not, for relevant tutorials please visit our website which is as shown http://www.spoken-tutorial.org
00:30 For this tutorial I am using Ubuntu Linux 12.04 Operating System
00:35 GNU BASH version 4.2
00:39 Please note, GNU Bash version 4 or above is recommended for practice.
00:46 In an earlier tutorial, we learned about standard output and standard errors.
00:52 Both stderr as well as the stdout can be redirected to the same file.
00:58 This can be done in multiple ways.
01:01 We will cover two of the most important methods of redirections in this tutorial.
01:08 The first method to redirect both the standard output and error is by using &>(ampersand) followed by greater-than sign
01:18 The syntax is Command space ampersand greater than space filename
01:25 Let me open a file named redirect.sh.
01:30 I have typed some code in this file.
01:32 This is the shebang line.
01:36 ls lists the directory content of 2 directories namely /usr and /user
01:44 Note that /user directory does not exist.
01:48 Hence the command ls will throw an error.
01:52 &(ampersand) followed by greater than will redirect stdout and stderr to out_(underscore)file.txt
02:03 Now Save the file.
02:05 Let us run the file redirect.sh.
02:07 Open the terminal using CTRL+ALT+T keys simultaneously on your keyboard
02:15 Type chmod space plus x space redirect dot sh
02:23 Press Enter
02:25 Type dot slash redirect dot sh
02:28 Press Enter.
02:30 We can see the output by opening out_(underscore)file.(dot)txt
02:36 Type cat space out_(underscore)file.(dot)txt
02:42 We can see both the error and output.
02:48 The error for directory /user is recorded in this file.
02:51 It says that there was no '/user' directory found.
02:56 The directory content for /usr is displayed.
03:00 Please note that the content for '/usr' directory may vary on your system.
03:06 Now let us delete this file. So, on the terminal Type rm space out_(underscore)file. (dot)txt
03:15 Another method is to use 2 greater than ampersand 1 after the filename
03:24 The syntax is command space greater than filename space 2 greater than ampersand 1
03:33 We can also redirect to slash dev slash null (/dev/null) file.
03:39 Let us learn a little more about slash dev slash null (/dev/null) file.
03:45 It is a special kind of file
03:48 It is a null file or a place where we can dump anything.
03:52 It includes the output and error messages
03:57 It is also called as bit bucket.
04:00 Let us now come back to our code in gedit.
04:04 Let us redirect both standard output and error to the null file.
04:11 I will copy this line of code and paste it below over here.
04:16 I want both the output and error messages to be discarded.
04:21 So I will change this part of the copied code.> (greater than) means truncate or write.
04:30 slash dev slash null is the null file 2>&1 (2 greater than ampersand 1)
04:37 Number “2” will redirect standard error to standard output, denoted by number “1”.
04:45 Now click on Save. Save the code.
04:48 Let us run the file redirect.sh.
04:52 Go to the terminal.
04:54 Recall the previous command with the up-arrow key. dot slash redirect.sh and press Enter.
05:03 We can see the output by typing cat out_(underscore)file.(dot)txt
05:11 Come back to our slides.
05:15 * We can capture as well as append standard output or error to a file.
05:21 The output or the error will be appended at the end of the file.
05:26 If the file does not exist, it will create a new file.
05:31 The syntax is command space greater than greater than space followed by filename
05:41 Let us understand this using an example.
05:45 Let me open the file redirect.(dot)sh
05:49 Now, here let's Type date space greater than greater than space out_(underscore)file.(dot)txt
06:00 The 'date' command will simply display the system date as output.
06:06 We can check this command on the terminal by typing 'date.'
06:11 Come back to the terminal.Type date You can see that the system date i.e. the current date is displayed.
06:23 The output of date command will be appended to the out_(underscore)file.(dot)txt file.
06:31 We are using this file to capture standard output and error of ls command.
06:39 Click on Save
06:40 Switch to the terminal.
06:43 Now press the uparrow key.Recall the previous command dot slash redirect dot sh
06:50 and press Enter.
06:52 Let us check the output by opening out_(underscore) file.(dot)txt
06:59 Type cat space out_(underscore)file.(dot)txt
07:05 Observe that the output of 'date' command is appended to the end of the file.
07:12 This brings us to the end of this tutorial.
07:15 Let us summarise.
07:17 In this tutorial we learnt
07:19 Redirection of both standard error and output And to append the redirected output
07:27 As an assignment,
07:29 Create X_(underscore)file.(dot)txt file with some content.
07:34 Redirect the content of both out_(underscore)file.(dot)txt and X_(underscore)file.(dot)txt to a new file.
07:44 Watch the video available at the link shown below.
07:47 It summarises the Spoken Tutorial project.
07:51 If you do not have good bandwidth, you can download and watch it.
07:56 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test
08:06 For more details, please write to contact@spoken-tutorial.org
08:13 Spoken Tutorial Project is a part of the Talk to a Teacher project.
08:17 It is supported by the National Mission on Education through ICT, MHRD, Government of India.

More information on this Mission is available at http://spoken-tutorial.org/NMEICT-Intro

08:30 The script has been contributed by FOSSEE and Spoken-Tutorial teams.
08:37 This is Ashwini from IIT Bombay.Thank you for joining.

Contributors and Content Editors

Pratik kamble, Sandhya.np14