2

I've written a script that starts, stops and sends status of Apache, with messages dependent on the output of the commands.

I have most of it correct, but my errors are not printing out correctly. In other words, even if I do not have Apache loaded, "stopping" it still shows a successful message.

I need help getting my error messages to print when necessary.

#!/bin/bash

echo -e "\e[1;30mApache Web Server Control Script\e[0m"
echo
echo "Enter the operation number to perform (1-4): "
echo " 1 - Start the httpd server"
echo " 2 - Restart the httpd server"
echo " 3 - Stop the httpd server"
echo " 4 - Check httpd server status"
echo
echo -n "===> "
read NUMBER

EXITSTATUS=$?
echo
if [ $NUMBER -eq "1" ]; then
    systemctl start httpd
    if [ $EXITSTATUS -eq "0" ]; then
        echo -e "\e[1;32mThe return value of the command 'systemctl 
        start httpd' was 0.\e[0m"
        echo -e "\e[1;32mThe Apache web server was successfully 
        started.\e[0m"
    else
        echo -e "\e[1;31mThe return value of the command 'systemctl 
        start httpd' was 5.\e[0m"
    echo -e "\e[1;31mThe Apache web server was not successfully 
    started.\e[0m"
    fi  
fi 

if [ $NUMBER -eq "2" ]; then
    systemctl restart httpd
    if [ $EXITSTATUS -eq "0" ]; then
        echo -e "\e[1;32mThe return value of the command 'systemctl 
        restart httpd' was 0.\e[0m"
        echo -e "\e[1;32mThe Apache web server was successfully 
        restarted.\e[0m"
    else
        echo -e "\e[1;31mThe return value of the command 'systemctl 
        restart httpd' was 5.\e[0m"
        echo -e "\e[1;31mThe Apache web server was not successfully 
        restarted.\e[0m"
    fi  
fi

if [ $NUMBER -eq "3" ]; then
    systemctl stop httpd
    if [ $EXITSTATUS -eq "0" ]; then
        echo -e "\e[1;32mThe return value of the command 'systemctl 
        stop httpd' was 0.\e[0m"
        echo -e "\e[1;32mThe Apache web server was successfully 
        stopped\e[0m."
    else
        echo -e "\e[1;31mThe return value of the command 'systemctl 
        stop httpd' was 5.\e[0m"
        echo -e "\e[0;31mThe Apache web server was successfully 
        stopped.\e[0m"
    fi  
fi

if [ $NUMBER -eq "4" ]; then
    systemctl status httpd
    if [ $EXITSTATUS -eq "0" ]; then
        msg=$(systemctl status httpd)
    else
        echo -e "\e[1;31mThe Apache web server is not currently 
        running.\e[0m"
        echo $(msg)
    fi  
fi

if [[ $NUMBER != [1-4] ]]; then
    echo -e "\e[1;31mPlease select a valid choice: Exiting.\e[0m"
fi
exit 0
1
  • 1
    Trying to guess your intent here... the way you wrote EXITSTATUS=$? in your code, and then used $EXITSTATUS where you should have used $? makes me think you meant EXITSTATUS as an alias for $?, so you can use $EXITSTATUS as a substitute for $?. That is not how it works (as you found out). Instead, you are simply creating a variable, EXITSTATUS, and you are assigning to it the then-current value of $?, which is 0 (the read command succeeds). As the code progresses, the value of $? changes after each command, but the value of EXITSTATUS does not change. It's not an alias! Commented Feb 22, 2018 at 16:39

2 Answers 2

2

The variable EXITSTATUS doesn't contain the exit code of the systemctl calls, but that of the read command. You could rewrite it either as

systemctl start httpd
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
[...]

or more simply as

systemctl start httpd
if [ $? -eq 0 ]; then
[...]

Storing the value of $? in a variable is only necessary if you either want to use it afterwards in another place (e. g. as exit code of your own script), or have to make other calls before branching on the value.

Sign up to request clarification or add additional context in comments.

6 Comments

Using $? at all is completely unnecessary in this case. To find out whether a command cmd succeeded, use if cmd directly.
That may be the case, but I prefer this for readability (think of longer commands with many switches), and wanted to propose it as alternative.
I know the exit status of an error here would be 5, and I think that's what we are supposed to be learning; how to return specific messages for specific errors. Even when deleting apache and then running the script, it still says successful.
@Hebruiser If you think this is an important information then you should incorporate it into the text of your question. However, I don't see what this should have to do with your issue, or my answer.
@Murphy Thank you. Just using " $? " worked perfectly.
|
1

You're not setting your variable $EXITSTATUS after running the commands, so it maintains its original value (the exit status of read NUMBER).

Since you only care about whether the command succeeded or not, better would be to avoid using it entirely and change the conditions to e.g.:

if systemctl restart httpd; then
  # it was successful ($? would be 0)
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.