Difference between revisions of "BASH/C3/Using-File-Descriptors/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 18: Line 18:
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we will learn to
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we will learn to
  
* Assign an output file descriptor  
+
* Assign an '''output file descriptor'''
* Assign an imput file descriptor  
+
* Assign an '''input file descriptor'''
* Close the file descriptor (fd)  
+
* Close the '''file descriptor (fd)'''
 
* With the help of some examples  
 
* With the help of some examples  
 
 
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 3'''Prerequisites'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 3
 
+
 
+
  
 +
'''Prerequisites'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial you should have knowledge of '''Shell Scripting''' in''' BASH.'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial you should have knowledge of '''Shell Scripting''' in''' BASH.'''
  
Line 43: Line 40:
 
* '''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 51: Line 48:
 
| 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 start with an introduction.
 
| 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 start with an introduction.
  
We have already studied about the '''file descriptors''' in the previous tutorial.
+
We have already studied about '''file descriptors''' in the previous tutorial.
 
+
* 0, 1 and 2 are the standard file descriptors for''' stdin,''' '''stdout''' and '''stderr'''
+
* File descriptors are used for i/o redirection
+
 
+
  
 +
* 0, 1 and 2 are the standard '''file descriptors''' for''' stdin,''' '''stdout''' and '''stderr'''
 +
* '''File descriptors''' are used for '''i/o redirection'''.
  
 
|-
 
|-
Line 90: Line 85:
  
 
We know that 0, 1, and 2 are standard '''file descriptors. '''
 
We know that 0, 1, and 2 are standard '''file descriptors. '''
 +
  
 
For any newly opened file, we have additional '''file descriptors''' from 3 to 9.
 
For any newly opened file, we have additional '''file descriptors''' from 3 to 9.
Line 96: Line 92:
 
Here, '''3 '''is the '''file descriptor'''.
 
Here, '''3 '''is the '''file descriptor'''.
  
This will write the output to '''output.txt''' file.
+
This will write the output to the '''output.txt''' file.
  
  
Line 106: Line 102:
 
| 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 string '''"Welcome to BASH learning" '''is sent to the file '''output.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 string '''"Welcome to BASH learning" '''is sent to the file '''output.txt'''  
  
This is done via a '''file descriptor 3'''.
+
This is done via '''file descriptor 3'''.
  
  
Line 121: Line 117:
 
For example:
 
For example:
  
We will append the current system date to the''' output.txt''' file.  
+
We will append the current '''system date''' to the''' output.txt''' file.  
  
  
 
The syntax is:
 
The syntax is:
  
'''date SPACE a greater-than sign followed by ampersand followed by a file descriptor 3'''
+
'''date SPACE greater-than symbol ampersand sign 3'''
  
  
Line 134: Line 130:
  
  
After this line the '''descriptor '''cannot write anything to the '''output.txt''' file.
+
After this line, the '''descriptor '''cannot write anything to the '''output.txt''' file.
  
 
|-
 
|-
Line 140: Line 136:
 
| 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 execute the code 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 us execute the code and see the output.
  
Open the '''temrinal '''using '''CTRL+ALT+T''' keys.
+
Open the '''terminal '''using '''CTRL+ALT+T''' keys.
  
 
|-
 
|-
Line 157: Line 153:
 
| 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;"| '''cat output.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;"| '''cat output.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;"| Let us check the output now by typing.-
 
| 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 check the output now by typing.-
 
Type:
 
  
 
'''cat output.txt'''
 
'''cat output.txt'''
  
  
We can see that the string,
+
We can see that the string '''Welcome to BASH learning''' and the current '''system date''' is displayed.
 
+
'''Welcome to BASH learning and the current system date is displayed.'''
+
  
 
|-
 
|-
Line 183: Line 175:
 
|-
 
|-
 
| 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;"|Click on Save. Let us execute the script once again and see what happens.
+
| 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'''.  
 +
 
 +
Let us execute the script once again and see what happens.
  
 
|-
 
|-
 
| 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;"| '''./fdassign'''
 
| 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;"| '''./fdassign'''
| 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;"| On 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;"| On the '''terminal''', press the uparrow key, to recall the previous command
 
+
Press the uparrow key, to recall the previous command
+
  
 
'''./fdassign'''
 
'''./fdassign'''
Line 200: Line 192:
 
| 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 see an 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 see an error
  
'''./fdassign.sh: line 6: 3: Bad file descriptor'''
+
'''Bad file descriptor'''
  
 
|-
 
|-
Line 212: Line 204:
  
 
Come back to the editor.
 
Come back to the editor.
 
Now
 
  
 
I will cut the last line of code and paste it below the '''date command'''
 
I will cut the last line of code and paste it below the '''date command'''
Line 219: Line 209:
 
|-
 
|-
 
| 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;"| '''./fdassign.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;"| '''./fdassign.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;"| Click on Save.
+
| 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'''.
  
  
Line 225: Line 215:
  
  
On the terminal, recall the previous command:
+
On the '''terminal''', recall the previous command:
  
 
'''./fdassign.sh'''
 
'''./fdassign.sh'''
Line 256: Line 246:
 
|-
 
|-
 
| 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 assign the file descriptor to the input 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 we will assign the '''file descriptor''' to the input file.
  
  
Line 282: Line 272:
  
 
The line  
 
The line  
 
  
 
'''<nowiki>exec 3< output.txt</nowiki>'''
 
'''<nowiki>exec 3< output.txt</nowiki>'''
 
  
 
will open the file for reading.
 
will open the file for reading.
Line 299: Line 287:
  
 
'''./fdread.sh'''
 
'''./fdread.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 let us execute this shell script.
+
| 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 let us execute this '''shell script'''.
  
  
On the terminal type:
+
On the '''terminal''', let me clear the '''prompt'''. Type:
  
 
'''chmod +x fdread.sh'''
 
'''chmod +x fdread.sh'''
  
'''./fdread.sh'''
+
 
 +
Type '''./fdread.sh'''
  
  
Line 328: Line 317:
 
| 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 the slides.
 
| 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 the slides.
  
Let us summarize
+
Let us summarize.
  
 
In this tutorial we learnt to,
 
In this tutorial we learnt to,
  
* Assign the output file descriptor  
+
* Assign the '''output file descriptor'''
 
+
* Assign the input file descriptor
+
* Closing the file descriptor.
+
 
+
  
 +
* Assign the '''input file descriptor'''
 +
* Closing the '''file descriptor'''.
  
 
|-
 
|-
Line 344: Line 331:
  
 
Try to  
 
Try to  
 
+
* append a few lines to a file '''test.txt'''using '''file descriptors'''
* append a few lines to a file '''test.txt'''<br/> using '''file descriptors '''covered in this tutorial.
+
 
* Display the content of the file using '''file descriptors'''
 
* Display the content of the file using '''file descriptors'''
 
 
  
 
|-
 
|-

Latest revision as of 11:01, 6 September 2014

­Title of script: Using file descriptors

Author: Lavitha Pereira and Ashwini Patil

Keywords: Video tutorial, file descriptors to file, closing file descriptors


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on Using file descriptors
Display Slide 2 In this tutorial, we will learn to
  • Assign an output file descriptor
  • Assign an input file descriptor
  • Close the file descriptor (fd)
  • With the help of some examples
Display Slide 3

Prerequisites

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.

Introduction

Slide 5

Let us start with an introduction.

We have already studied about file descriptors in the previous tutorial.

  • 0, 1 and 2 are the standard file descriptors for stdin, stdout and stderr
  • File descriptors are used for i/o redirection.
Slide 6

Syntax:

exec [File descriptor] > filename

The syntax to assign a file descriptor to an output file is:

exec [File descriptor] > filename


Let us see an example.

I have a code file with the name fdassign.sh


#!/usr/bin/env bash The first line is the shebang line.
exec 3> output.txt


The exec command replaces the current shell process.


It will be executed in the place of the current shell without creating a new process.


We know that 0, 1, and 2 are standard file descriptors.


For any newly opened file, we have additional file descriptors from 3 to 9.


Here, 3 is the file descriptor.

This will write the output to the output.txt file.


echo "Welcome to BASH learning" >&3


The string "Welcome to BASH learning" is sent to the file output.txt

This is done via file descriptor 3.


This is similar to redirecting a string to a file.

date >&3


Each new string will be appended to the file.


For example:

We will append the current system date to the output.txt file.


The syntax is:

date SPACE greater-than symbol ampersand sign 3


exec 3<&- Here we close the file descriptor.


After this line, the descriptor cannot write anything to the output.txt file.

CTRL+ALT+T keys Let us execute the code and see the output.

Open the terminal using CTRL+ALT+T keys.

Type:

chmod +x fdassign.sh

./fdassign

Type:

chmod +x fdassign.sh

./fdassign

cat output.txt Let us check the output now by typing.-

cat output.txt


We can see that the string Welcome to BASH learning and the current system date is displayed.

Type:

echo “Hi” > &3

Let us go back to the editor.


Now I will type echo at the end, after the descriptor is closed.


Type:

echo “Hi” > &3

Click on Save.

Let us execute the script once again and see what happens.

./fdassign On the terminal, press the uparrow key, to recall the previous command

./fdassign


press Enter.

./fdassign.sh: line 6: 3: Bad file descriptor We see an error

Bad file descriptor

Cut

echo "hiii" >&3

Paste above

Let us fix this error.


Come back to the editor.

I will cut the last line of code and paste it below the date command

./fdassign.sh Click on Save.


Let us execute the code once again.


On the terminal, recall the previous command:

./fdassign.sh


press Enter.

cat output.txt Now let us open the output.txt file.


Type:

cat output.txt

Welcome to BASH learning

Fri Aug 8 15:02:51 IST 2014

Hi

We can see the output.

The string Hi is displayed at the end.


<<PAUSE>>

Now we will assign the file descriptor to the input file.


Let us see an example.


I have a file named fdread.sh


Let us go through it.

[Highlight]

exec 3< output.txt

cat <&3

exec 3<&-

This is the exec command.


Here we will read the file output.txt


The line

exec 3< output.txt

will open the file for reading.


cat command will display the content of the file.


And finally we close the file descriptor.

chmod +x fdread.sh

./fdread.sh

Now let us execute this shell script.


On the terminal, let me clear the prompt. Type:

chmod +x fdread.sh


Type ./fdread.sh


Welcome to BASH learning

Fri Aug 8 15:12:06 IST 2014

Hi

We can see the output on the terminal.


The content of output.txt file is displayed.

This brings us to the end of this tutorial.
Summary slide 7 Come back to the slides.

Let us summarize.

In this tutorial we learnt to,

  • Assign the output file descriptor
  • Assign the input file descriptor
  • Closing the file descriptor.
Assignment Slide 8 As an assignment,

Try to

  • append a few lines to a file test.txtusing file descriptors
  • Display the content of the file using file descriptors
Display Slide 9

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 10

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 11

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