![]() |
|
|||||||
| Home | Forums | Register | Rules & FAQ | Donate | 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 !! |
![]() |
|
|
Submit Tools | Thread Tools | Search this Thread | Display Modes |
| Forum Sponsor |
|
|
|
||||
|
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. |
| Forum Sponsor |
|
|
|
||||
|
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 < |
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |