Now that all platforms (beside the apple ones) can be built on linux, I want a fabulous build-all script, that automagically creates a folder with all runtimes. Time to refresh my bash-basics.

And to say it from the start: Spaces do matter!!

  • How to set a variable

      # GOOD:
      # WRONG:
      varname = "fortuna"
  • Show argument count

    echo number of arguments: $#
    echo script itself: $0
    echo arg1:$1 arg2:$2 arg3:$3  ...
  • basic conditionals for number comparison with if-elif-else-construct using some checks: -eq, -ne, -lt, -le, -gt, -ge

    if [[ $# -eq 0 ]]; then
      echo "NO ARGS"
    elif [[ $# -eq 1 ]]; then
      echo "ONE ARGUMENT"
      echo "$# Arguments"
    # not equals
    if [[ $# -ne 1 ]]; then
      echo "arg-count != 1"
      echo "arg-count == 1"
    # check if a directory does NOT exist
    if [[ ! -d "linux" ]]; then
      echo "linux-directory does not exist"

    For string-comparison use: =,!=

  • Some useful directory-magic:

    # get the directory from which the script is called
    # get the directory where the script is located (relative)
    REL_BASEDIR=$(dirname $0)
    # get the directory where the script is located (absolute)
    ABS_BASEDIR=$( cd $(dirname $0) ; pwd )
    # get the directory where the script is located (absolute with resolved symlinks)
    ABS_BASEDIR_RESOLVED=$( cd $(dirname $0) ; pwd -P )
  • Check if something went wrong (e.g. with cmake).

    cmake -DCMAKE_BUILD_TYPE=Release .. || {
      echo "Something went wrong with CMAKE! Exiting..."
      exit 1;
    echo "Everything went fine!"
  • exit the script:

    # just exit with code 0
    # give it a special exit-code you can use on the shell
    exit 95
    There are some special exit-codes
  • creating functions:

    function myEcho {
        echo $1
    myEcho Hello
  • Prevent a command to output errors

    # if a.txt is not present do not output any error
    rm a.txt 2>/dev/null

* Bash Conditionals
* Testing exit values
* Functions