Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

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.
0
dmwynne
Asked:
dmwynne
  • 3
  • 2
  • 2
2 Solutions
 
arnoldCommented:
#!/bin/bash

echo -n "Select to see
1 Do you want to see service status
2. Do you want to stop, start, restart the service
q: to exit
?: "
while read a; do

if [ "$a" = "q" ] ; then
   break;

elif [ "$a" = "1" ] ; then
     echo "service status is: "
elif [ "$a" = "2" ] ; then
     echo "other sub menu should appear to give you the options:"
else
     echo "You made a choice that is invalid."
     echo -n "Select to see
1 Do you want to see service status
2. Do you want to stop, start, restart the service
q: to exit
?: "
fi
done

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
bash actually has a select command which can do the above rather more easily. I realize you're just starting with bash, but I advise you might as well start properly.
For a quick description of how to use select, type help select at the bash prompt. You'll see straight away how it already handles verification of the input, so you don't need to code for that (if you enter an invalid choice, you are prompted again).
select keeps prompting you for menu items until you select an item with an implementation that ends with a break command. I've assumed that in your top-level menu, you want to keep going until you select exit but level 2 is always single-shot.
#!/bin/bash

# Because we'll be testing against the choice strings,
# put them in variables so we only have to define them once.
# Use meaningful names which are still short,
# e.g. "m1a1" for Menu 1, Alternative 1.

m1a1="see service status"
m1a2="stop, start or restart the service"
m1a3="exit this menu"
m2a1="stop service"
m2a2="start service"
m2a3="restart service"

# select prompts with PS3 which by default is "#?" (i.e. "which number").
# We can make it a little more user-friendly,
PS3="Enter the number of your choice> "

# After displaying the sub_menu,
# you really want to re-display the top_level menu.
# Hence this loop
while true
do

  echo "Here's what you can do:"

# We must use quotes because the variables expand to strings with spaces in them.
# This applies from now on.
select i in "$m1a1" "$m1a2" "$m1a3"
  do
  if [ "$i" = "$m1a1" ]; then

# This is stub code.
# In case you didn't know,
# a stub is where you display a message
# because you haven't written the actual code yet.
    echo service status stub

  fi
  if [ "$i" = "$m1a2" ]; then
    select i in "$m2a1" "$m2a2" "$m2a3"
    do
      if [ "$i" = "$m2a1" ]; then
        echo service stop stub
        break
      elif [ "$i" = "$m2a2" ]; then
        echo service start stub
        break
      elif [ "$i" = "$m2a3" ]; then
        echo service restart stub
        break
      fi
    done

# This is a little tricky:
# we want the outer menu to be repeated now,
# but if we took no special action then all we would get is another prompt
# (for a choice from the outer menu).
# That would be really confusing,
# so break from the outer menu here -
# it will be re-displayed becuse of the while loop.
    echo    # Output a blank line
    break
  fi

# This is a concise alternative to using the if command.
# It's particularly suitable when the command to be executed is a 1 -liner,
# although you can do multiple lines using curly braces,
# and many project managers prefer to have scripts written that way.
# Again I'm thinking of what you'd be well-advised to learn,
# rather than sticking to the constraints of your question.
# The "&&" says to obey the 2nd command if the first command succeeds,
# i.e. if the test passes.
# "&&" along with its partner "||" has many other uses -
# post back if you'd like me to describe a few.
[ "$i" = "$m1a3" ] && exit

  done
done

Open in new window

A bit more than you asked for, but considering the extra functionality it's only gone from21 lines in the preceding post to 35 (after removing comments and blank lines).
0
 
Duncan RoeSoftware DeveloperCommented:
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

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
dmwynneAuthor Commented:
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?
0
 
arnoldCommented:
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.
0
 
dmwynneAuthor Commented:
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
0
 
arnoldCommented:
Look at any if the startup scripts in /etc/init.d
 
There are many learn bash scripting sites/examples.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now