Correction : ré-écriture de headlessvnc

This commit is contained in:
Hippolyte Chauvin 2023-06-11 12:46:46 +02:00
parent b46f8fec80
commit 0c8f792f01

View file

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