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.


Hate these ads?

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.



Comments
WordsoftheDayexec :

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


Add your comments

ad

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
131363 1,495

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

pavatar.jpg
More:




Unix/Linux Consultants


SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at http://opensystemscomputing.com and www.go2unix.com.


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!


UBB Computer Services Support for Openserver, Unixware and Linux. Windows integration with Unix/Linux servers. Hardware, Backup and Networking issues. Located near Sacramento CA, we provide onsite support throughout Northern CA and Nationwide via remote access. We are a SCO Authorized Partner and a Microlite BackupEdge Certified Reseller.



Twitter
  • Nov 30 20:25
    I have 37,000 words of a 50,000 word project. I'd like to finish it this week..
  • Nov 30 20:05
    My wife made turkey sandwiches with stuffing and cranberry orange relish - I did not want to eat the last bite. Didn't want it to end!









Change Congress


Related Posts