Skip to content

Commit 6be2096

Browse files
Add tutorial files.
1 parent 95a581d commit 6be2096

14 files changed

+265
-0
lines changed

0-reset-tutorial.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
echo
8+
echo '========================= WARNING ========================'
9+
echo 'If you made changes to the files in the `bash-for-devops`'
10+
echo 'directory then your changes will be lost if you continue.'
11+
echo 'Any new files you added to the directory will be deleted.'
12+
echo '=========================================================='
13+
echo
14+
read -p "Continue? (y/n): " -n 1 -r response
15+
echo
16+
17+
case "${response}" in
18+
y|Y)
19+
echo
20+
echo '========================== INFO =========================='
21+
echo 'You will see a `No such image` error message below if the'
22+
echo '`bash-for-devops` Docker image has not yet been created.'
23+
echo 'This error is expected and can be ignored.'
24+
echo '=========================================================='
25+
echo
26+
27+
unset file_name # script must be run using `source` for this to work
28+
29+
image_name='bash-for-devops'
30+
31+
docker image remove \
32+
--force \
33+
${image_name} || true # suppress error
34+
35+
echo
36+
37+
git reset --hard HEAD
38+
39+
echo
40+
;;
41+
*)
42+
echo
43+
echo 'Action cancelled'
44+
echo
45+
;;
46+
esac
47+
48+
# As this script will be run using `source 0-reset-tutorial.sh` the set
49+
# commands on lines 3-5 will be executed in the shell where the examples
50+
# are being run. As we want the examples to be runnning under default
51+
# Bash configuration it's necessary to reset to the default shell
52+
# attributes here so the examples will work correctly.
53+
set +o errexit
54+
set +o nounset
55+
set +o pipefail

1-what-went-wrong.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
image_name='bash-for-devops'
4+
5+
docker build \
6+
--file Dockerfile \
7+
--tag ${image_name} \
8+
.
9+
10+
docker run \
11+
--init \
12+
--name ${image_name} \
13+
--rm \
14+
--tty \
15+
${image_name}

2-execution-trace.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
set -o xtrace
4+
5+
image_name='bash-for-devops'
6+
7+
docker build \
8+
--file Dockerfile \
9+
--tag ${image_name} \
10+
.
11+
12+
docker run \
13+
--init \
14+
--name ${image_name} \
15+
--rm \
16+
--tty \
17+
${image_name}

3-error-exit.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o xtrace
5+
6+
image_name='bash-for-devops'
7+
8+
docker build \
9+
--file Dockerfile \
10+
--tag ${image_name} \
11+
.
12+
13+
docker run \
14+
--init \
15+
--name ${image_name} \
16+
--rm \
17+
--tty \
18+
${image_name}

4-we-can-build-anything.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o xtrace
5+
6+
docker_file="${1}"
7+
image_name='bash-for-devops'
8+
9+
docker build \
10+
--file ${docker_file} \
11+
--tag ${image_name} \
12+
.
13+
14+
docker run \
15+
--init \
16+
--name ${image_name} \
17+
--rm \
18+
--tty \
19+
${image_name}

5-no-unset.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o xtrace
6+
7+
docker_file="${1}"
8+
image_name='bash-for-devops'
9+
10+
docker build \
11+
--file ${docker_file} \
12+
--tag ${image_name} \
13+
.
14+
15+
docker run \
16+
--init \
17+
--name ${image_name} \
18+
--rm \
19+
--tty \
20+
${image_name}

6-generate-file-list.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o xtrace
6+
7+
docker_file="${1}"
8+
image_name='bash-for-devops'
9+
10+
ls -Y | grep '.sh$' | awk '{printf " --> %s\n", $1}' > file-list.txt
11+
12+
docker build \
13+
--file ${docker_file} \
14+
--tag ${image_name} \
15+
.
16+
17+
docker run \
18+
--init \
19+
--name ${image_name} \
20+
--rm \
21+
--tty \
22+
${image_name}

7-pipe-fail.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
set -o xtrace
7+
8+
docker_file="${1}"
9+
image_name='bash-for-devops'
10+
11+
ls -Y | grep '.sh$' | awk '{printf " --> %s\n", $1}' > file-list.txt
12+
13+
docker build \
14+
--file ${docker_file} \
15+
--tag ${image_name} \
16+
.
17+
18+
docker run \
19+
--init \
20+
--name ${image_name} \
21+
--rm \
22+
--tty \
23+
${image_name}

8-all-the-things.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
set -o xtrace
7+
8+
docker_file="${1}"
9+
image_name='bash-for-devops'
10+
11+
ls | grep '.sh$' | awk '{printf " --> %s\n", $1}' > file-list.txt
12+
13+
docker build \
14+
--file ${docker_file} \
15+
--tag ${image_name} \
16+
.
17+
18+
docker run \
19+
--init \
20+
--name ${image_name} \
21+
--rm \
22+
--tty \
23+
${image_name}

Dockerfile-build

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use the Alpine base image as the basis for the container
2+
FROM alpine
3+
4+
# Set the working directory for Docker to run the commands that follow
5+
WORKDIR /opt
6+
7+
# When the container is run, Docker will concatenate the following list of strings with a space between each string and execute the resulting command string:
8+
# cat header.txt file-list.txt footer.txt
9+
CMD ["cat", "header.txt", "file-list.txt", "footer.txt"]
10+
11+
# Copy the project text files into the Docker image working directory
12+
COPY *.txt ./

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Using Bash for Devops
2+
3+
## How to improve Bash error handling with the set builtin
4+
5+
This repo contains example files for [Using Bash for DevOps](https://medium.com/expedia-group-tech/using-bash-for-devops-7046eed1aa63), a blog post on Expedia Group Technology. The blog post contains full instructions for using the scripts.
6+
7+
Clone the repo.
8+
9+
```shell
10+
git clone https://github.com/techabstraction/bash-for-devops.git
11+
```
12+
13+
Change into the project directory.
14+
15+
```shell
16+
cd bash-for-devops
17+
```
18+
19+
To reset the files and start again, source the reset script as shown below, then type `y` at the confirmation dialogue.
20+
21+
```shell
22+
source 0-reset-tutorial.sh
23+
```
24+
25+
Note, you must use `source` to run the reset script above to ensure the `file_name` variable is unset. The other scripts in the repo **should not** be run using `source`.

file-list.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--> 0-reset-tutorial.sh
2+
--> 1-what-went-wrong.sh
3+
--> 2-execution-trace.sh
4+
--> 3-error-exit.sh
5+
--> 4-we-can-build-it.sh
6+
--> 5-no-unset.sh
7+
--> 6-generate-file-list.sh
8+
--> 7-pipe-fail.sh
9+
--> 8-all-the-things.sh

footer.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Expedia Group Technology
3+

header.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Using Bash for Devops Scripts
3+
-----------------------------
4+

0 commit comments

Comments
 (0)