Link to home
Start Free TrialLog in
Avatar of dmwynne
dmwynneFlag for United States of America

asked on

Simple bash script to see status and restart a service using if then

I am wondering if someone could post an example of an interactive bash script that prompts the user with a menu with 2 choices:

1.  Do you want to see service status
2.  Do you want to stop, start, restart service

If 1. show status, if 2 go to another menu.  The 2nd menu would give options for either stopping, starting, or restarting the service.

I know there are many ways to do this but I am trying to understand if this can be done using just if / then statements and if you could post the code it would help me understand as I am just starting to learn about bash scripts.
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In answer to your original question can this be done using just if / then statements I would have to say the answer is no. Restricting yourself to only a few statements can make things very hard, and you often can't get what you really want.
By the way, here's some output from my previous post
22:36:28$ ./ee102.sh 
Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 2
1) stop service
2) start service
3) restart service
Enter the number of your choice> 4
Enter the number of your choice> 3
service restart stub

Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 3
22:46:31$ ./ee102.sh 
Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 1
service status stub
Enter the number of your choice> 2
1) stop service
2) start service
3) restart service
Enter the number of your choice> 1
service stop stub

Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 2
1) stop service
2) start service
3) restart service
Enter the number of your choice> 2
service start stub

Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 2
1) stop service
2) start service
3) restart service
Enter the number of your choice> 3
service restart stub

Here's what you can do:
1) see service status                  3) exit this menu
2) stop, start or restart the service
Enter the number of your choice> 3
22:57:17$ 

Open in new window

Avatar of dmwynne

ASKER

So I ran the script and selected option 2 and it gave me "other sub menu should appear to give you the options:" line.  Shouldn't it display option 2.  Do you want to stop...

Also how do I get the command to run when they select option 1.  So the user picks 1 to see service status, how do I actually put the command service sshs status or something in the script?
The script was an example and not a functional one.
The detail on what to do when an option is selected requires a process. i.e. if the service is running, prompt to stop/restart
if the service is stopped, option to start
this is why the selection of option 2 would in general require a sub-menu display
The current status is X
what do you want to do:
1. st

etc.

For whom are you creating this script/process?  Usually service management can only be done by those who have elevated or root rights.
Avatar of dmwynne

ASKER

Arnold - thank you.  This is a script for me to use on a Centos box I admin.  This was really an exercise for me to learn something that I was having trouble grasping, being new to scipting.  I know Linux well enought to get a box up and running and to perform a function like monitor with nagios or backup machines but want to expand my knowledge to scripting.  Thanks for your help.

duncan_roe - thanks.  I really appreciate you providing the detailed answer you did.  I am going to split points because you both helped me very much.

Thanks
Look at any if the startup scripts in /etc/init.d
 
There are many learn bash scripting sites/examples.