BASH/C3/Basics-of-Redirection-(error-handling)/English

From Script | Spoken-Tutorial
Revision as of 16:56, 5 August 2014 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

­Title of script: Basics of redirection

Author: Lavitha Pereira

Keywords: Video tutorial, redirection, file descriptors, standard input, standard output, standard error


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on Basics of redirection
Display Slide 2 In this tutorial, we will learn
  • Input and output in Bash
  • Redirection and file descriptors
  • Standard input
  • Standard output
  • Standard error

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.

Display Slide 5

Input and Output

* In GNU/Linux we can send output to a file or read input from a file
  • Each Shell command has its own inputs and outputs
  • Input and output is redirected using a special notation interpreted by the Shell


Display Slide 6

Redirection and file descriptors

* Changing the default path of input or output is called as redirection
  • In GNU/Linux everything is a file, including the hardware
  • Common return values are:
    • 0 for Input i.e. Keyboard
    • 1 for Output i.e. Screen
    • 2 for Error i.e. Screen


Display Slide 7

Redirection and file descriptors

* 0, 1, 2 are POSIX numbers and also known as file descriptors (FD).
  1. A redirector uses POSIX numbers to talk with a user or other program.


Display slide 8

Standard Input


* Standard input is the default input method
  • It is used by all commands to read input.
  • It is denoted by zero (0).
  • Also known as stdin(Standard input).



Display slide 9

Input Redirection


command < filename

* The default standard input is the keyboard.
  • (<) Lesser than is input redirection symbol
  • The syntax is Command space lesser symbol space filename


Let me open a file named redirection.sh
Highlight

#!/usr/bin/env bash

I have typed some code in this file.

This is the shebang line.

sort < file.txt

ls > ls_file.txt

rm /tmp/4815.txt 2> error.txt

sort < file.txt * Type sort space lesser than space file.txt
This is an example of input redirection.
  • The input is taken from file
    file.txt
  • sort command does the sorting of numbers present in file.txt


Switch to terminal Let us run the file redirection.sh.

Go to terminal.

Visual cue is missing Before that let us see the content of file.txt

You can see that the file contains series of numbers.

Let's close the file.
Type

chmod +x redirection.sh>>Press Enter

dot slash redirection.sh


Now type:chmod space plus x space redirection dot sh

Press Enter.

Type dot slash redirection.sh

Press Enter.

Output:

0

2

3

7

8

9


We can see the output on terminal after sorting.

The numbers are sorted in asscending order.


<<PAUSE>>

Display Slide 10

Standard Output


* Standard output is used by a command to display output.
  • The default output is displayed on the screen.
  • It is denoted by one number (1).
  • Also known as stdout(Standard output).


Display slide 11

Output Redirection


command > filename

  • ( > )Greater than is output redirection symbol
  • Syntax is Command space greater symbol space filename


Let me go back to the file redirection.sh
# sort < file.txt Comment the previous line ie. sort
ls > ls_file.txt Below it type ls space greater than space ls_file.txt

This is an example of output redirection.

Continue previous highlight The output of 'ls' will be stored in ls_file.txt file.


ls command lists the information about files in that particular directory.

Save the file and switch to terminal
ls First let us type 'ls' and see the output.
dot slash redirection.sh


Now press the uparrow key.

Recall the previous dot slash command.

Press Enter.

Now let us check whether the output is redirected correctly.
To open the file, type

gedit ls_file.txt

Type gedit ls_file.txt and press Enter.

We can now see the output in this file. So our redirect was successful.


<<PAUSE>>

Display Slide 12

Standard Error


Standard error
  • is the default output error
  • which is used to write all system errors
  • It is denoted by two number (2).
  • It is also known as stderr(Standard error).


Display slide 13

Error Redirection


command 2> errors.txt

* The default standard error output is visible on the screen or monitor.
  • Two greater than(2>) is error redirection symbol
  • command space 2 greater than space error.txt


Let me go back to the file redirection.sh
# ls > ls_files.txt We will comment the previous statement.

rm /tmp/4815.txt 2> error.txt

rm /tmp/4815.txt 2> error.txt Now type rm space backslash tmp backslash 4815.txt space 2 greater than space error.txt.


The error output is redirected to error.txt file.

Click on save and switch to the terminal.

Type-

rm /tmp/4815.txt

We will type the following command to see the error.

rm /tmp/4815.txt

The error displayed will be

rm: cannot remove `/tmp/4815.txt': No such file or directory


as the file is not present which we are trying to delete.

Now we will execute our previous dot slash command with error redirection.
dot slash redirection.sh


So, press the uparrow key.

Recall the previous command.

Press Enter.

Now let us check whether the error is redirected.
To open the file, type

gedit error.txt

Type gedit error.txt and press Enter.



We can now see the error redirected to file error.txt.
This brings us to the end of this tutorial.
Display Slide 14

Summary

Summary

Let us summarise.

In this tutorial we learnt

  • Input and output in bash
  • Redirection and file descriptors
  • Standard input using <(less than) symbol
  • Standard output >(greater than) symbol
  • Standard error using 2> (2 greater than symbol)
  • With the help of some examples


Assignment As an assignment,

Write a program in any language like C, C++, java.

And redirect the output or error to a new file.

OR

Create a text file with some content like your name, address.

Redirect the content to a new file.

Display Slide 15

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 16

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 17

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 18 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