Hands-on task
Version bump script
Write /usr/local/bin/bump that reads the version from /root/VERSION (format X.Y.Z) and increments the patch (last number) by 1, writing it back. For example 1.2.3 → 1.2.4.
- bash
- semver
- arithmetic
- ifs
- chmod
Hands-on task
Write /usr/local/bin/bump that reads the version from /root/VERSION (format X.Y.Z) and increments the patch (last number) by 1, writing it back. For example 1.2.3 → 1.2.4.
Semantic versioning X.Y.Z is three numbers separated by dots (major, minor, patch), and bumping the version comes down to parsing the string into components, changing the right one and assembling it back. In the shell it's convenient to split the string without external utilities by temporarily setting the separator to a dot via the IFS variable and reading the fields with a single read; the arithmetic is done by the $(( )) construct. Remember the traps: > overwrites the file while >> appends; a script without a shebang won't run as an executable, and bash may treat numbers with a leading zero as octal.