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?
    – Krishna
    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
    – Inian
    Commented Jun 3, 2016 at 7:34
  • Is "$BRANCH" equals "tomcat" all the time and what is the reason behind taking variable tomcat?
    – Krishna
    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
1
  • No problem, glad to help!
    – Will
    Commented Jun 3, 2016 at 7:51
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
  • Oops, sorry, we worked on the same answer at the same time :)
    – Will
    Commented Jun 3, 2016 at 7:47
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 $?!.

Not the answer you're looking for? Browse other questions tagged or ask your own question.