fff-vpn-select: improve logic for fastd start/stop #13

Closed
adschm wants to merge 1 commits from adschm/firmware:vpnlogic into master
Owner

The start/stop part of fff-vpn-select currently uses a subshell
for what is meant to be logic grouping. Address that by swapping
condition levels and add comment to explain what is actually done.

The start/stop part of fff-vpn-select currently uses a subshell for what is meant to be logic grouping. Address that by swapping condition levels and add comment to explain what is actually done.
adschm added the
packages/fff
node
labels 2020-12-14 13:37:20 +01:00
adschm added the
bug
label 2020-12-14 14:16:41 +01:00
jkimmel reviewed 2020-12-15 12:59:29 +01:00
@ -54,3 +54,2 @@
pidfile="/tmp/run/fastd.fff.pid"
if [ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ]; then
([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) || /etc/init.d/fastd start
if [ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]; then
Owner

[ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ] sollte aequivalent zu [ -s "$pidfile" -a -d "/proc/$(cat "$pidfile")" ] sein und spart einen aufruf an test

`[ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]` sollte aequivalent zu `[ -s "$pidfile" -a -d "/proc/$(cat "$pidfile")" ]` sein und spart einen aufruf an test
Author
Owner

shellcheck sagt "Prefer [ p ] && [ q ] as [ p -a q ] is not well defined."

shellcheck sagt "Prefer [ p ] && [ q ] as [ p -a q ] is not well defined."
jkimmel marked this conversation as resolved
jkimmel reviewed 2020-12-15 13:04:36 +01:00
@ -56,1 +55,3 @@
([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) || /etc/init.d/fastd start
if [ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]; then
# Stop if service is running but no peers present
[ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] || /etc/init.d/fastd stop
Owner

Gibt es einen Grund

[ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] || ...

statt einfach direkt

ls /etc/fastd/fff/peers/* 2>/dev/null || ...

zu nutzen?

Gibt es einen Grund `[ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] || ...` statt einfach direkt `ls /etc/fastd/fff/peers/* 2>/dev/null || ...` zu nutzen?
Author
Owner

Ich fand das auch ekelig, habe dann gegoogled und tatsächlich wird genau das bestehende als Optimum empfohlen. Bei ls ohne [] muss man ja noch den Output unterdrücken ... Außerdem bin ich mir nicht sicher, wie ls mit return values umgeht, wenn man wildcards verwendet ...

Ich fand das auch ekelig, habe dann gegoogled und tatsächlich wird genau das bestehende als Optimum empfohlen. Bei ls ohne [] muss man ja noch den Output unterdrücken ... Außerdem bin ich mir nicht sicher, wie ls mit return values umgeht, wenn man wildcards verwendet ...
Author
Owner

ls /etc/fastd/fff/peers/* &>/dev/null || ...

scheint zu funktionieren, aber ob das jetzt besser ist?

`ls /etc/fastd/fff/peers/* &>/dev/null || ...` scheint zu funktionieren, aber ob das jetzt besser ist?
Owner

also $(...) wuerde meines wissens eine neue subshell aufmachen. Die wuerden wir uns mit dem Ansatz sparen.

also `$(...)` wuerde meines wissens eine neue subshell aufmachen. Die wuerden wir uns mit dem Ansatz sparen.
Member

Die Sach ist ganz simpel. ls wirft bei einem leeren Verzeichnis keinen Fehler. Mit * hinten dran bekommen wir den Fehler weil das globbing nicht funzt. D.h. nicht unbedingt, dass das Verzeichnis leer ist. Darum der Test auf "leere" Ausgabe von ls.
In unserem Fall koennte es sogar besser sein, den * wegzulassen.

Die Sach ist ganz simpel. ls wirft bei einem leeren Verzeichnis keinen Fehler. Mit * hinten dran bekommen wir den Fehler weil das globbing nicht funzt. D.h. nicht unbedingt, dass das Verzeichnis leer ist. Darum der Test auf "leere" Ausgabe von ls. In unserem Fall koennte es sogar besser sein, den * wegzulassen.
adschm force-pushed vpnlogic from 2fb746fe4c to dadde41b3c 2020-12-22 14:44:42 +01:00 Compare
adschm closed this pull request 2021-08-24 02:11:06 +02:00

Pull request closed

Sign in to join this conversation.
No description provided.