From 0c8f792f0113dea7df19cc7dd561f257b91d4ff8 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Sun, 11 Jun 2023 12:46:46 +0200 Subject: [PATCH] =?UTF-8?q?Correction=20:=20r=C3=A9-=C3=A9criture=20de=20h?= =?UTF-8?q?eadlessvnc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/headlessvnc | 85 +++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/bin/headlessvnc b/bin/headlessvnc index 83d535f..4ddaf27 100755 --- a/bin/headlessvnc +++ b/bin/headlessvnc @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # shellcheck disable=SC2317 # cd to the local VNC directory, exit if it fails @@ -6,40 +6,47 @@ cd "${HOME}/.vnc" || exit 5 # Variables file_base="./$(hostname):1." -verbs="start|status|stop|restart|help" +verbs="start status stop restart help" -echol() { - col="$1" - shift - printf '\033['"${col}"'m%s\033[0m\n' "$*" +echobf() { + printf '\033[1m%s\033[0m\n' "$*" +} + +print_error() { + >&2 ( + printf '\033[1;31m%s\033[0m ' "ERROR:" + echobf "$*" + ) } error() { - >&2 printf '\033[1;31m%s\033[0m %s\n' "ERROR:" "$1" + print_error "$1" shift exit "$1" } -help() { - name="$(basename "$0")" - cat << EOF -${name} - Start a VNC server - -Usage: - ${name} ${verbs} -EOF -} - -usage() { - >&2 help - error "Invalid usage: ${1}" 1 -} - is_running() { vncserver -list | grep -q '^:1' } -start() { +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 \ @@ -52,17 +59,17 @@ start() { fi } -status() { - log_f="${file_base}log" +public_status() { + local log_f="${file_base}log" if is_running; then - echol '1;32' "The VNC server is running." - tail "$log_f" + echobf "The VNC server is running." + tail "$log_f" | sed 's/^/\t/g' else - echol '1;31' "The VNC server is not running." + echobf "The VNC server is not running." fi } -stop() { +public_stop() { if is_running; then vncserver -kill :1 else @@ -70,26 +77,20 @@ stop() { fi } -restart() { +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 +error="You must give an argument"; [ -n "$1" ]; arg="$1"; shift; unset error set +e; trap - EXIT - # Main case statement -case "$arg" in - status|start|stop|restart|help) - "$arg" - ;; - *) - usage "Invalid argument \"$arg\"" - ;; -esac +if echo "$verbs" | grep -q "$arg"; then + "public_${arg}" +else + error_usage +fi