jc@minya.UUCP (John Chambers) (08/31/90)
Hey, wow!, a special group for all my dumb shell questions! And I just happen to have one to ask... Recently, I've had the fun of modifying programs so they can be started by init and/or inetd, and still work. These daemons, unlike shells, have a way of starting programs with very little initialization. On some of my recent portability tests, I have even come across one Unix system whose init starts things with an empty environ vector (i.e., environ[0] == (char *)0), and with NO open files. Yes, you read that right; files 0, 1 and 2 are not open. It does at least make argv[0] contain a pointer to the program's name, and argc is correct, so I guess I really shouldn't complain. But this causes interesting problems when the "program" is a shell script. In C, I know how to call fstat() and test the result for zero. In Bourne Shell, I don't know how to do the equivalent. I basically want to write something like: if [ <file 0 is open> ];then exec</dev/null;fi but I don't know what to put between the [ and the ]. Any ideas? Is it possible? Or should I just rewrite the scripts in C? -- Zippy-Says: Imagine ... a world without clothing folds, chiaroscuro, or marital difficulties ... Home: 1-617-484-6393 Work: 1-508-952-3274 Uucp: ...!{harvard.edu,ima.com,eddie.mit.edu,ora.com}!minya!jc (John Chambers) Uucp-map: minya adelie(DEAD)
tif@doorstop.austin.ibm.com (Paul Chamberlain/32767) (08/31/90)
In article <437@minya.UUCP> jc@minya.UUCP (John Chambers) writes: >I basically want to write something like: > if [ <file 0 is open> ];then exec</dev/null;fi This seems to work: if read x then : okay else exec < /dev/null fi I hope you're not going to all this trouble to use /dev/null though. Paul Chamberlain | I do NOT represent IBM tif@doorstop, sc30661@ausvm6 512/838-7008 | ...!cs.utexas.edu!ibmaus!auschs!doorstop.austin.ibm.com!tif
jeff@quark.WV.TEK.COM (Jeff Beadles) (08/31/90)
jc@minya.UUCP (John Chambers) writes: >I basically want to write something like: > if [ <file 0 is open> ];then exec</dev/null;fi >but I don't know what to put between the [ and the ]. Any ideas? >Is it possible? Or should I just rewrite the scripts in C? Well, from the test(1) man page... -t [ fildes ] True if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device. This, you can tell if a file descriptor is attached to a terminal. (It will fail for a pipe or redirection) Thus, something like this might be what you are looking for: if [ -t 0 ] ; then exec </dev/null fi Best of luck, -Jeff -- Jeff Beadles jeff@quark.WV.TEK.COM UTek Network Engineering, Tektronix Inc. +1 503 685 2568 SPEEA - Just say no.
jc@minya.UUCP (John Chambers) (09/03/90)
In article <3330@awdprime.UUCP>, tif@doorstop.austin.ibm.com (Paul Chamberlain/32767) writes: > In article <437@minya.UUCP> jc@minya.UUCP (John Chambers) writes: > >I basically want to write something like: > > if [ <file 0 is open> ];then exec</dev/null;fi > > This seems to work: > > if read x > then : okay > else > exec < /dev/null > fi > > I hope you're not going to all this trouble to use /dev/null though. No, of course not. The problem is that, if init invokes a script, and the script invokes programs that assume the standard three files are open, the result is often that some program bombs out, the shell doesn't like it and so it exits, and the script dies in its tracks. If I just wanted to use /dev/null, I'd direct the I/O there. What I want is to find out which (if any) of the standard files aren't open, and open them attached to something innocuous; for file 0, the obvious choice is /dev/null. If any of the three standard files are open, I'f like to leave them where they are, so they can be passed on to subprocesses as usual. Another problem with the above is that it gobbles down one line of standard input, so the first command that does input will miss its first line. It'd be a bit weird to tell users that input files must have a dummy first line that will be discarded, just because the script does something like this. -- Zippy-Says: Imagine ... a world without clothing folds, chiaroscuro, or marital difficulties ... Home: 1-617-484-6393 Work: 1-508-952-3274 Uucp: ...!{harvard.edu,ima.com,eddie.mit.edu,ora.com}!minya!jc (John Chambers) Uucp-map: minya adelie(DEAD)
lugnut@sequent.UUCP (Don Bolton) (09/05/90)
In article <437@minya.UUCP> jc@minya.UUCP (John Chambers) writes: >Hey, wow!, a special group for all my dumb shell questions! And I just >happen to have one to ask... > > >Recently, I've had the fun of modifying programs so they can be started >by init and/or inetd, and still work. These daemons, unlike shells, >have a way of starting programs with very little initialization. On >some of my recent portability tests, I have even come across one Unix >system whose init starts things with an empty environ vector (i.e., >environ[0] == (char *)0), and with NO open files. Yes, you read that >right; files 0, 1 and 2 are not open. It does at least make argv[0] >contain a pointer to the program's name, and argc is correct, so I >guess I really shouldn't complain. > >But this causes interesting problems when the "program" is a shell >script. In C, I know how to call fstat() and test the result for >zero. In Bourne Shell, I don't know how to do the equivalent. I >basically want to write something like: > if [ <file 0 is open> ];then exec</dev/null;fi >but I don't know what to put between the [ and the ]. Any ideas? >Is it possible? Or should I just rewrite the scripts in C? I'm one of them thar marketeer types gone bad turned programmer, so I have some trouble relating to your meaning of "open" but test operators are... -b file file is a special block file -c file file is a special character file -d file file is a directory -f file file is an ordinary file -g file file has its set group id (SGID) bit set -k file file has its sticky bit set -p file file is a named pipe -r file file is readable by the process < this whatcha need ? -s file file is nonzero length -t fd fd is the open file descriptor associated with a terminal (1 is default) -u file file has its user id (SUID) bit set -w file file is writeable by the process -x file file is executable I reccomend the book UNIX SHELL PROGRAMMING Kochan and Wood one o them thar Hayden Books. and The UNIX Programming Environment by Kernighan and Pike, Prentice Hall. Twixt the two I've written some hairy programs. also the AWK Programming Language by Aho, Weinberger, Kernighan looks like it would be of help to you.
lugnut@sequent.UUCP (Don Bolton) (09/05/90)
In article <3330@awdprime.UUCP> tif@reed.UUCP (Paul Chamberlain/32767) writes: >In article <437@minya.UUCP> jc@minya.UUCP (John Chambers) writes: >>I basically want to write something like: >> if [ <file 0 is open> ];then exec</dev/null;fi > >This seems to work: > > if read x > then : okay > else > exec < /dev/null > fi > HUH? seems like echo "" or exit would go there, or just forget the else entirely tis an optional item. >I hope you're not going to all this trouble to use /dev/null though. > Best use for /dev/null is a mail forward while you are on vacation :-) >Paul Chamberlain | I do NOT represent IBM tif@doorstop, sc30661@ausvm6 >512/838-7008 | ...!cs.utexas.edu!ibmaus!auschs!doorstop.austin.ibm.com!tif
jsdy@hadron.COM (Joseph S. D. Yao) (10/12/90)
In article <437@minya.UUCP> jc@minya.UUCP (John Chambers) writes: >Recently, I've had the fun of modifying programs so they can be started >by init and/or inetd, and still work. ... >environ[0] == (char *)0), and with NO open files. ... >But this causes interesting problems when the "program" is a shell >script. In C, I know how to call fstat() and test the result for >zero. In Bourne Shell, I don't know how to do the equivalent. I >basically want to write something like: > if [ <file 0 is open> ];then exec</dev/null;fi With inetd, you should always have 0 and 1 open to the net connection; no problem. With init, you are either running out of one of the /etc/rc-type files, or directly from inittab (assuming a System V-style init). In either case, my suggestion is to use the Kobiyashi Maru alternative: if you don't like the rules, change them! When calling the program from init, explicitly call it: /etc/my_shell_prog < /dev/null > /dev/null 2>&1 or even HOME=/ /bin/ksh /etc/my_shell_prog < /dev/null > /dev/null 2>&1 Note that it is nowhere promised that there would be anything IN environ, or even in argv, merely that they would be there. Program so as to take into account the worst possibility, and things will usually work out much better. Joe Yao jsdy@hadron.COM ( jsdy%hadron.COM@{uunet.UU.NET,decuac.DEC.COM} ) arc,arinc,att,avatar,blkcat,cos,decuac,\ dtix,ecogong,grebyn,inco,insight,kcwc, \ lepton,lsw,netex,netxcom,phw5,research, >!hadron!jsdy rlgvax,seismo,sms,smsdpg,sundc,telenet, / uunet / (Last I counted ...)