(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



2003/11/04 exec





In Bourne-like shells (sh, bash, ksh), exec serves two functions:

It can replace your current program with another, or can help you control input and output more easily.

The first use is often used at the last line of a .profile:

exec someprog

Without the "exec", someprog would still run, but when the user quit, they'd be returned to .profile which, having nothing else to do, would drop them to a shell prompt. With the "exec", leaving the program brings them to login.

For redirecting input, exec is very handy. The following little script happily echoes whatever you type, unless you type Q:


date > tt
exec 5<&0
while true
do
read stuff
echo $stuff
exec <&5
case $stuff in 
 [qQ]) exec < tt;;
 esac
 done
 

This illustrates how you can move back and forth between different input sources. The "exec 5<&0" saves the current standard input so that we can later restore it (exec <&5). If the input sees Q, then the file tt is read instead.

If you do

exec > tt
ls
 

the output of "ls" will be in tt (do "exec > /dev/tty" to return things to normal).

You can specifically close a file descriptor too:

exec <&-
 

closes STDIN.




Click here to add your comments


It's not strictly true to say that "exec > /dev/tty" returns things to normal. I did a quick test and found that once you do this, you can't then redirect the output from the script to a file at the command line level...any output will pop up on the screen.

Anyone know a better way of doing it?

ian_mclaughlin@hotmail.com




Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Auto FTP Manager

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Misc.


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress