Difference between revisions of "BASH/C3/Basics-of-Redirection-(error-handling)/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''­Title of script: Basics of redirection''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, redirection, file descriptors, standard input, standard output, standa…')
 
 
Line 12: Line 12:
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 1
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 1
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Dear friends, Welcome to the spoken tutorial on''' Basics of redirection'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Dear friends, welcome to the spoken tutorial on''' Basics of redirection'''
  
 
|-
 
|-
Line 23: Line 23:
 
* Standard output
 
* Standard output
 
* Standard error
 
* Standard error
 +
  
 
With the help of some examples  
 
With the help of some examples  
Line 34: Line 35:
  
 
If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)'''
 
If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)'''
 
 
 
  
 
|-
 
|-
Line 47: Line 45:
 
* '''GNU BASH''' version 4.2
 
* '''GNU BASH''' version 4.2
  
Please note,''' GNU Bash''' '''version 4''' or above is recommended to practice this tutorial.
+
Please note,''' GNU Bash''' '''version 4''' or above is recommended for practice.
  
 
|-
 
|-
Line 53: Line 51:
  
 
'''Input and Output'''
 
'''Input and Output'''
| 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;"| * In '''GNU/Linux '''we can''' '''send output to a file or read input from a 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;"|  
 
+
* 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
 
* Each '''Shell''' command has its own inputs and outputs
 
* Input and output is redirected using a special notation interpreted by the '''Shell'''
 
* Input and output is redirected using a special notation interpreted by the '''Shell'''
 
  
  
Line 64: Line 61:
  
 
'''Redirection and file descriptors'''
 
'''Redirection and file descriptors'''
| 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;"| * Changing the default path of input or output is called''' '''as''' redirection'''
+
| 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;"|  
 
+
* Changing the default path of input or output is called as''' redirection'''
 
* In GNU/Linux everything is a file, including the hardware
 
* In GNU/Linux everything is a file, including the hardware
 
* Common return values are:
 
* Common return values are:
Line 71: Line 68:
 
** '''1''' for '''Output''' i.e. '''Screen '''
 
** '''1''' for '''Output''' i.e. '''Screen '''
 
** '''2 '''for '''Error '''i.e. '''Screen '''
 
** '''2 '''for '''Error '''i.e. '''Screen '''
 
 
  
 
|-
 
|-
Line 78: Line 73:
  
 
'''Redirection and file descriptors'''
 
'''Redirection and file descriptors'''
| 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;"| * '''0, 1, 2 '''are '''POSIX '''numbers and also known as '''file descriptors''' '''(FD''').  
+
| 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;"|  
 
+
* '''0, 1, 2 '''are '''POSIX '''numbers and also known as '''file descriptors''' '''(FD''').  
# A redirector uses POSIX numbers to talk with a user or other program.  
+
* A redirector uses POSIX numbers to talk with a user or other program.  
 
+
 
+
  
 
|-
 
|-
Line 89: Line 82:
 
'''Standard Input'''
 
'''Standard Input'''
  
 
+
| 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;"|  
 
+
* Standard input is the default input method
| 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;"| * Standard input is the default input method
+
 
* It is used by all commands to read input.  
 
* It is used by all commands to read input.  
 
 
* It is denoted by zero (0).  
 
* It is denoted by zero (0).  
* Also known as '''stdin(Standard input). '''<br/>
+
* Also known as '''stdinb(Standard input). '''
 
+
 
+
  
  
Line 107: Line 96:
  
 
<nowiki>command < filename</nowiki>
 
<nowiki>command < filename</nowiki>
| 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 default standard input is the keyboard.  
+
| 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;"|  
* ('''<nowiki><</nowiki>''') '''Lesser than '''is input [http://bash.cyberciti.biz/guide/Redirection_symbol redirection symbol]
+
* The default standard input is the keyboard.  
 +
* ('''<nowiki><</nowiki>''') '''Less than '''symbol is input redirection symbol
 
* The syntax is '''Command space lesser symbol space filename'''
 
* The syntax is '''Command space lesser symbol space filename'''
 
  
  
Line 125: Line 114:
 
This is the''' shebang line.'''
 
This is the''' shebang line.'''
  
'''<nowiki>sort < file.txt </nowiki>'''
 
 
'''ls > ls_file.txt '''
 
 
'''rm /tmp/4815.txt 2> error.txt '''
 
  
 
|-
 
|-
 
| 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;"| '''<nowiki>sort < file.txt</nowiki>'''
 
| 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;"| '''<nowiki>sort < file.txt</nowiki>'''
| 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;"| * Type''' sort s'''pace '''lesser than''' space '''file.txt'''<br/> This is an example of input redirection.
+
| 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 input is taken from file <br/> '''file.txt'''
+
* Type''' sort space less than symbol space file.txt'''<br/> This is an example of input redirection.
* '''sort c'''ommand does the '''sorting '''of numbers''' '''present in''' file.txt'''
+
* The input is taken from file '''file.txt'''
 
+
* '''sort '''command does the '''sorting '''of numbers present in''' file.txt'''
 
+
  
 
|-
 
|-
Line 143: Line 126:
 
| 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''' redirection.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''' redirection.sh.'''  
  
Go to '''terminal.'''
+
Open the '''terminal''' using '''Ctrl, Alt''' and '''T''' simultaneously on your keyboard.
  
 
|-
 
|-
 
| 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;"| Visual cue is missing
 
| 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;"| Visual cue is missing
| 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;"| Before that let us see the content of''' file.txt'''
+
| 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;"| Before that, let us see the content of''' file.txt'''
 +
 
 +
Type '''cat space file.txt'''.
  
 
You can see that the file contains series of numbers.
 
You can see that the file contains series of numbers.
  
 
|-
 
|-
| 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;"| Type
| 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's close 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;"| Type''' '''
+
  
 
'''chmod +x redirection.sh>>'''Press '''Enter'''
 
'''chmod +x redirection.sh>>'''Press '''Enter'''
  
 
'''dot slash redirection.sh'''
 
'''dot slash redirection.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;"| Now type:'''chmod space plus x space redirection dot 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;"| Now type:'''chmod space plus x space redirection dot sh'''
Line 187: Line 166:
 
9
 
9
  
 
+
| 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;"| We can see the output on the '''terminal''' after sorting.
 
+
| 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;"| We can see the output on '''terminal''' after sorting.
+
  
 
The numbers are sorted in asscending order.
 
The numbers are sorted in asscending order.
 
  
 
<nowiki><<PAUSE>></nowiki>
 
<nowiki><<PAUSE>></nowiki>
Line 203: Line 179:
  
  
| 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;"| * Standard output is used by a command to display output.  
+
| 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;"| Come back to our slides.
  
 +
* Standard output is used by all commands to display output.
 
* The default output is displayed on the screen.  
 
* The default output is displayed on the screen.  
* It is denoted by '''one''' number '''(1)'''.
+
* It is denoted by ''' number one'''(1).
* Also known as '''stdout(Standard output). '''
+
* Also known as '''stdout (Standard output). '''
 
+
  
  
Line 219: Line 195:
 
'''command > filename'''
 
'''command > filename'''
 
| 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;"|  
* ( > )'''Greater than''' is output redirection symbol  
+
* ( > )'''Greater than''' symbol is the output redirection symbol  
* Syntax is '''Command''' space''' greater symbol''' space '''filename'''
+
* Syntax is '''Command space greater symbol space filename'''
 
+
 
+
  
 
|-
 
|-
Line 234: Line 208:
 
|-
 
|-
 
| 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;"| '''ls > ls_file.txt'''
 
| 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;"| '''ls > ls_file.txt'''
| 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;"| Below it type''' ls space greater than space ls_file.txt'''
+
| 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;"| Below it, type''' ls space greater than space ls_file.txt'''
  
 
This is an example of output redirection.
 
This is an example of output redirection.
Line 240: Line 214:
 
|-
 
|-
 
| 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;"| Continue previous highlight
 
| 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;"| Continue previous highlight
| 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 output of ''''ls'''' will be stored in '''ls_file.txt '''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;"| The output of ''''ls'''' will be stored in '''ls_file.txt '''file'.
  
  
Line 247: Line 221:
 
|-
 
|-
 
| 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;"| Save the file and switch to '''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;"| Save the file and 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;"| ls
 
| 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;"| ls
| 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;"| First let us type ''''ls'''' and see the output.
+
| 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 me clear the prompt.  First let us type ''''ls'''' and see the output.
  
 
|-
 
|-
Line 258: Line 232:
  
  
| 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 press the '''uparrow''' key.
+
| 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 press the '''uparrow''' key thrice.
  
Recall the previous '''dot slash '''command.
+
Recall the previous command '''dot slash redirection.sh'''.
  
 
Press '''Enter.'''
 
Press '''Enter.'''
Line 286: Line 260:
  
  
| 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;"| '''Standard error '''
+
| 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;"| Come back to our slides.
 
+
* 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).'''
+
 
+
  
 +
* '''Standard error '''is the default output error
 +
* It is used to write all system errors
 +
* It is denoted by  '''number two (2).'''
 +
* It is also known as '''stderr (Standard error).'''
  
 
|-
 
|-
Line 303: Line 274:
  
 
'''command 2> errors.txt'''
 
'''command 2> errors.txt'''
| 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 default '''standard error''' output is visible on the screen or monitor.  
+
| 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;"|  
* '''Two greater than(2>)''' is error redirection symbol  
+
* The default '''standard error''' output is visible on the screen or monitor.  
* '''command space 2 greater than space error.txt'''
+
* '''Two greater than symbol (2>)''' is error redirection symbol  
 
+
* Syntax is '''command space 2 greater than space error.txt'''
 
+
  
 
|-
 
|-
Line 315: Line 285:
 
|-
 
|-
 
| 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;"| '''<nowiki># ls > ls_files.txt</nowiki>'''
 
| 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;"| '''<nowiki># ls > ls_files.txt</nowiki>'''
| 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;"| We will comment the previous statement.
+
| 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;"| We will comment the previous line.
  
'''rm /tmp/4815.txt 2> error.txt '''
+
i.e.'''ls'''
  
 
|-
 
|-
 
| 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;"| rm /tmp/4815.txt 2> error.txt
 
| 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;"| rm /tmp/4815.txt 2> error.txt
| 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 type''' rm space backslash tmp backslash 4815.txt space 2 greater than space error.txt.'''
+
| 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;"| Below it type''' rm space backslash tmp backslash 4815.txt space 2 greater than symbol space error.txt.'''
  
  
 
The error output is redirected to''' error.txt file.'''
 
The error output is redirected to''' error.txt file.'''
  
Click on save and switch to the terminal.
+
Click on '''Save''' and switch to the '''terminal'''.
  
 
|-
 
|-
Line 332: Line 302:
  
 
'''rm /tmp/4815.txt'''
 
'''rm /tmp/4815.txt'''
| 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;"| We will type the following command to see the''' '''error.
+
| 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;"| We will type a command to see the error.
  
 +
Type
 
'''rm /tmp/4815.txt'''
 
'''rm /tmp/4815.txt'''
 +
 +
Press '''Enter'''.
  
 
|-
 
|-
 
| 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;"| The error displayed will be
+
| 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 error displayed is
  
 
'''rm: cannot remove `/tmp/4815.txt': No such file or directory'''
 
'''rm: cannot remove `/tmp/4815.txt': No such file or directory'''
  
 
as the file is not present which we are trying to delete.
 
  
 
|-
 
|-
 
| 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;"| Now we will execute our previous '''dot slash''' command with error redirection.
+
| 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 we will execute our file.
  
 
|-
 
|-
Line 356: Line 327:
 
| 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;"| So, press the '''uparrow''' key.
 
| 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;"| So, press the '''uparrow''' key.
  
Recall the previous command.
+
Recall the previous command '''dot slash redirection.sh'''
  
 
Press '''Enter.'''
 
Press '''Enter.'''
Line 369: Line 340:
 
'''gedit error.txt'''
 
'''gedit error.txt'''
 
| 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;"| Type '''gedit error.txt''' and press '''Enter.'''
 
| 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;"| Type '''gedit error.txt''' and press '''Enter.'''
 
 
  
  
Line 391: Line 360:
 
In this tutorial we learnt
 
In this tutorial we learnt
  
* Input and output in bash
+
* Input and output in Bash
 
* Redirection and file descriptors  
 
* Redirection and file descriptors  
 
* <nowiki>Standard input using <(less than) symbol</nowiki>
 
* <nowiki>Standard input using <(less than) symbol</nowiki>
 
* Standard output >(greater than) symbol
 
* Standard output >(greater than) symbol
 
* Standard error using 2> (2 greater than symbol)
 
* Standard error using 2> (2 greater than symbol)
* With the help of some examples
 
 
 
  
 
|-
 
|-
Line 404: Line 370:
 
| 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,
 
| 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,
  
Write a program in any language like C, C++, java.  
+
Write a program in any language like C, C++, Java.  
  
 
And redirect the output or error to a new file.
 
And redirect the output or error to a new file.

Latest revision as of 22:27, 5 August 2014

­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 for practice.

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).
  • 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 stdinb(Standard input).


Display slide 9

Input Redirection


command < filename

  • The default standard input is the keyboard.
  • (<) Less than symbol 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
  • Type sort space less than symbol 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.

Open the terminal using Ctrl, Alt and T simultaneously on your keyboard.

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

Type cat space file.txt.

You can see that the file contains series of numbers.

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 the terminal after sorting.

The numbers are sorted in asscending order.

<<PAUSE>>

Display Slide 10

Standard Output


Come back to our slides.
  • Standard output is used by all commands to display output.
  • The default output is displayed on the screen.
  • It is denoted by number one(1).
  • Also known as stdout (Standard output).


Display slide 11

Output Redirection


command > filename

  • ( > )Greater than symbol is the 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 the terminal
ls Let me clear the prompt. First let us type 'ls' and see the output.
dot slash redirection.sh


Now press the uparrow key thrice.

Recall the previous command dot slash redirection.sh.

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


Come back to our slides.
  • Standard error is the default output error
  • It is used to write all system errors
  • It is denoted by number two (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 symbol (2>) is error redirection symbol
  • Syntax is 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 line.

i.e.ls

rm /tmp/4815.txt 2> error.txt Below it type rm space backslash tmp backslash 4815.txt space 2 greater than symbol 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 a command to see the error.

Type rm /tmp/4815.txt

Press Enter.

The error displayed is

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


Now we will execute our file.
dot slash redirection.sh


So, press the uparrow key.

Recall the previous command dot slash redirection.sh

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