Skip to content

Instantly share code, notes, and snippets.

@michaelward82
Last active March 5, 2016 22:47
Show Gist options
  • Select an option

  • Save michaelward82/c1903f2b37a76975740e to your computer and use it in GitHub Desktop.

Select an option

Save michaelward82/c1903f2b37a76975740e to your computer and use it in GitHub Desktop.
Helper function to create prettier and more meaningful vagrant provisioning script output. https://github.com/michaelward82/vagrant-provisioning-shell-function-helper for project
exe () {
MESSAGE_PREFIX="\b\b\b\b\b\b\b\b\b\b"
echo -e "$MESSAGE_PREFIX Execute: $1"
LOOP=0
while true;
do
if ! [ $LOOP == 0 ]; then echo -e "$MESSAGE_PREFIX ... "; fi;
sleep 3;
LOOP=$((LOOP+1))
done & ERROR=$("${@:2}" 2>&1)
status=$?
kill $!; trap 'kill $!' SIGTERM
if [ $status -ne 0 ];
then
echo -e "$MESSAGE_PREFIX ✖ Error" >&2
echo -e "$ERROR" >&2
else
echo -e "$MESSAGE_PREFIX ✔ Success"
fi
return $status
}
# Use as:
# exe "Update apt indexes" sudo apt-get update
# exe "Disable default vhost" sudo a2dissite 000-default
#
# Output:
#
# ==> Execute: Update apt indexes
# ==> ...
# ==> ...
# ==> ...
# ==> ✔ Success
# ==> Execute: Disable default vhost
# ==> ✔ Success
@michaelward82
Copy link
Author

Example output using exe function, with no errors:
Example output using exe function, with  errors

Example output using exe function, with error:
Example output using exe function, with error

Default output from executing commands directly, no actual errors:
Default output from executing commands directly, no actual errors

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