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
When using <<-, you can only indent with tabs.
Fixing that error might help you solve the problemif [[ "$BRANCH" = "tomcat" && "$rc" -eq 0 ]] ...
It may be that $? after the && is actually no longer referring to your original return code.