2

I am new to bash. My issue is, when I run this script it only does the first command and does not finish the rest of them. Is there a way to make bash execute all of these commands from one startup script ?

I have a startup script as follows:

#! /bin/bash
cd dockerfiles/nginx
exec docker build -t nginx .
wait
cd ../percona
exec docker build -t percona .
wait
cd ../memcache
exec docker build -t memcache .
wait
cd ../sphinx
exec docker build -t sphinx .
wait
cd ..
exec docker build -t php .
wait
cd ..
exec docker-compose up
0

1 Answer 1

7

The exec command tells bash to do the same thing done by the execve() syscall -- to replace its own process table entry with the program being run.

By nature, this operation means that the script is no longer running when complete. If you don't want that, just take out the exec prefixes.

(Also, there's absolutely no reason to use wait here at all; wait only has any effect whatever if a process is backgrounded, and you aren't backgrounding anything).


That said, you might consider using subshells to scope the cd operations -- and then exec will replace only the subshell's process, not the parent; it's not strictly necessary even in that case, but is a slight performance optimization (likewise, replacing #!/bin/bash with #!/bin/sh is a performance improvement on platforms providing a lightweight /bin/sh since you aren't using any bash-only features):

#!/bin/sh
(cd dockerfiles/nginx && exec docker build -t nginx .)
(cd dockerfiles/percona && exec docker build -t percona .)
(cd dockerfiles/memcache && exec docker build -t memcache .)
(cd dockerfiles/sphinx && exec docker build -t sphinx .)
(cd dockerfiles && exec docker build -t php .)
docker-compose up
Sign up to request clarification or add additional context in comments.

4 Comments

You might see the edits I added, which let you get rid of all the cd .. mess (and actually do make profitable use of exec, albeit as a completely optional performance enhancement).
I do see your edits, and have changed my script to match them. This is far better than what I had. Thanks for the help.
Also note that this can be simplified with a loop: for f in nginx percona memcache sphinx; do ( cd dockerfiles/$f && exec docker build -t $f . ) ; done This makes it easier for a human reader to see the similarities in the commands.
@WilliamPursell, indeed; if we didn't have php breaking the pattern, I would have suggested that. (Though I'd tend to suggest using quotes on expansions in that code, just by way of demonstrating good practices)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.