LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to restart (https://www.linuxquestions.org/questions/linux-general-1/script-to-restart-4175461519/)

JJJCR 05-11-2013 04:08 AM

Script to restart
 
hi guys, how do I make a simple script to restart a service like:

/etc/init.d/kerio-connect restart

do I just need to key in that command in a script?

is there a way to prompt the user like:

Are you sure you want to restart?
Press 'Yes' to proceed 'No' to cancel.

Need your help guys, sorry so noob in linux scripting.

Thanks in advance. :)

arunchinnachamy 05-11-2013 05:44 AM

Code:

echo "You sure want to restart the service followed by [ENTER]:"
read answer
if  ("$year" == "yes")
    /etc/init.d/apache2 restart

The code should give you an idea how you can perform the user confirmation. This page should help.

JJJCR 05-11-2013 07:25 AM

hi, thank you so much for you reply i tried this code Ubuntu VM

but it has an error
'r':command not found
's':command not found
'x':command not found etc. etc.

Please help guys,i just tried whether the script is able to capture the keys but it does not work it throws some error.

any help is greatly appreciated.

Code:

#!/bin/bash
#This script will restart, start or stop the Kerio connect service

echo "Press the letter followed by enter, "R" to restart Kerio Connect service, "X" to stop to restart Kerio Connect service, "S" to start"

read answer

if ("$answer" == "R" || "r"); then

#/etc/init.d/kerio-connect restart

echo "You press R or r"

elif ("$answer" == "X" || "x"); then

#/etc/init.d/kerio-connect stop

echo "You press X or x"

else ("$answer" == "S" || "s"); then

#/etc/init.d/kerio-connect start
#
echo "You press S or s"

fi


suicidaleggroll 05-11-2013 10:25 AM

Quote:

Originally Posted by JJJCR (Post 4949064)
hi, thank you so much for you reply i tried this code Ubuntu VM

but it has an error
'r':command not found
's':command not found
'x':command not found etc. etc.

That's because arunchinnachamy's post has the wrong syntax.

Code:

echo "enter stuff"
read answer
if [[ "$answer" == "yes" ]]; then
  echo yes
else
  echo something else
fi


JJJCR 05-11-2013 08:05 PM

hi guys, thank you so much for your help i came up with this one:

Code:


#!/bin/bash

clear

echo "Press S to start X to stop R to restart and press [enter]"

read answer

if [[ $answer == "S" || $answer == "s" ]]; then
  echo "You press S"
# put the command here to be executed
elif [[ $answer == "X" || $answer == "x" ]]; then
  echo "You press X"
# put the command here to be executed
elif [[ $answer == "R" || $answer == "r" ]]; then
  echo "You press R"
# put the command here to be executed
else
  echo "Unrecognize choice"
fi



All times are GMT -5. The time now is 10:26 PM.