[PATCH v2] Remove double equals.
mail at adrianschmutzler.de
mail at adrianschmutzler.de
Do Jul 26 10:49:06 CEST 2018
Hallo,
solange es nur um Gleichheit geht, würde ich bei dem String-Vergleich bleiben, weil der nachvollziehbar funktioniert.
Alles andere bietet in meinen Augen keinen echten Vorteil, aber macht das Verständnis des Codes komplexer.
Grüße
Adrian
> -----Original Message-----
> From: franken-dev [mailto:franken-dev-bounces at freifunk.net] On Behalf
> Of Tim Niemeyer
> Sent: Mittwoch, 25. Juli 2018 23:18
> To: Robert Langhammer <rlanghammer at web.de>; franken-
> dev at freifunk.net
> Subject: Re: [PATCH v2] Remove double equals.
>
> Am Mittwoch, den 25.07.2018, 18:00 +0200 schrieb Robert Langhammer:
> > == is a bash specific alias for = , and should not be used in
> > #!/bin/sh scripts. Not in #!/usr/bin/haserl scripts either, where the
> > shell defaults to /bin/sh.
> >
> > > Signed-off-by: Robert Langhammer <rlanghammer at web.de>
> >
> > ---
> >
> > Changes in v2:
> > - do an integer comparison when the operand is an integer
> > - use -z to test an empty string
> >
> > ---
> > .../fff-hoods/files/usr/lib/functions/fff/hoodfile | 4 ++--
> > .../fff/fff-hoods/files/usr/sbin/configurehood | 8 +++----
> > .../fff/fff-vpn-select/files/usr/sbin/vpn-select | 2 +-
> > .../fff/fff-web/files/www/ssl/cgi-bin/header | 2 +-
> > .../fff/fff-web/files/www/ssl/cgi-bin/home.html | 2 +-
> > .../fff-web/files/www/ssl/cgi-bin/password.html | 6 ++---
> > .../fff/fff-web/files/www/ssl/cgi-bin/ports.html | 26
> > +++++++++++-----------
> > .../fff/fff-web/files/www/ssl/cgi-bin/reboot.html | 4 ++--
> > .../fff-web/files/www/ssl/cgi-bin/settings.html | 14 ++++++------
> > .../fff/fff-web/files/www/ssl/cgi-bin/upgrade.html | 12 +++++-----
> > 10 files changed, 40 insertions(+), 40 deletions(-)
> >
> > diff --git
> > a/src/packages/fff/fff-hoods/files/usr/lib/functions/fff/hoodfile
> > b/src/packages/fff/fff-hoods/files/usr/lib/functions/fff/hoodfile
> > index 8c80802..89af84c 100644
> > --- a/src/packages/fff/fff-hoods/files/usr/lib/functions/fff/hoodfile
> > +++ b/src/packages/fff/fff-hoods/files/usr/lib/functions/fff/hoodfile
> > @@ -94,10 +94,10 @@ getGatewayHoodfile() {
> > }
> >
> > getKeyserverHoodfile() {
> > > - if [ $# == 1 ]; then
> > > + if [ $# -eq 1 ]; then
> > > lat=$(uci -q get fff.system.latitude)
> > > long=$(uci -q get fff.system.longitude)
> > > - elif [ $# == 3 ]; then
> > > + elif [ $# -eq 3 ]; then
> > > lat=$2
> > > long=$3
> > > else
> > diff --git a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> > b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> > index 86d83fc..c2c2506 100755
> > --- a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> > +++ b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> > @@ -158,16 +158,16 @@ if [ -s "$hoodfile" ]; then
> > > exit 1
> > > fi
> >
> > > - # add 802.11s mesh if type == "802.11s"
> > > - if ( [ -n "$radio5" ] && [ "$mesh_type5" == "802.11s" ]
> ) || [ "$mesh_type2" == "802.11s" ]; then
> > > + # add 802.11s mesh if type = "802.11s"
> > > + if ( [ -n "$radio5" ] && [ "$mesh_type5" = "802.11s" ] )
> || [
> > > +"$mesh_type2" = "802.11s" ]; then
> > > if ! wifiAddMesh "$radio" "$mesh_id"; then
> > > echo "Can't add Mesh interface on
> $radio."
> > > exit 1
> > > fi
> > > fi
> >
> > > - # add IBSS mesh if type == "ibss"
> > > - if ( [ -n "$radio5" ] && [ "$mesh_type5" == "ibss" ] ) ||
> [ "$mesh_type2" == "ibss" ]; then
> > > + # add IBSS mesh if type = "ibss"
> > > + if ( [ -n "$radio5" ] && [ "$mesh_type5" = "ibss" ] ) || [
> > > +"$mesh_type2" = "ibss" ]; then
> > > if ! wifiAddAdHocMesh "$radio"
> "$mesh_essid" "$mesh_bssid"; then
> > > echo "Can't add AdHocMesh interface
> on $radio."
> > > exit 1
> > diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> > b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> > index 85930a8..2a1c631 100755
> > --- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> > +++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> > @@ -15,7 +15,7 @@ json_select vpn
> > while json_select "$Index" > /dev/null
> > do
> > > json_get_var protocol protocol
> > > - if [ "$protocol" == "fastd" ]; then
> > > + if [ "$protocol" = "fastd" ]; then
> > > json_get_var servername name
> > > filename="/etc/fastd/fff/peers/$servername"
> > > echo "#name \"${servername}\";" > "$filename"
> > diff --git a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/header
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/header
> > index d149c04..b9ca1b6 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/header
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/header
> > @@ -9,7 +9,7 @@ UPGRADE_PATH="$(getUpgradePath)"
> > echo -en "Content-Type: text/html\r\n\r\n"
> > nav_entry() {
> > > script_file="/cgi-bin/$1"
> > > - if [ "$script_file" == "$REQUEST_URI" ] ; then
> > > + if [ "$script_file" = "$REQUEST_URI" ] ; then
> > > local class_active=' class="active"'
> > > fi
> > > echo -ne "\t<li${class_active}><a
> href=\"${script_file}\">$2</a></li>\n\t\t"
> > diff --git a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/home.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/home.html
> > index 21bd4e7..821f7a0 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/home.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/home.html
> > @@ -5,7 +5,7 @@
> > . /lib/functions/fff/evalhoodinfo
> >
> > # prepare
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > if [ "$POST_resethood" != "" ] ; then
> > > # reset hood
> > > rm "$hoodfilecopy" 2> /dev/null
> > diff --git
> > a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/password.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/password.html
> > index d3287bf..fc3055a 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/password.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/password.html
> > @@ -2,10 +2,10 @@
> >
> > <%
> > # write
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > #check for special characters in password
> > > regex='^[a-zA-Z0-9!#\$%\(\)\*\+,\.:;=\?@\^_-]+$'
> > > - if [ "$POST_pass1" == "" ] ; then
> > > + if [ -z "$POST_pass1" ] ; then
> > > MSG='<span class="red">Das Passwort darf nicht leer
> sein!</span>'
> > elif ! echo -n "$POST_pass1" | egrep -q "$regex"; then
> > > MSG='<span class="red">Passwort enthält
> ungültige Zeichen!</span>'
> > @@ -46,7 +46,7 @@ fi
> > <%in /www/ssl/cgi-bin/footer %>
> > <%
> > #force instant password change
> > -if [ "$restart_uhttpd" == "1" ] ; then
> > +if [ "$restart_uhttpd" -eq 1 ] ; then
> Für den Fall, dass $restart_uhttpd leer ist, würde es nicht korrekt
> sein:
> --- %< ---
> # if [[ "" -eq 1 ]]; then echo true; else echo false; fi
> -ash: out of range
> false
> --- >% ---
>
> Dummerweise weiß ich jetzt auch nicht, wie man damit gut umgeht.
>
> a) Doch einen String-Vergleich machen?
>
> if [ "X$restart_uhttpd" = "X1" ] ; then
>
> b) Oder beim -eq bleiben, aber ne Null ran?
>
> if [ "0$restart_uhttpd" -eq 1 ] ; then
>
> c) Oder die Fehlermeldung ignorieren??
>
> Keine Ahnung.. ;)
>
> Diese Anmerkung gilt natürlich auch für alle folgenden -eq Vergleiche.
>
> Tim
>
> > /etc/init.d/uhttpd restart 2>/dev/null
> > fi
> > %>
> > diff --git a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/ports.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/ports.html
> > index 5912126..01506fc 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/ports.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/ports.html
> > @@ -3,7 +3,7 @@
> > <%
> > board_name=$(uci -q get board.model.name)
> > # write
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > if [ "$POST_change_mode" != "" ] ; then
> > > sed -i '/^.*# set via WebUI/d' /etc/network.config
> > > echo "ETHMODE=\"${POST_mode}\" # set via WebUI" >>
> > > /etc/network.config
> > @@ -45,7 +45,7 @@ format_state() {
> > format_port() {
> > > port=$(echo "$1" | sed 's/.* port:\([^ ]*\) .*/\1/')
> > > link=$(echo "$1" | sed 's/.* link:\([^ ]*\).*/\1/')
> > > - if [ "$link" == "up" ] ; then
> > > + if [ "$link" = "up" ] ; then
> > > speed=$(echo "$1" | sed 's/.* speed:\([^ ]*\).*/\1/')
> > > duplex=$(echo "$1" | sed 's/.* \([^ ]*-duplex\).*/\1/')
> > > else
> > @@ -95,7 +95,7 @@ format_port() {
> > > if [ ! "$(awk -F= '/WANDEV=/ { print $2 }'
> /etc/network.$board_name)" = "$(awk -F= '/SWITCHDEV=/ { print $2 }'
> /etc/network.$board_name)" ] ; then
> > > wanif=$(uci -q get
> network.wan.ifname)
> > > link=$(cat
> /sys/class/net/${wanif}/operstate)
> > > - if [ "$link" == "up" ] ; then
> > > + if [ "$link" = "up" ] ; then
> > > speed="connected"
> > > else
> > > speed="no link"
> > @@ -166,10 +166,10 @@ format_port() {
> > > <th style="width:
> 1em;">Anschluss Modus:</th>
> > > <td>
> > > <select name="mode">
> > > - <option value="BATMAN" <%
> [ "$ETHMODE" == "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > - <option value="CLIENT" <% [
> "$ETHMODE" == "CLIENT" ] && echo -n 'selected="selected" '
> %>>CLIENT</option>
> > > + <option value="BATMAN" <%
> [ "$ETHMODE" = "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > + <option value="CLIENT" <% [
> "$ETHMODE" = "CLIENT" ] && echo
> > > +-n 'selected="selected" ' %>>CLIENT</option>
> > > <% if grep -q "ONE_PORT"
> "/etc/network.$board_name" ; then %>
> > > - <option value="WAN" <% [
> "$ETHMODE" == "WAN" ] && echo -n 'selected="selected" '
> %>>WAN</option>
> > > + <option value="WAN" <% [
> "$ETHMODE" = "WAN" ] && echo -n
> > > +'selected="selected" ' %>>WAN</option>
> > > <% fi %>
> > > </select>
> > > </td>
> > @@ -207,16 +207,16 @@ format_port() {
> > > <tr>
> > > <td>
> > > <select name="mode0">
> > > - <option value="BATMAN" <%
> [ "$LAN0MODE" == "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > - <option value="CLIENT" <% [
> "$LAN0MODE" == "CLIENT" ] && echo -n 'selected="selected" '
> %>>CLIENT</option>
> > > - <option value="WAN" <% [
> "$LAN0MODE" == "WAN" ] && echo -n 'selected="selected" '
> %>>WAN</option>
> > > + <option value="BATMAN" <%
> [ "$LAN0MODE" = "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > + <option value="CLIENT" <% [
> "$LAN0MODE" = "CLIENT" ] && echo -n 'selected="selected" '
> %>>CLIENT</option>
> > > + <option value="WAN" <% [
> "$LAN0MODE" = "WAN" ] && echo -n
> > > +'selected="selected" ' %>>WAN</option>
> > > </select>
> > > </td>
> > > <td>
> > > <select name="mode1">
> > > - <option value="BATMAN" <%
> [ "$LAN1MODE" == "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > - <option value="CLIENT" <% [
> "$LAN1MODE" == "CLIENT" ] && echo -n 'selected="selected" '
> %>>CLIENT</option>
> > > - <option value="WAN" <% [
> "$LAN1MODE" == "WAN" ] && echo -n 'selected="selected" '
> %>>WAN</option>
> > > + <option value="BATMAN" <%
> [ "$LAN1MODE" = "BATMAN" ] && echo -n 'selected="selected" '
> %>>BATMAN</option>
> > > + <option value="CLIENT" <% [
> "$LAN1MODE" = "CLIENT" ] && echo -n 'selected="selected" '
> %>>CLIENT</option>
> > > + <option value="WAN" <% [
> "$LAN1MODE" = "WAN" ] && echo -n
> > > +'selected="selected" ' %>>WAN</option>
> > > </select>
> > > </td>
> > > <td><input type="submit"
> name="change_twoport" /></td>
> > @@ -240,7 +240,7 @@ format_port() {
> > <%in /www/ssl/cgi-bin/footer %>
> > <%
> > # write
> > -if [ "$do_reboot" == "1" ] ; then
> > +if [ "$do_reboot" -eq 1 ] ; then
> > reboot
> > fi
> > %>
> > diff --git
> > a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/reboot.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/reboot.html
> > index eddc4ff..2efdf3e 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/reboot.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/reboot.html
> > @@ -2,7 +2,7 @@
> >
> > <%
> > # prepare
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > if [ "$POST_reboot" != "" ] ; then
> > > do_reboot=1
> > > MSG='<span class="green">Router wird
> neugestartet...</span>'
> > @@ -29,7 +29,7 @@ fi
> > <%in /www/ssl/cgi-bin/footer %>
> > <%
> > # write
> > -if [ "$do_reboot" == "1" ] ; then
> > +if [ "$do_reboot" -eq 1 ] ; then
> reboot
> > fi
> > %>
> > diff --git
> > a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/settings.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/settings.html
> > index cfb3a6b..7dd5eed 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/settings.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/settings.html
> > @@ -2,7 +2,7 @@
> >
> > <%
> > # write
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > #check for valid hostname as specified in rfc 1123
> > > #see http://stackoverflow.com/a/3824105
> > > regex='^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])'
> > @@ -24,7 +24,7 @@ if [ "$REQUEST_METHOD" == "POST" ] ; then
> >
> > > # Bitratenbegrenzung
> > > uci -q get "fff.trafficcontrol" > /dev/null || uci -q set
> fff.trafficcontrol=fff
> > > - if [ "$POST_traffic_limit" == "on" ] ; then
> > > + if [ "$POST_traffic_limit" = "on" ] ; then
> > > uci -q set "fff.trafficcontrol.enabled=1"
> > > uci -q set "simple-tc.example.enabled=1"
> > > else
> > @@ -40,7 +40,7 @@ if [ "$REQUEST_METHOD" == "POST" ] ; then
> >
> > > # Restliche Einstellungen
> > > uci -q set "fff.notifyupdate=webui"
> > > - if [ "$POST_upgrade_notification" == "on" ] ; then
> > > + if [ "$POST_upgrade_notification" = "on" ] ; then
> > > uci -q set "fff.notifyupdate.value=1"
> > > else
> > > uci -q set "fff.notifyupdate.value=0"
> > @@ -50,7 +50,7 @@ if [ "$REQUEST_METHOD" == "POST" ] ; then
> >
> > > if uci -q get system.poe_passthrough > /dev/null ; then
> > > uci -q set "fff.poe_passthrough=fff"
> > > - if [ "$POST_poe_passthrough" == "on" ] ; then
> > > + if [ "$POST_poe_passthrough" = "on" ] ; then
> > > uci -q set "fff.poe_passthrough.active=1"
> > > uci -q set "system.poe_passthrough.value=1"
> > > else
> > @@ -70,12 +70,12 @@ fi
> > <%
> > # read
> > chkd='checked="checked" '
> > -if [ "$(uci -q get 'fff.trafficcontrol.enabled')" == "1" ] ; then #
> > not set=0
> > +if [ "$(uci -q get 'fff.trafficcontrol.enabled')" -eq 1 ] ; then #
> > +not set=0
> > traffic_checked="$chkd"
> > else
> > > traffic_checked=""
> > fi
> > -if [ "$(uci -q get 'fff.notifyupdate.value')" == "0" ] ; then # not
> > set=1
> > +if [ "$(uci -q get 'fff.notifyupdate.value')" -eq 0 ] ; then # not
> > +set=1
> > upgrade_checked=""
> > else
> > > upgrade_checked="$chkd"
> > @@ -122,7 +122,7 @@ fi
> >
> > > <%
> > > if uci -q get system.poe_passthrough > /dev/null ; then
> > > - if [ "$(uci -q get 'fff.poe_passthrough.active')" == "1" ]
> ; then # not set=0
> > + if [ "$(uci -q get 'fff.poe_passthrough.active')" -eq 1 ] ;
> then #
> > +not set=0
> > poe_pt_checked="$chkd"
> > > else
> > > poe_pt_checked=""
> > diff --git
> > a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/upgrade.html
> > b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/upgrade.html
> > index af46023..9762edc 100755
> > --- a/src/packages/fff/fff-web/files/www/ssl/cgi-bin/upgrade.html
> > +++ b/src/packages/fff/fff-web/files/www/ssl/cgi-bin/upgrade.html
> > @@ -2,10 +2,10 @@
> >
> > <%
> > # prepare
> > -if [ "$REQUEST_METHOD" == "POST" ] ; then
> > +if [ "$REQUEST_METHOD" = "POST" ] ; then
> > > if [ "$POST_reset" != "" ] ; then
> > > # reset
> > > - if [ "$FORM_really_reset" == "on" ] ; then
> > > + if [ "$FORM_really_reset" = "on" ] ; then
> > > do_reset=1
> > > MSG='<span class="green">Router wird
> zurückgesetzt und anschließend neugestartet...</span>'
> > > fi
> > @@ -13,12 +13,12 @@ if [ "$REQUEST_METHOD" == "POST" ] ; then
> > > # upgrade
> > > if [ ! -f "$HASERL_firmware_path" ] ; then
> > > MSG='<span class="red">Firmware Datei nicht
> gefunden!</span>'
> > > - elif [ "$(du -k "$HASERL_firmware_path" | cut -f1)" == "0" ] ;
> then
> > > + elif [ "$(du -k "$HASERL_firmware_path" | cut -f1)" -eq 0 ] ;
> > > +then
> > > MSG='<span class="red">Firmware Datei ist
> leer!</span>'
> > > elif ! error=$(sysupgrade -T "$HASERL_firmware_path") ;
> then
> > > MSG="<span class=\"red\">Firmware Datei
> ungültig: <tt>${error}</tt></span>"
> > > else
> > > - if [ "$POST_keep_config" == "on" ] ; then
> > > + if [ "$POST_keep_config" = "on" ] ; then
> > > args=""
> > > else
> > > args="-n"
> > @@ -63,12 +63,12 @@ fi
> > <%in /www/ssl/cgi-bin/footer %>
> > <%
> > # write
> > -if [ "$do_reset" == "1" ] ; then
> > +if [ "$do_reset" -eq 1 ] ; then
> > > echo "<pre>"
> > > echo y | firstboot
> > > reboot
> > > echo "</pre>"
> > -elif [ "$do_sysupgrade" == "1" ] ; then
> > +elif [ "$do_sysupgrade" -eq 1 ] ; then
> > > echo "<pre>"
> > > echo "# sysupgrade $args $HASERL_firmware_path"
> > > sysupgrade $args $HASERL_firmware_path
Mehr Informationen über die Mailingliste franken-dev