BASH/C3/More-on-Redirection/English

From Script | Spoken-Tutorial
Jump to: navigation, search

­

Title of script: More on redirection

Author: Lavitha Pereira

Keywords: Video tutorial, Error and output redirection, Append output


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on More on redirection
Display Slide 2 In this tutorial, we will learn
  • Redirection of both standard error and output
  • Appending redirected output
  • With the help of some examples


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
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.2

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.
  • This can be done in multiple methods.

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


The syntax is

Command space ampersand greater than space filename

Let me open a file named redirect.sh.


I have typed some code in this file.

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'


Note that '/user' directory does not exist.

Hence the command 'ls' will throw an error.


And ampersand followed by greater than will redirect stdout and stderr to out_file.txt

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


cat out_file.txt

We can see the output by opening out_file.txt


Type cat space out_file.txt

Output

[Highlight]

ls: cannot access /user: No such file or directory


/usr:

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.


The directory content for /usr is displayed.


Please note that the content for '/usr' directory may vary on your system.



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:


command > filename 2>&1


command >/dev/null 2>&1

Another method is to use

2>&1

2 greater than ampersand 1 after the filename


The syntax iscommand space greater than

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
  • It is a null file or a place where we can dump anything.
  • It includes the output and error messages
  • It is also called as bit bucket.


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


So, we have

ls /usr /user >/dev/null 2>&1

I want both the output and error messages to be discarded.


So I will change this part of the copied code.> (greater than) means truncate or write.


/dev/null is the null file


2>&1 (2 greater than ampersand 1)

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.


dot slash redirect.sh

and press Enter.

Terminal We can see the output by typing


cat out_file.txt

Display Slide 9

Appen redirected output

Synatx:

command >> filename


Come back to our slides.
  • We can capture as well as append standard output or error to a file.
  • The output or the error will be appended at the end of the file.
  • If the file does not exist, it will create a new file.

The syntax is:command space greater than greater than space followed by filename

Let us understand this using an example.


Let me open the file redirect.sh

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.


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.

Highlight the date command code line Now come back to our code file.


The output of date command will be appended to the out_file.txt file.


We are using this file to capture standard output and error of 'ls' command.


Click on Save

Switch to the terminal.

dot slash redirect.sh


Now press the uparrow key.


Recall the previous command

./redirect.sh

and press Enter.

cat out_file.txt Let us check the output by opening out_file.txt


Type cat space out underscore 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

  • Redirection of both standard error and output
  • And to append the redirected output


Display slide 15

Assignment

As an assignment,
  1. Create 'X_file.txt' file with some content.
  2. Redirect the content of both 'out_file.txt' and 'X_file.txt' to a new file.


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.


It summarises the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Display Slide 17

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

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.


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

Display Slide 19 The script has been contributed by FOSSEE and Spoken-Tutorial teams.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey