CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Shell Programming Series(XX)


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author System On 2007-04-29 10:01:48




View and vote on the article here: Shell Programming Series(XX)


Shell Programming Series(XX)

Category
Programming
Summary
File Descriptors



File descriptors are numbered IDs that are set up each time the kernel (FreeBSD) starts a process. These numbers are what the process uses to write output and read input.
Body
There are three file descriptors that are opened by by default:



F.D. 0 ==> STDIN. This is where standard input comes from. It is normally the keyboard, bit it can be redirected to read from a file or some other source.

F.D. 1==> STDOUT. This is where standard output goes. It is normally the screen, but as you have seen it can be redirected.

F.D. 2==>STDERR. This is where standard error messages go. Once again, it is normally the screen, but it can also be redirected.



File descriptors can be used in shell programs to make your programs both more efficient and easier to write. The "exec" command can be used to open a file descriptor. Here is an example of how file descriptors can make your programs both easier to write and more efficient:



#!/bin/sh

# open a file descriptor on F.D. 1 (STDOUT)

exec > testfile.txt

# echo some stuff to STDOUT which will now go to the file testfile.txt

echo "Line1 of the file"

echo "Line2 of the file"

echo "Line3 of the file"

echo "Line4 of the file"

echo "Line5 of the file"

exit 0




The exec statement in line two of the previous code sample causes "testfile.txt" to be opened as STDOUT. As a result, all the echo lines are set to the "testfile.txt" instead of to the screen, even though the output is not redirected in the echo statements. If you have a lot of things that need to be written to a file, this is more efficient than using shell redirection each time. It is also easier to code since you don't have to redirect the output each time.

This is also very useful when opening a file descriptor for STDIN and using read. Here is a sample:



#!/bin/sh

# open a file descriptor on F.D. 0 (STDIN)

exec < testfile.txt

while read string

do

echo $string

done

exit 0




Assuming that you still have the file "textfile.txt" from the previous example, this sample will output:



Line1 of the file

Line2 of the file

Line3 of the file

Line4 of the file

Line5 of the file




So, what is so great about this? It demonstrates an important concept of using a file descriptor with read. Since the file is not closed between each call to read, read remembers the last line it read, and moves the pointer to the next line in the file. This way, read will read each line of the file in sequence automatically.



This article was imported from zZine. (original author: ismail)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.03152 seconds


About Us | Privacy Policy | Mission Statement | Help