1827856 Members
1516 Online
109969 Solutions
New Discussion

Re: Scripting Problem

 
SOLVED
Go to solution
Sridhar R
Regular Advisor

Scripting Problem

MY Script in HP-UX is:

#! /bin/ksh
main ()
{
tput clear
echo "\t************************** WELCOME ****************************** \n"
echo "\t\t\t 1.top\n"
echo "\t\t\t 2.bdf\n"
echo "\t\t\t 3.syslog\n"
echo "\t\t\t 4.log shipment\n"
echo "\t\t\t x.exit\n"
echo "\t***************************************************************** \n"
echo "\t Enter your choice: \c"
read input
case $input in
1) /usr/bin/top -d 1 ;;
2) /usr/bin/bdf ;;
3) /usr/bin/tail -10 /var/adm/syslog/syslog.log ;;
4) /usr/bin/ll /earch3a/edc_sb ;;
*) exit 0 ;;
esac
}
while (true)
do
main
done


The Script works fine. But upon giving exit, it exits and comes to the prompt, but what i am looking for is to get exited from the server completely. And anywhere in between the script, if i give ctrl+c, even then the prompt comes up.

I want to avoid hapenning this.

Kindly help!!

Thanks in Advance!!

Sridhar
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: Scripting Problem

If you want the script to logout of the login shell and machine, you'll need to use exec on your script: $ exec script

If you want to exit completely on control C, you need to add a trap:
trap 'echo "doing exit"; exit' 2
Sridhar R
Regular Advisor

Re: Scripting Problem

Thanks for the reply!

But i want to know where should i put these two lines in the script.

Kindly let me know!!
James R. Ferguson
Acclaimed Contributor
Solution

Re: Scripting Problem

Hi Shridar:

As Dennis noted, add a 'trap' to catch the 'Ctrl_C' signal. The 'exec scriptname' would be placed in the last line of your login profile, like:

exec /home/shridar/menu.sh

Then, as soon as you login, your script runs. When the script exits, you are also logged off the server. Your script would look like the following. Notice that I added a prompt and a read for keyboard input at the end of the 'case' execution. You want the user to be able to see your output!

#!/usr/bin/sh
trap 'echo "doing exit"; exit' 2
main ()
{
tput clear
echo "\t************************** WELCOME ****************************** \n"
echo "\t\t\t 1.top\n"
echo "\t\t\t 2.bdf\n"
echo "\t\t\t 3.syslog\n"
echo "\t\t\t 4.log shipment\n"
echo "\t\t\t x.exit\n"
echo "\t***************************************************************** \n"
echo "\t Enter your choice: \c"
read input
case $input in
1) /usr/bin/top -d 1 ;;
2) /usr/bin/bdf ;;
3) /usr/bin/tail -10 /var/adm/syslog/syslog.log ;;
4) /usr/bin/ll /earch3a/edc_sb ;;
*) exit 0 ;;
esac
echo "Press RETURN to continue"
read REPLY
}
while (true)
do
main
done

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Scripting Problem

And to clarify why the exit in a script does not logout the session, anything you run from your login shell starts a subprocess (also called a child process) and when the child finishes, control is returned back to your login shell. The exec command says in essence: replace my current shell with a shell running this script. Then when the new script finishes, the session is closed.

If you want a certain user to always run this set of commands, you can change this user's login shell in /etc/passwd to specify the script instead of /usr/bin/sh or /usr/bin/ksh. Now the script will be the login process and when complete, will close the session. This is a common way to prevent shell access and only provide a menu for restricted users.


Bill Hassell, sysadmin
Sridhar R
Regular Advisor

Re: Scripting Problem

Thanks very much to all the replies. Its working great now.

Thanks once again.

Sridhar
Sridhar R
Regular Advisor

Re: Scripting Problem


Got the solution that i expected.