[SunHELP] simple query about "exec"

DAUBIGNE Sebastien - BOR sunhelp at sunhelp.org
Mon Nov 12 08:31:59 CST 2001


You can backup both stderr and stdout to any free file=20
descriptor using the ">&" redirection operator.

Basically ">&" is a front-end for the "dup" syscall (man dup/dup2)=20
which duplicates a file decriptor to any other free file descriptor.

Here is a sample script :=20

# First backup stdout/stderr to fds 5 and 6,=20
# then redirects stdout/stderr to file.
# Be carefull to keep the ">&" operators in this order
# (sh interprets it from left to right)
exec 5>&1 6>&2 1> file 2>&1
# Do the job=20
....
# First restore stdout/stderr to original values
# then close backup fds 5 and 6
exec 1>&5 2>&6 5>&- 6>&-

Note that you have to make sure that the backup file descriptors=20
(5 and 6 in this example) are not currently in use before using the=20
first exec.
For instance, ksh usually opens fd 63 for .sh_history file. As far as I =

know, sh basically doesn't open any fd except standards in/out/err=20
FDs (0/1/2).

You can get the open FDs of the current shell with one of theses =
commands :=20

/usr/proc/bin/pfiles $$
ls -il  /proc/$$/fd

--
Sebastien DAUBIGNE=20
sebastien.daubigne at sema.fr <mailto:sebastien.daubigne at sema.fr>  - (+33)
(0)5.57.26.56.36
Sema Global Services - AFM/DW/Pessac

	-----Message d'origine-----
	De:	Foong, Tzeweng [SMTP:Tzeweng.Foong at team.telstra.com]
	Date:	vendredi 9 novembre 2001 09:08
	=C0:	sunhelp at sunhelp.org
	Objet:	RE: [SunHELP] simple query about "exec"

	Hmm I would like to know as well !!!

	Whan I tried to find out I was not able to figure it out. I hope=20
	there is a way to do it...... =20

	My solution was to to create another script say "subscript.sh" and=20
	call that script. Then have subscript.sh do the "exec" thing.
	once sub script finishes then the main script will be executing=20
	and output/loging goes to where it was previously.

	Hope this helps. I still would like to know how to reset the=20
	stdout stderr thing if some one knows how to do it.

	Tze Weng Foong




	-----Original Message-----
	From: TAG DBA [mailto:dbatag at tatainfotech.com]
	Sent: Thursday, 8 November 2001 5:18 PM
	To: 'sunhelp at sunhelp.org'
	Subject: [SunHELP] simple query about "exec"


	This must be a real simple question - but I cant seem to figure this
out :
	In one of my scripts I redirect std o/p and std error to a file like
	this "
	exec >$LOGFILE 2>&1 =20
	And later I reset std error and std input back to the screen like
this :
	exec > /dev/tty 2>&1=20

	This works fine when I run the script from the command line - but
when I
	run it from cron , the second line gives this error :
	/scripts/doexp.sh[84]: /dev/tty: cannot create

	Perhaps there is no /dev/tty associated with a cron job - but then
how
	do I still get the script to dump std output and std error to where
it
	was originally going ?

	Thanks and Regards,
	~aslam
	_______________________________________________
	SunHELP maillist  -  SunHELP at sunhelp.org
	http://www.sunhelp.org/mailman/listinfo/sunhelp
	_______________________________________________
	SunHELP maillist  -  SunHELP at sunhelp.org
	http://www.sunhelp.org/mailman/listinfo/sunhelp



More information about the SunHELP mailing list