The UNIX Forums  



Go Back   The UNIX Forums > Top Forums > UNIX for Dummies Questions & Answers
Home Forums Register Rules & FAQDonate Members List Search Today's Posts Mark Forums Read

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

Reply
 
Submit Tools Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-2004
moxxx68's Avatar
Registered User
 
Join Date: Mar 2004
Posts: 301
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Arrow 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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-04-2004
Perderabo's Avatar
Unix Daemon
 
Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,060
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Forum Sponsor
  #3 (permalink)  
Old 12-04-2004
moxxx68's Avatar
Registered User
 
Join Date: Mar 2004
Posts: 301
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #4 (permalink)  
Old 12-04-2004
Perderabo's Avatar
Unix Daemon
 
Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,060
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 <
Reply With Quote
  #5 (permalink)  
Old 12-04-2004
moxxx68's Avatar
Registered User
 
Join Date: Mar 2004
Posts: 301
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #6 (permalink)  
Old 12-04-2004
Perderabo's Avatar
Unix Daemon
 
Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,060
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with exec command and file descriptors?? rfourn Shell Programming and Scripting 1 07-18-2007 05:05 PM
Find command with prune and exec Sebarry UNIX for Advanced & Expert Users 1 06-18-2006 12:43 PM
Find command with prune and exec Sebarry Chat with iBot - Our RSS Robot Girl 1 06-18-2006 08:36 AM
exec command g_s_r_c Shell Programming and Scripting 1 09-20-2004 11:20 AM
using the -exec command moxxx68 UNIX for Dummies Questions & Answers 0 04-13-2004 01:51 PM


web tracker

All times are GMT -5. The time now is 03:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
UNIX Forum Content Copyright ©1993-2008 SilkRoad Asia All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93