#!/bin/bash # shellcheck disable=SC2317 # cd to the local VNC directory, exit if it fails cd "${HOME}/.vnc" || exit 5 # Variables file_base="./$(hostname):1." verbs="start status stop restart help" echobf() { printf '\033[1m%s\033[0m\n' "$*" } print_error() { >&2 ( printf '\033[1;31m%s\033[0m ' "ERROR:" echobf "$*" ) } error() { print_error "$1" shift exit "$1" } is_running() { vncserver -list | grep -q '^:1' } public_help() { local name name="$(basename "$0")" cat << EOF ${name} - Start a VNC server Usage: ${name} $(echo "$verbs" | sed 's/ /|/g') EOF } error_help() { print_error "Invalid usage" public_help return 1 } public_start() { if ! is_running; then set -e vncserver \ -xstartup ./xstartup \ -localhost \ -alwaysshared \ -securitytypes none else error "The VNC server is already running!" 4 fi } public_status() { local log_f="${file_base}log" if is_running; then echobf "The VNC server is running." tail "$log_f" | sed 's/^/\t/g' else echobf "The VNC server is not running." fi } public_stop() { if is_running; then vncserver -kill :1 else error "The VNC server is not running!" 3 fi } public_restart() { stop start } # Argument parsing set -e; trap 'set +e; error "$error" "$?"' EXIT error="You must give an argument"; [ -n "$1" ]; arg="$1"; shift; unset error set +e; trap - EXIT # Main case statement if echo "$verbs" | grep -q "$arg"; then "public_${arg}" else error_usage fi