|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|