exec command and field descriptors.. | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

exec command and field descriptors..

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-03-2004
moxxx68's Avatar
moxxx68 moxxx68 is offline
Registered User
 
Join Date: Mar 2004
Last Activity: 17 December 2005, 5:40 AM EST
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
CPU & Memory exec command and field descriptors..

according to the many sources the exec command other than its use in find and escaping the shell, has another definitive use.. which I am having a hard time understanding.
according to many resources and info pages that I have read I can use the exec command with a file descriptor.. such as
exec 1< file
or
exec 5>&0
I do not quite understand what a file descriptor is and what is the purpose of this (pattern) or (expression)..
any feedback welcome
moxxx68
Sponsored Links
    #2  
Old 12-04-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 18 April 2014, 4:11 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
If you write a script, you can send input into it like this:

./somescript < inputfile

The script can accomplish the same thing internally by using:
exec < inputfile

After that line, the script's input is inputfile. It can do the same to output:

exec > somescript.log 2>&1

This send stderr and stdout combined into the file. There is more to it, but this is the basics.
Sponsored Links
    #3  
Old 12-04-2004
moxxx68's Avatar
moxxx68 moxxx68 is offline
Registered User
 
Join Date: Mar 2004
Last Activity: 17 December 2005, 5:40 AM EST
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
correct me if I am wrong (i saw a similiar example to what you are saying in tha perl man pages)..
$ page some.script
#!/bin/sh
#basename
fd=basename
etc etc
$page input.file
#!/bin/sh
# input.file
etc etc
exec fd < input.file
etc etc
fi
    #4  
Old 12-04-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 18 April 2014, 4:11 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
The trouble is that exec can do several different things...it really needs to be several commands. The code you gave won't work. Change that to
exec $fd < inputfile
and it probably will. But that is not what's meant by an fd.

A fd is always an integer. In shell scripts, it will be a very low integer. By convention:
0 = standard input
1 = standard output
2 = standard error output

The idea is that you write your program to output to fd 1 without knowing what fd one is. Then at execution time you can do stuff like:
echo this > first.file
echo that > second.file

It would be terrible if echo always sent stuff to "first.file". You would need to do:
echo that
cp first.file second.file
or something like that.

By default 0 1 2 are all connected to /dev/tty so you can type input to a program and see the results in your window.

Here is an experiment I just did:
$ expr 1 + 2
3
$ expr 1 + 2 > expr.out
$ cat expr.out
3
$ expr cat + dog > expr.out
expr: non-numeric argument
$

With the last expr command, I have an error. Since the error goes to 2 which is still /dev/tty, I see it immediately, even though the standard which is 1 goes to a file. That why we have both 1 and 2. You can send 1 into a file while 2 is still displayed to you.

Don't want to see error messages? Bad idea usually, but you can do:
expr cat + dog > expr.out 2>/dev/null

And now error messages are thrown away.

expr cat + dog > expr.out
really means
expr cat + dog 1> expr.out
but if you leave the integer off, 1 is assumed for > while 0 is assumed for <
Sponsored Links
    #5  
Old 12-04-2004
moxxx68's Avatar
moxxx68 moxxx68 is offline
Registered User
 
Join Date: Mar 2004
Last Activity: 17 December 2005, 5:40 AM EST
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
where does :
exec 3
exec 4
exec 5
come into the picture.. ?
so far I have understood the stdout and stderr
but i don't see how this the use of exec or the use of fd 3 can write to a file. or how fd 4 can read stndin and how fd 5 can stdout to tty.. i am not making the connection between the numbers and the exec command basically for 3 to 5.
Sponsored Links
    #6  
Old 12-04-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 18 April 2014, 4:11 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
They are other integers that relate to files. Some programs are too complex to fit into a stdin/stdout model. Some scripts simply need more stuff as well. A contrived example:

exec 3> john.out
exec 4>paul.out
exec 5>george.out
exec 6>ringo.out

echo harrison >&5
echo lennon >&3

With these echo statements, something like >&3 really means 1>&3 which means send fd 1 into whatever fd 3 is pointing to. Nobody actually writes to 3 in this case. 3 is kind of a placeholder. With the korn shell, you can do

print -u6 starr

where the -u6 says to actually use fd 6. And you might write a c program with statements like:
write(4, "mccartney", 10);

With a program like that, you may need to connect something to fd 4 if the program itself doesn't do it.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use 3 descriptors with ssh command khalidou13 Shell Programming and Scripting 2 05-09-2012 07:51 AM
exec and file descriptors anamcara HP-UX 0 04-20-2009 04:02 AM
exec command santosh1234 Shell Programming and Scripting 2 04-02-2009 08:52 AM
Help with exec command and file descriptors II masaki Shell Programming and Scripting 2 01-03-2009 10:27 PM
Help with exec command and file descriptors?? rfourn Shell Programming and Scripting 1 07-18-2007 06:05 PM



All times are GMT -4. The time now is 07:43 PM.