Skip to content

Instantly share code, notes, and snippets.

@aidmax
Last active January 18, 2019 06:38
Show Gist options
  • Save aidmax/340213ab37146de0967931f8f5823ce3 to your computer and use it in GitHub Desktop.
Save aidmax/340213ab37146de0967931f8f5823ce3 to your computer and use it in GitHub Desktop.

Bash function to execute a sequence of commands with time tracking and error handling


#!/bin/bash

do_cmd()
{
    SECONDS=0

    echo
    echo "$(date +%Y-%m-%d\ %H:%M:%S) executing..."
    "$@"
    ret=$?
    if [[ $ret -eq 0 ]]
    then
        echo "Successfully ran [ $* ]"
        duration=$SECONDS
        # total=$SECONDS
        echo "$((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
        echo "---"
    else
        echo "Error: Command [ $* ] returned $ret"
        exit $ret
    fi
}

main ()
{
    echo "$(date +%Y-%m-%d\ %H:%M:%S) Starting..."
    do_cmd wget https://github.com/electron/asar/archive/v0.14.6.zip
    do_cmd wget https://github.com/kubernetes/kubernetes/releases/download/v1.13.2/kubernetes.tar.gz
    do_cmd wget https://github.com/kubernetes/kubernetes/archive/v1.13.2.zip
    echo "$(date +%Y-%m-%d\ %H:%M:%S) Success"
}

time main

echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment