Visit The New, Modern Unix Linux Community


Help with trap in terminal locking pgm


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with trap in terminal locking pgm
# 1  
Question Help with trap in terminal locking pgm

Hii ,
i would like to know how to finish the program...
the pgm given below works but it doesn't satisfy the condition that it should not terminate process when yu press ctrl-D , or ctrl-Z .
how to use trap command for this??
please help....

Q.Shell scrip to implement terminal locking .It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for password confirmation(to retype password).If a match occurs,it must lock the terminal and prompt for the password.if the proper password is entered,the terminal must be unlocked. Note the script must be written to disregard BREAK,control -D etc. No time limit need to be implemented for the lock duration.

echo "\nEnter the password:\c"
stty -echo
read pass1
echo "\nRetypr password"
read pass2
if [ $pass1 = $pass2 ] ; then
tput clear
echo "\nThe terminal is locked\n"
echo "\nEnter password to unlock:"
read pass3
while [ $pass1 != $pass3 ]
do
echo "\nEnter password to unlock"
read pass3
done
else
echo "\nTerminal could not be locked"
fi
stty echo
# 2  
You put a line like:
Code:
trap "echo Signal Caught" 0 1 2 3 6 15 20

At the top of your script, the signal number for <ctrl>+<z> is 20, <ctrl>+<d> seems to just cause the read command to see an "enter" and return an empty string which simply cause your script to prompt the user to re-enter the password.
What you do in response to the trap signal is of course up to you, having just "" so that nothing happens my be what you want.
On my Ubuntu install the signal numbers and their meanings are in /usr/include/bits/signum.h.
# 3  
clrl+d problem

thanks... it doesnt terminate when i enter <ctrl>+<z> ... but when i enter <ctrl>+<d> it terminates with this msg
"[: 21: !=: argument expected"
so which number should i add for trp so that it doesnt come out when i enter ctrl+d...??

Code:
anusha@anusha-desktop:~$ sh 7a.sh

Enter the password:
Retypr password
















The terminal is locked


Enter password to unlock:
[: 21: !=: argument expected

anusha@anusha-desktop:~$

# 4  
I don't believe you can trap <ctrl>+<d>, but the solution is to allow your script to handle empty responses thus:
Change:
Code:
if [ $pass1 = $pass2 ] ; then

to
Code:
if [ "${pass1}" = "${pass2}" ] ; then

and change:
Code:
while [ $pass1 != $pass3 ]

to:
Code:
while [ "${pass1}" != "${pass3}" ]

Then if ${pass1} or ${pass2} or ${pass3} are empty they will be an empty string rather than a null value. The braces are not essential but are good scripting practice.
# 5  
Locking a terminal is an interesting question which has bugged me too.

Ctrl-D is not a signal, it just closes stdin. It's also a function of the terminal, hence controlled by stty. I'm not sure it even can be disabled. Not even 'su -' can ignore ctrl-D and it's the one thing that can ignore everything else... IGNOREEOF=-1 will stop the terminal from quitting from it at least.

If you're changing the terminal settings it'd be wise to save/restore them since they're global to the terminal.

Code:
TTYSAVE=`stty --save`

...

stty $TTYSAVE

-----Post Update-----

Setting the EOF char to something impossible may work. 'stty eof 255'

Last edited by Corona688; 06-04-2009 at 07:30 PM..
# 6  
thThank youTonyFullerM

Thank youTonyFullerMalv.... pgm is working now ....
thank you Corona688 for your information....
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #556
Difficulty: Easy
Making mistakes in programming is inevitable.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot get terminal application to launch with a graphical launcher when successful in terminal

I have been having an extremely annoying problem. For the record, I am relatively new at this. I've only been working with unix-based OS's for roughly two years, mostly Xubuntu and some Kali. I am pretty familiar with the BASH language, as that's the default shell for debian. Now, I've made this... (16 Replies)
Discussion started by: Huitzilopochtli
16 Replies

2. Shell Programming and Scripting

Simpler crontab entry to execute pgm on last day of the month

The following bash command line works for the last day of the month. Test by replacing the 1 with tomorrows day of month number && echo "Day before tomorrow"Can it be used within crontab? As * * 28-31 * * && echo "Today ls last day of month" >>/tmp/crontabtestI tried to test crontab with... (1 Reply)
Discussion started by: lsatenstein
1 Replies

3. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

4. Programming

File operations in C pgm

i am reading and writing to a a file in C language. the input file is described as follows 111 aaa descr1 222 bbb descr2 333 ccc <SPACE> {6 spaces are left after ccc i.e in 3rd column} 444 ddd descr4 when i read and write to a file, the space is not coming in the output file.... (8 Replies)
Discussion started by: vkca
8 Replies

5. UNIX for Advanced & Expert Users

how to check port already used in pcap pgm

hi, I am writing one packet receiving program using libpcap library. Now, I want to check port is already using or not. how to check in receiver program.. If normal program,using bind return value we can able to check the port is already using or not. but, in pcap program how can i... (1 Reply)
Discussion started by: ram.sj
1 Replies

6. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

7. UNIX for Dummies Questions & Answers

Which pgm write into a file ?

Hi! I'm looking for a command/script to be able to get the program who write into a file. I've a logfile who grow up but I don't find which program write on it :eek: thanks for your support&help. .fr. (2 Replies)
Discussion started by: Franzlebord
2 Replies

8. Programming

Pgm Output is Interleaved with other pgms

Hi I have a shared object which is called from a third party tool. In my .so, I am using 'freopen(mylogfilename,"at",stderr)'. It is logging all my program output and it is also writing warning messages from the third party daemons also. How can I avoid these unwanted messages... (1 Reply)
Discussion started by: axes
1 Replies

9. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies

10. Shell Programming and Scripting

Prompt User for Pgm Output Destination

SCO Open Server 5 A program that I am working with outputs data to a system or printer with the traditional output > lp This output line along with some other job output formatting information is stored as a defined output within the program so that the average user scrolls the list of... (0 Replies)
Discussion started by: termite
0 Replies

Featured Tech Videos