From 01bb9339b1782f366e647470438fcfdf21cef240 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Sun, 11 Jun 2023 12:53:27 +0200 Subject: [PATCH] =?UTF-8?q?Correction=20:=20r=C3=A9-=C3=A9criture=20de=20h?= =?UTF-8?q?eadlessvnc,=202/2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/headlessvnc | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/bin/headlessvnc b/bin/headlessvnc index 4ddaf27..c5f1381 100755 --- a/bin/headlessvnc +++ b/bin/headlessvnc @@ -1,17 +1,17 @@ #!/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." +# Variable verbs="start status stop restart help" +# echo bold text echobf() { printf '\033[1m%s\033[0m\n' "$*" } +# Print an error message print_error() { >&2 ( printf '\033[1;31m%s\033[0m ' "ERROR:" @@ -19,16 +19,19 @@ print_error() { ) } +# Print an error and exit error() { print_error "$1" shift exit "$1" } +# Check if the VNC server is running is_running() { vncserver -list | grep -q '^:1' } +# Show a help message public_help() { local name name="$(basename "$0")" @@ -40,12 +43,14 @@ Usage: EOF } +# Show the same help with an error error_help() { print_error "Invalid usage" - public_help + >&2 public_help return 1 } +# Start the VNC server public_start() { if ! is_running; then set -e @@ -59,16 +64,17 @@ public_start() { fi } +# Check if the server is running 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' + tail "./$(hostname):1.log" | sed 's/^/\t/g' else echobf "The VNC server is not running." fi } +# Stop the VNC server public_stop() { if is_running; then vncserver -kill :1 @@ -77,17 +83,20 @@ public_stop() { fi } +# Restart the VNC server 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 +# Parse argument +if [ -n "$1" ]; then + arg="$1" +else + arg=status +fi -# Main case statement +# Main switch if echo "$verbs" | grep -q "$arg"; then "public_${arg}" else