[Tutor] python scripting and unix

dman dsh8290@rit.edu
Tue, 29 Jan 2002 16:23:17 -0500


On Mon, Jan 28, 2002 at 07:47:05PM -0500, Christine Wilson wrote:
| I am writing a python script set to execute upon login to my unix account.  
| The user is prompted for further information - if the answer is correct, 
| they may continue.  If the answer is wrong (meaning that it is not me) I 
| want the session to exit/logout.
| 
| How can I do this?  I'm just becoming familiar with Python and scripting.  
| Any ideas?
| 
| The script is executing before the user actually is prompted with the Unix 
| prompt.

In your script (.bash_login or whatever it is for your shell) use

~~~~~~~~~~
./path/to/your_script || exit
~~~~~~~~~~

If your script exits with a non-zero (success) exit status then the
command after the "or" ('||') will execute.


| ----------------------------------------------------------------------
| #!/usr/bin/env python
| 
| import os
| os.system("clear")

# the above line isn't important, really
# probably better to stick it in your login file instead anyways

  import sys

| from sudialg import *
| if ask_who():   #gets user input for first answer, returns 1 if correct
|  #continues
      sys.exit( 0 )
| 
| else:
      sys.exit( 1 )
|   exit   #I want the telnet session to end with NO access to my account
|            #but this is just exiting the script


FWIW you can probably just stick it in your login file as

read -p "the question" FOO 
if [ "$FOO" != "the magic answer" ] ; then
    exit || logout
fi


('man bash' and look through the relevant parts for a very terse
explanation of shell scripting)

HTH,
-D

-- 

The teaching of the wise is a fountain of life,
turning a man from the snares of death.
        Proverbs 13:14