Correction : ré-écriture de headlessvnc, 2/2

This commit is contained in:
Hippolyte Chauvin 2023-06-11 12:53:27 +02:00
parent 0c8f792f01
commit 01bb9339b1

View file

@ -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