Difference between revisions of "BASH/C3/Using-File-Descriptors/English"
(Created page with "'''Title of script: Using file descriptors ''' '''Author:''' Lavitha Pereira and Ashwini Patil '''Keywords: Video tutorial, file descriptors to file, closing file descript...") |
|||
Line 16: | Line 16: | ||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 2 | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 2 | ||
− | | 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 | + | | 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 imput file descriptor | ||
− | * | + | * Close the file descriptor (fd) |
* With the help of some examples | * With the help of some examples | ||
Line 53: | Line 53: | ||
We have already studied about the '''file descriptors''' in the previous tutorial. | We have already studied about the '''file descriptors''' in the previous tutorial. | ||
− | * 0, 1 and 2 are the standard file descriptors for''' stdin,''' '''stdout''' and '''stderr | + | * 0, 1 and 2 are the standard file descriptors for''' stdin,''' '''stdout''' and '''stderr''' |
− | + | ||
* File descriptors are used for i/o redirection | * File descriptors are used for i/o redirection | ||
Line 61: | Line 60: | ||
|- | |- | ||
| 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;"| Slide 6 | | 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;"| Slide 6 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
Syntax: | Syntax: | ||
'''<nowiki>exec [File descriptor] > filename</nowiki>''' | '''<nowiki>exec [File descriptor] > 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;"| | + | | 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 syntax to assign a '''file descriptor '''to an output file is: |
'''<nowiki>exec [File descriptor] > filename</nowiki>''' | '''<nowiki>exec [File descriptor] > filename</nowiki>''' | ||
Line 78: | Line 72: | ||
I have a code file with the name '''fdassign.sh''' | I have a code file with the name '''fdassign.sh''' | ||
− | |||
− | |||
Line 105: | Line 97: | ||
This will write the output to '''output.txt''' file. | This will write the output to '''output.txt''' file. | ||
− | |||
− | |||
Line 120: | Line 110: | ||
This is similar to redirecting a string to a file. | This is similar to redirecting a string to a file. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 140: | Line 127: | ||
'''date SPACE a greater-than sign followed by ampersand followed by a file descriptor 3''' | '''date SPACE a greater-than sign followed by ampersand followed by a file descriptor 3''' | ||
− | |||
− | |||
Line 198: | Line 183: | ||
|- | |- | ||
| 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;"| 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. |
|- | |- | ||
Line 204: | Line 189: | ||
| 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 | |
'''./fdassign''' | '''./fdassign''' | ||
− | + | press '''Enter.''' | |
|- | |- | ||
Line 223: | Line 208: | ||
'''Paste above ''' | '''Paste above ''' | ||
− | | 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 fix | + | | 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 fix this error. |
Line 245: | Line 230: | ||
− | + | press '''Enter.''' | |
|- | |- | ||
Line 314: | Line 299: | ||
'''./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. |
Line 322: | Line 307: | ||
'''./fdread.sh''' | '''./fdread.sh''' | ||
− | |||
− | |||
Revision as of 17:40, 5 September 2014
Title of script: Using file descriptors
Author: Lavitha Pereira and Ashwini Patil
Keywords: Video tutorial, file descriptors to file, closing file descriptors
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on Using file descriptors |
Display Slide 2 | In this tutorial, we will learn to
|
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
Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
Introduction
Slide 5 |
Let us start with an introduction.
We have already studied about the file descriptors in the previous tutorial.
|
Slide 6
Syntax: exec [File descriptor] > filename |
The syntax to assign a file descriptor to an output file is:
exec [File descriptor] > filename
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.
For any newly opened file, we have additional file descriptors from 3 to 9.
This will write the output to 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 a file descriptor 3.
|
date >&3
|
Each new string will be appended to the file.
We will append the current system date to the output.txt file.
date SPACE a greater-than sign followed by ampersand followed by a file descriptor 3
|
exec 3<&- | Here we close the file descriptor.
|
CTRL+ALT+T keys | Let us execute the code and see the output.
Open the temrinal 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.-
Type: cat output.txt
Welcome to BASH learning and the current system date is displayed. |
Type:
echo “Hi” > &3 |
Let us go back to the editor.
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
|
./fdassign.sh: line 6: 3: Bad file descriptor | We see an error
./fdassign.sh: line 6: 3: Bad file descriptor |
Cut
echo "hiii" >&3 Paste above |
Let us fix this error.
Now I will cut the last line of code and paste it below the date command |
./fdassign.sh | Click on Save.
./fdassign.sh
|
cat output.txt | Now let us open the output.txt file.
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.
|
Now we will assign the file descriptor to the input file.
| |
[Highlight]
exec 3< output.txt cat <&3 exec 3<&- |
This is the exec command.
|
chmod +x fdread.sh
./fdread.sh |
Now let us execute this shell script.
chmod +x fdread.sh ./fdread.sh
|
Welcome to BASH learning
Fri Aug 8 15:12:06 IST 2014 Hi |
We can see the output on the terminal.
|
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,
|
Assignment Slide 8 | As an assignment,
Try to
|
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.
|
Display Slide 10
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
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.
|
Display Slide 12 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |