[comp.unix.questions] Using 'exit' in a Bourne shell script

pjh@mccc.UUCP (Peter J. Holsberg) (01/30/88)

I would like to have my users logout via a shell script "off" rather
than with ^d.  The script I wrote is very simple, but 'exit' has no
effect.

		echo "OFF \c:" >> /usr/.logins
		who am i >> /usr/.logins
		exit

The correct entries appear in /usr/.logins, but 'exit' seems to be
ignored.  Typing 'exit' at the prompt does in fact do what I want --
i.e., act as a substitute for ^d.  I see that exit(2) is a system call
that terminates the calling process.  Oh oh!  It terminates 'off' but
doesn't log the user off!  Rats!@!@#$%

Can anyone suggest a way (SysVr3.0) to do what I want?  Thanks.
.

-- 
Peter Holsberg                  UUCP: {rutgers!}princeton!mccc!pjh
Technology Division             CompuServe: 70240,334
Mercer College                  GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

ok@quintus.UUCP (Richard A. O'Keefe) (01/30/88)

In article <169@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
> I would like to have my users logout via a shell script "off" rather
> than with ^d.
> Can anyone suggest a way (SysVr3.0) to do what I want?  Thanks.

Don't use a script.  Use a shell function.  Then it'll be running in
the login shell.  By the way, there is a potential surprise anyway:
	login: user-logs-in
	$ ed myfile
	!off
Here we have login shell-->ed-->subshell, and you can't log out of the
subshell.

Since you are writing records to a log file, presumably you do
something like this on login:
	echo ON $USER `tty` >>logfile
Do one more thing:

	echo $$ON $USER `tty`

Now to logout, you can do
	echo OFF $USER `tty` >>logfile
	CODE="ON $USER `tty`"
	kill -HUP `egrep -e "$CODE$" logfile | tail -1 | sed -e "s/ON.*//"`
	
This should work from a subshell.

kathy@bakerst.UUCP (Kathy Vincent) (01/31/88)

In article <169@mccc.UUCP> pjh@mccc.UUCP (Peter J. Holsberg) writes:
>I would like to have my users logout via a shell script "off" rather
>than with ^d.  The script I wrote is very simple, but 'exit' has no
>effect. ...
>
>The correct entries appear in /usr/.logins, but 'exit' seems to be
>ignored.  


The exit in your shell script is only going to get you out of that
script.  In fact, sending exit codes is one way of communicating between
scripts ( if [ script exits with a number "n" ] then ...).

A friend of mine solved that problem thusly.  He was only interested
in his own .profile, but

	trap '$HOME/.logout' 0

Which is to say, "when you receive the logoff signal, execute $HOME/.logout
first and THEN log off."  I believe there's probably a problem with this,
though:  Traps can be reset, so a user could, as I understand it, ask to
have something else happen at signal 0 , thereby negating what you're
trying to do.  Someone correct me if I'm wrong about this (I'm sure
someone will) 'cuz I'd like to know.


Kathy Vincent ------>  {ihnp4|mtune|codas|ptsfa}!bakerst!kathy
              ------>  {ihnp4|mtune|burl}!wrcola!kathy
              ------>  { favourite AT&T gateway }!wruxe!unix

dave@lsuc.uucp (David Sherman) (02/01/88)

> I would like to have my users logout via a shell script "off" rather
> than with ^d.

$ cat /usr/bin/bye
clear
echo "Bye now. A parting quote:"
/usr/games/fortune
kill -1 0
$

David Sherman
-- 
{ uunet!mnetor  pyramid!utai  decvax!utcsri  ihnp4!utzoo } !lsuc!dave

pjb@dutesta.UUCP (P.J. Brand) (02/01/88)

From article <169@mccc.UUCP>, by pjh@mccc.UUCP (Peter J. Holsberg):
> I would like to have my users logout via a shell script "off" rather
> than with ^d.  The script I wrote is very simple, but 'exit' has no
> effect.
> 
> 		echo "OFF \c:" >> /usr/.logins
> 		who am i >> /usr/.logins
> 		exit
> 
> The correct entries appear in /usr/.logins, but 'exit' seems to be
> ignored.  Typing 'exit' at the prompt does in fact do what I want --
> i.e., act as a substitute for ^d.  I see that exit(2) is a system call
> that terminates the calling process.  Oh oh!  It terminates 'off' but
> doesn't log the user off!  Rats!@!@#$%
> 
> Can anyone suggest a way (SysVr3.0) to do what I want?  Thanks.
> .
> 
> -- 

The script would work if your users would call:

	. /usr/local/off

or wherever your script is stored.
But, instead, you would probably prefer to use:

	kill -9 0

instead of the exit, to make the script work as expected.


==============================================================================
Paul Brand
Delft University of Technology         INTERNET : karel@dutesta.UUCP
Faculty of Electrical Engineering      UUCP     : ..!mcvax!dutrun!dutesta!karel

avr@mtgzz.UUCP (XMRP50000[jcm]-a.v.reed) (02/02/88)

In article <169@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
> I would like to have my users logout via a shell script "off" rather
> than with ^d.

Any sh script can kill its parent shell (in your case, the login shell)
with

kill -1 `ps -f -p $$|grep $$|cut -c16-20`

This assumes the System V ps spec, but could be adapted to any
UNIX(tm) system and any version of /bin/sh.

					Adam Reed (mtgzz!avr)

larry@jc3b21.UUCP (Lawrence F. Strickland) (02/02/88)

in article <1062@bakerst.UUCP>, kathy@bakerst.UUCP (Kathy Vincent) says:
> 
> In article <169@mccc.UUCP> pjh@mccc.UUCP (Peter J. Holsberg) writes:
>>I would like to have my users logout via a shell script "off" rather
>>than with ^d.  The script I wrote is very simple, but 'exit' has no
>>effect. ...
> 
> 	trap '$HOME/.logout' 0

As you mentioned, exit will only get you out of the script, not the
enwrapped shell.  I've always had good luck with:

	kill -9 0

in a shell script.  This is a bit rough (as it kills ALL programs associated
with the terminal, including background ones), but it has to be since a
normal kill is ignored by the shell.  I'm sure there is a better way, too...
-- 
+--------------------------------------+-- St. Petersburg Junior College --+
|        Lawrence F. Strickland        |   P.O. Box 13489                  |
| ...gatech!codas!usfvax2!jc3b21!larry |   St. Petersburg, FL 33733        |
+-(or) ...gatech!usfvax2!jc3b21!larry -+-- Phone: +1 813 341 4705 ---------+

heff@flnexus.ATT.COM (Paul K Heffner) (02/03/88)

in article <3564@mtgzz.UUCP>, avr@mtgzz.UUCP (XMRP50000[jcm]-a.v.reed) says:
> In article <169@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
>> I would like to have my users logout via a shell script "off" rather
>> than with ^d.
> Any sh script can kill its parent shell (in your case, the login shell)
  with
> kill -1 `ps -f -p $$|grep $$|cut -c16-20`

The command:

stty 0

sets your baud rate to zero, forces a hangup which causes SIGHUP to
be sent to all correspondent processes thereby 86'ing them. This
has been used with nominal success in some of the 'bye' routines
on our systems here.



Paul Heffner
AT&T Orlando

emigh@ncsugn.ncsu.edu (Ted H. Emigh) (02/04/88)

In article <144@flnexus.ATT.COM> heff@flnexus.ATT.COM (Paul K Heffner) writes:
> In article <169@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
>> I would like to have my users logout via a shell script "off" rather
>> than with ^d.

>The command:

>stty 0

>sets your baud rate to zero, forces a hangup which causes SIGHUP to
>be sent to all correspondent processes thereby 86'ing them. This
>has been used with nominal success in some of the 'bye' routines
>on our systems here.

Of course, this will only work if you want them to be disconnected, rather
than merely logout.  In particular, if you have people using su, they may
not want to be disconnected.



-- 
Ted H. Emigh, Dept. Genetics and Statistics, NCSU, Raleigh, NC
uucp:	mcnc!ncsuvx!ncsugn!emigh	internet:  emigh%ncsugn.ncsu.edu
BITNET: NEMIGH@TUCC                  @ncsuvx.ncsu.edu:emigh@ncsugn.ncsu.edu

jonl@sco.COM (Owl Of Nite) (02/12/88)

+-I seem to recall emigh@ncsugn.UUCP (Ted H. Emigh) writing:
|
| In article <144@flnexus.ATT.COM> heff@flnexus.ATT.COM (Paul K Heffner) writes:
| > In article <169@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
| >> I would like to have my users logout via a shell script "off" rather
| >> than with ^d.
| 
| >The command:
| 
| >stty 0
| 
| >sets your baud rate to zero, forces a hangup which causes SIGHUP to
| >be sent to all correspondent processes thereby 86'ing them. This
| >has been used with nominal success in some of the 'bye' routines
| >on our systems here.
| 
| Of course, this will only work if you want them to be disconnected, rather
| than merely logout.  In particular, if you have people using su, they may
| not want to be disconnected.
| 
| 
| 
| -- 
| Ted H. Emigh, Dept. Genetics and Statistics, NCSU, Raleigh, NC
| uucp:	mcnc!ncsuvx!ncsugn!emigh	internet:  emigh%ncsugn.ncsu.edu
| BITNET: NEMIGH@TUCC                  @ncsuvx.ncsu.edu:emigh@ncsugn.ncsu.edu

Also, something to be noted is that when using rlogin (at least on 4.2 and 4.3
BSD machines) and stty 0 will be completely ignored by the pseudo tty.

if the world was beige, id paint it a different color
===============================================================================
jon luini || WORK: 408-425-7222    || HOME: 408-423-2917
Work:     || jonl@sco.com          || ...!{uunet, ihnp4, ucbvax!ucscc}!sco!jonl
Evil:     || niteowl@ssyx.ucsc.edu || ...!{ucbvax}!ucscc!ssyx!niteowl
===============================================================================