1

I'm new to the Bash scripting. I added the if .. else in the Bash script. My script is for making Docker-containers if the container doesn't exist, and creating a new container if it does exist. But the script never runs the else block; it always run the if block.

My script:

#!/bin/bash
tomcat=$(ssh [email protected] sudo docker ps -a | grep -w 'my-tomcat')
echo "$?"

if [ "$BRANCH" = "tomcat" ] && [ "$?" -eq 0 ]; then
        ssh [email protected] /bin/sh <<-EOF
                { cd tomcat/ && sudo docker stop my-tomcat && sudo docker rm my-tomcat; } \
                && { sudo docker build -t tomcat .; } \
                && { sudo docker run -h tomcat --name my-tomcat -itd -p 2013:8080 tomcat; } \
                && { sudo docker start my-tomcat; }
        EOF
else
        ssh [email protected] /bin/sh <<-EOF
                { cd tomcat/ && sudo docker build -t tomcat .; } \
                && { sudo docker run -h tomcat --name my-tomcat -itd -p 2013:8080 tomcat; } \
                && { sudo docker start my-tomcat; } 
        EOF
fi
5
  • Is "$BRANCH" equals "tomcat" all the time? Commented Jun 3, 2016 at 7:34
  • 1
    Copy pasted your script in shellcheck.net and it threw an ugly error When using <<-, you can only indent with tabs. Fixing that error might help you solve the problem Commented Jun 3, 2016 at 7:34
  • Is "$BRANCH" equals "tomcat" all the time and what is the reason behind taking variable tomcat? Commented Jun 3, 2016 at 7:40
  • I would try assigning the $? to a variable (rc) before using it in the if and write if [[ "$BRANCH" = "tomcat" && "$rc" -eq 0 ]] ... It may be that $? after the && is actually no longer referring to your original return code. Commented Jun 3, 2016 at 7:42
  • everybody thanks it's working now Commented Jun 3, 2016 at 7:53

3 Answers 3

2

Once you run:

echo "$?"

if [ "$BRANCH" = "tomcat" ] && [ "$?" -eq 0 ]; then

In your if statement, "$?" will always be 0, because it will represent the return value of [ "$BRANCH" = "tomcat" ]. And even if it didn't, it would represent the return value of the echo.

Do this instead to preserve the exit code:

tomcat="$(ssh [email protected] sudo docker ps -a | grep -w 'my-tomcat')"
tomcat_status="$?"

echo "$tomcat_status"

if [ "$BRANCH" = "tomcat" ] && [ "$tomcat_status" -eq 0 ]; then
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad to help!
2
[ .. ] && [ "$?" -eq 0 ]
#            ^ This will always be 0, since it's referring to the previous 
#              commands ([ .. ]) exit code

Store the exit code from earlier:

tomcat=$(ssh [email protected] sudo docker ps -a | grep -w 'my-tomcat')
tomcat_exit=$?

And what is $BRANCH? And tomcat?

if [ "$BRANCH" = "$tomcat" ] && [ "$tomcat_exit" -eq 0 ]; then
#                 ^ Is this a constant "tomcat" or variable "$tomcat"?

1 Comment

Oops, sorry, we worked on the same answer at the same time :)
1

To aid debugging, type set -x before the if-statement, and set +x after the fi statement.

This enables execution tracing, and you can see the substitution for $branch and $?!.

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.