LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2010, 12:22 AM   #1
m4manoze
LQ Newbie
 
Registered: Feb 2010
Posts: 11

Rep: Reputation: 0
regarding exit bash script


Dear Experts,

I have made a simple bash script through which i can add, del, edit user from certain file using different CASE variables. like
case
1. adduser
2. del user
3. edit user

now i want to add a exit CASE like
1. adduser
2. del user
3. edit user
4. exit

now i want to make a script such a way that, if user input is 4 then only script quit. I used with exit function also but it didn't work. if user press ENTER or other keys then also it quit the program.

Please help me.


Regards,
Manoj
 
Old 03-07-2010, 12:43 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Regards "exit function also but it didn't work", exit is normally a shell builtin. Have you written an exit function to replace it?

Regards "if user press ENTER or other keys then also it quit the program", do you mean that you want the script to exit if the user enters anything other than 1, 2 or 3?

Can you post your script, preferably in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 03-07-2010, 01:17 AM   #3
m4manoze
LQ Newbie
 
Registered: Feb 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Code:
#!/bin/bash

function function1
{
echo "you are in user add mode" ###I have certain script run for this
}

function function2
{
echo "you are in user del mode" ###I have certain script run for this
}

function function3
{
echo "you are in show user mode"###I have certain script run for this
}

function function4
{
echo "you are in edit user mode"###I have certain script run for this
}

function function9
{
echo "you are in script exit mode"### for this i need help
}

clear
function menu
{
clear
echo "PLEASE ENTER:"
echo "============="
echo "1. Add User In Expire:"
echo "2. Delet User From Expire:"
echo "3. Show User Expire:"
echo "4. Edit User In Expire:"
echo ""
echo "--------"
echo "Exit (9)"
echo "--------"

read case;

case $case in
1) function1
;;
2) function2
;;
3) function3
;;
4) function4
;;
9) exit
;;


esac
}
menu

###need help section:

I only want program to exit only if user press key "9" if user press enter or what ever other key program should be running. Whenever user press key "9" then only program should exit.


Quote:
Originally Posted by catkin View Post
Regards "exit function also but it didn't work", exit is normally a shell builtin. Have you written an exit function to replace it?

Regards "if user press ENTER or other keys then also it quit the program", do you mean that you want the script to exit if the user enters anything other than 1, 2 or 3?

Can you post your script, preferably in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 03-07-2010, 02:06 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Something like this should do it
Code:
#clear
function menu
{
clear
echo "PLEASE ENTER:"
echo "============="
echo "1. Add User In Expire:"
echo "2. Delet User From Expire:"
echo "3. Show User Expire:"
echo "4. Edit User In Expire:"
echo ""
echo "--------"
echo "Exit (9)"
echo "--------"

read case;

case $case in
1) function1
;;
2) function2
;;
3) function3
;;
4) function4
;;
9) exit
;;
*) 
echo "Invalid choice, '$case'" >&2
echo 'Press enter to continue'
read


esac
}

while true
do
    menu
done
 
1 members found this post helpful.
Old 03-07-2010, 06:41 AM   #5
m4manoze
LQ Newbie
 
Registered: Feb 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Dear Catkin,

wowowowo...wow.... it worked. Thank you Catkin!!!!!!!!!!!

But confused how it works? i really want to know what does it do? It will be really helpful to me.

Regards,
Manoj

Quote:
Originally Posted by catkin View Post
Something like this should do it
Code:
#clear
function menu
{
clear
echo "PLEASE ENTER:"
echo "============="
echo "1. Add User In Expire:"
echo "2. Delet User From Expire:"
echo "3. Show User Expire:"
echo "4. Edit User In Expire:"
echo ""
echo "--------"
echo "Exit (9)"
echo "--------"

read case;

case $case in
1) function1
;;
2) function2
;;
3) function3
;;
4) function4
;;
9) exit
;;
*) 
echo "Invalid choice, '$case'" >&2
echo 'Press enter to continue'
read


esac
}

while true
do
    menu
done
 
Old 03-07-2010, 08:23 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by m4manoze View Post
But confused how it works? i really want to know what does it do?j
Hello Manoj

You wrote "if user press enter or what ever other key program should be running. Whenever user press key "9" then only program should exit". That implied if the user pressed anything but 9 you wanted to start over and that required a loop. Hence
Code:
while true
do
    menu
done
That's an infinite loop, it keeps on going as long as true is, er, true. I keeps on running the menu function, displaying the menu, reading the user input and acting accordingly.

The only scripted way to stop that loop is when the user enters 9 and the case statement calls exit.

It's a lot easier to see what's going on and to debug if you don't use the clear command.

You can also get a good idea what is going on by putting set -xv in the script. Just before while true would be a good place.
 
Old 03-08-2010, 06:19 AM   #7
m4manoze
LQ Newbie
 
Registered: Feb 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Hello Manoj

You wrote "if user press enter or what ever other key program should be running. Whenever user press key "9" then only program should exit". That implied if the user pressed anything but 9 you wanted to start over and that required a loop. Hence
Code:
while true
do
    menu
done
That's an infinite loop, it keeps on going as long as true is, er, true. I keeps on running the menu function, displaying the menu, reading the user input and acting accordingly.

The only scripted way to stop that loop is when the user enters 9 and the case statement calls exit.

It's a lot easier to see what's going on and to debug if you don't use the clear command.

You can also get a good idea what is going on by putting set -xv in the script. Just before while true would be a good place.
Thank you catkin for your helpful information.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exit a BASH script Pete-Ratcliffe Programming 2 09-03-2009 10:55 AM
How to exit BASH with a BASH script bert07 Programming 4 09-03-2009 03:19 AM
what is the script which runs at the exit bash shell supersubu123 Linux - General 6 01-24-2007 01:25 PM
Bash Script- command on exit PDock Programming 6 12-23-2005 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration