Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesperronn/06d5a2855265c8d057c6 to your computer and use it in GitHub Desktop.
Save jesperronn/06d5a2855265c8d057c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
BASEDIR=$(dirname $0)
. $BASEDIR/functions.sh
# step "Remounting / and /boot as read-write:"
# try mount -o remount,rw /
# try mount -o remount,rw /boot
# next
#
# usecases:
# * verify an expression exits exitcode 0
# * verify a certain string occurs (with grep)
# * verify a certain string does not occur
check_vboxnet() {
ifconfig |grep vboxnet 2>&1 > /dev/null
}
step "nmap installed"
try which nmap
next
step "vboxnet interface must exist"
try check_vboxnet
next
#!/bin/bash
# this file is based directly on suggesion by John Kugelman in
# http://stackoverflow.com/a/5196220/109305
#
#. /etc/init.d/functions
BASEDIR=$(dirname $0)
. $BASEDIR/initd_functions.sh
# Use step(), try(), and next() to perform a series of commands and print
# [ OK ] or [FAILED] at the end. The step as a whole fails if any individual
# command fails.
#
# Example:
# step "Remounting / and /boot as read-write:"
# try mount -o remount,rw /
# try mount -o remount,rw /boot
# next
step() {
echo -n "$@"
STEP_OK=0
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
}
try() {
# Check for `-b' argument to run command in the background.
local BG=
[[ $1 == -b ]] && { BG=1; shift; }
[[ $1 == -- ]] && { shift; }
# Run the command.
if [[ -z $BG ]]; then
"$@"
else
"$@" &
fi
# Check if command failed and update $STEP_OK if so.
local EXIT_CODE=$?
if [[ $EXIT_CODE -ne 0 ]]; then
STEP_OK=$EXIT_CODE
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
if [[ -n $LOG_STEPS ]]; then
local FILE=$(readlink -m "${BASH_SOURCE[1]}")
local LINE=${BASH_LINENO[0]}
echo "$FILE: line $LINE: Command \`$*' failed with exit code $EXIT_CODE." >> "$LOG_STEPS"
fi
fi
return $EXIT_CODE
}
next() {
[[ -f /tmp/step.$$ ]] && { STEP_OK=$(< /tmp/step.$$); rm -f /tmp/step.$$; }
[[ $STEP_OK -eq 0 ]] && echo_success || echo_failure
echo
return $STEP_OK
}
#!/bin/bash
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
#
# Get a sane screen width
[ -z "${COLUMNS:-}" ] && COLUMNS=80
# Read in our configuration
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
echo_success() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_SUCCESS
echo -n $" OK "
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 0
}
echo_failure() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_FAILURE
echo -n $"FAILED"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
echo_passed() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_WARNING
echo -n $"PASSED"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
echo_warning() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_WARNING
echo -n $"WARNING"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment