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 #!/bin/bash
# shellcheck disable=SC2317
# cd to the local VNC directory, exit if it fails # cd to the local VNC directory, exit if it fails
cd "${HOME}/.vnc" || exit 5 cd "${HOME}/.vnc" || exit 5
# Variables # Variable
file_base="./$(hostname):1."
verbs="start status stop restart help" verbs="start status stop restart help"
# echo bold text
echobf() { echobf() {
printf '\033[1m%s\033[0m\n' "$*" printf '\033[1m%s\033[0m\n' "$*"
} }
# Print an error message
print_error() { print_error() {
>&2 ( >&2 (
printf '\033[1;31m%s\033[0m ' "ERROR:" printf '\033[1;31m%s\033[0m ' "ERROR:"
@ -19,16 +19,19 @@ print_error() {
) )
} }
# Print an error and exit
error() { error() {
print_error "$1" print_error "$1"
shift shift
exit "$1" exit "$1"
} }
# Check if the VNC server is running
is_running() { is_running() {
vncserver -list | grep -q '^:1' vncserver -list | grep -q '^:1'
} }
# Show a help message
public_help() { public_help() {
local name local name
name="$(basename "$0")" name="$(basename "$0")"
@ -40,12 +43,14 @@ Usage:
EOF EOF
} }
# Show the same help with an error
error_help() { error_help() {
print_error "Invalid usage" print_error "Invalid usage"
public_help >&2 public_help
return 1 return 1
} }
# Start the VNC server
public_start() { public_start() {
if ! is_running; then if ! is_running; then
set -e set -e
@ -59,16 +64,17 @@ public_start() {
fi fi
} }
# Check if the server is running
public_status() { public_status() {
local log_f="${file_base}log"
if is_running; then if is_running; then
echobf "The VNC server is running." echobf "The VNC server is running."
tail "$log_f" | sed 's/^/\t/g' tail "./$(hostname):1.log" | sed 's/^/\t/g'
else else
echobf "The VNC server is not running." echobf "The VNC server is not running."
fi fi
} }
# Stop the VNC server
public_stop() { public_stop() {
if is_running; then if is_running; then
vncserver -kill :1 vncserver -kill :1
@ -77,17 +83,20 @@ public_stop() {
fi fi
} }
# Restart the VNC server
public_restart() { public_restart() {
stop stop
start start
} }
# Argument parsing # Parse argument
set -e; trap 'set +e; error "$error" "$?"' EXIT if [ -n "$1" ]; then
error="You must give an argument"; [ -n "$1" ]; arg="$1"; shift; unset error arg="$1"
set +e; trap - EXIT else
arg=status
fi
# Main case statement # Main switch
if echo "$verbs" | grep -q "$arg"; then if echo "$verbs" | grep -q "$arg"; then
"public_${arg}" "public_${arg}"
else else