exec command and field descriptors..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers exec command and field descriptors..
# 1  
Old 12-03-2004
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
# 2  
Old 12-04-2004
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.
# 3  
Old 12-04-2004
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
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 <
# 5  
Old 12-04-2004
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.
# 6  
Old 12-04-2004
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.
This User Gave Thanks to Perderabo For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Use 3 descriptors with ssh command

Hi, I want to execute some commands via ssh. errupdate <enter> =5A7598C3: <enter> Report=False <enter> Log=False <enter> <CTRL + D> <CTRL + D> how can i do this ? I suppose i have to use file descriptor but i don't know how ? Thank you (2 Replies)
Discussion started by: khalidou13
2 Replies

4. Shell Programming and Scripting

exec command help

Hi, I have the following lines in a script : . . exec < some_file . . . I have very little idea about exec command. I would like to know what this does and what will happen if the file some_file does not exist. Specifically, I would like to know whether the lines following this... (5 Replies)
Discussion started by: elixir_sinari
5 Replies

5. UNIX for Dummies Questions & Answers

Exec command

Hi can some one explain the following command , It would really help if some can really elloborate on what is happening out here export PATH | exec /bin/sh ./auto_approve :q P.S: This is the first time i am using exec ,so an elloboration what does it do and what is the use of the :q will be... (1 Reply)
Discussion started by: Sri3001
1 Replies

6. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

7. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

8. Shell Programming and Scripting

Help with exec command and file descriptors II

Need to close files which descriptor number are larger than 9 in ksh. 'exec 10>&-' fails with 'ksh: 10: not found'. How do you specify file descriptors which occupies two or more digits in ksh script? Thanks, Masaki (2 Replies)
Discussion started by: masaki
2 Replies

9. Shell Programming and Scripting

Help with exec command and file descriptors??

im having trouble with the comprehending the exec command and the use of file descriptors. given: #!/bin/sh exec 4>&1 exec 1>&2 exec 2>&4 exec 4>&- echo Hello would the standard output of this script be sent to STDOUT, STDERR or neither and why??? thanks for the help. (1 Reply)
Discussion started by: rfourn
1 Replies

10. UNIX for Dummies Questions & Answers

using the -exec command

linux redhat 8.0 I am getting accustomed to using the -exec command to get around my databse.. and use it to edit and update files..! is this more apllicable than jumping from one directory to the other.. I have set up the databse so that the inode #'s are accessable and can get me from one... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question