We help IT Professionals succeed at work.

exit 0 in shell script is exiting from session

Umavmishra
Umavmishra asked
on
1,156 Views
Last Modified: 2013-12-26
Hi All,

I have a menu driven script which exits on a switch case statement as below


Please let me know what is the problem with the exit statement given in the code snippet. When I select option 5 on the menu, I get logged off the linux session
while :
do
 clear
 echo "                         MAIN M E N U"
 echo "         1. Get files"
 echo "         2. Run Application"
 echo "         3. Delete files "
 echo "         4. Do ALL of the above"
 echo "         5. Exit"
 echo "                                                                 "
 echo "                                                                 "
 echo -n "Please enter option [1 - 5]"
 read opt
 case ${opt} in
  1) echo "Getting files ";
     get_files
        ;;
  2) echo "Executing Application";
     executeapp
        ;;
  3) echo "Deleting files older than 2 weeks in directory";
     delete_files;
        ;;
  4) echo "Doing everything!";
        get_files;
        executeapp;
        delete_files;
        ;;
  5) exit 0;
        ;;
  *) echo "$opt is an invaild option. Please select option between 1-5 only";
     echo "Press [enter] key to continue. . .";
     read enterKey
        ;;
esac
done

Open in new window

Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
how do you invoke the script, and what do you want to happen when you select option 5 on the menu?
CERTIFIED EXPERT
Top Expert 2007

Commented:
If you get logged off when you select option 5, it means it is most likely invoked from your .profile

with something like

exec /path/to/menu.sh

The exec will overlay the current process (in this case the login) with menu.sh, so that when you exit the menu, you are immediately logged off.

You need to change your .profile to just call the menu without the exec, eg:

/path/to/menu.sh

Author

Commented:
my .profile does not have any reference to this script.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
how do you invoke the script?

Author

Commented:
. ./script.sh
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
typo
*look=loop

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.