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 # shellcheck disable=SC2317
# cd to the local VNC directory, exit if it fails # cd to the local VNC directory, exit if it fails
@ -6,40 +6,47 @@ cd "${HOME}/.vnc" || exit 5
# Variables # Variables
file_base="./$(hostname):1." file_base="./$(hostname):1."
verbs="start|status|stop|restart|help" verbs="start status stop restart help"
echol() { echobf() {
col="$1" printf '\033[1m%s\033[0m\n' "$*"
shift }
printf '\033['"${col}"'m%s\033[0m\n' "$*"
print_error() {
>&2 (
printf '\033[1;31m%s\033[0m ' "ERROR:"
echobf "$*"
)
} }
error() { error() {
>&2 printf '\033[1;31m%s\033[0m %s\n' "ERROR:" "$1" print_error "$1"
shift shift
exit "$1" 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() { is_running() {
vncserver -list | grep -q '^:1' 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 if ! is_running; then
set -e set -e
vncserver \ vncserver \
@ -52,17 +59,17 @@ start() {
fi fi
} }
status() { public_status() {
log_f="${file_base}log" local log_f="${file_base}log"
if is_running; then if is_running; then
echol '1;32' "The VNC server is running." echobf "The VNC server is running."
tail "$log_f" tail "$log_f" | sed 's/^/\t/g'
else else
echol '1;31' "The VNC server is not running." echobf "The VNC server is not running."
fi fi
} }
stop() { public_stop() {
if is_running; then if is_running; then
vncserver -kill :1 vncserver -kill :1
else else
@ -70,26 +77,20 @@ stop() {
fi fi
} }
restart() { public_restart() {
stop stop
start start
} }
# Argument parsing # Argument parsing
set -e; trap 'set +e; error "$error" "$?"' EXIT set -e; trap 'set +e; error "$error" "$?"' EXIT
error="You must give an argument" error="You must give an argument"; [ -n "$1" ]; arg="$1"; shift; unset error
[ -n "$1" ]; arg="$1"; shift
unset error
set +e; trap - EXIT set +e; trap - EXIT
# Main case statement # Main case statement
case "$arg" in if echo "$verbs" | grep -q "$arg"; then
status|start|stop|restart|help) "public_${arg}"
"$arg" else
;; error_usage
*) fi
usage "Invalid argument \"$arg\""
;;
esac