From 66c7634f18c375b8f39a8f5a9ad22623a31b6760 Mon Sep 17 00:00:00 2001 From: Kristian Haugene Date: Sat, 9 Mar 2019 18:52:06 +0100 Subject: [PATCH] Standardize scripts, use bash and double brackets --- openvpn/tunnelDown.sh | 4 ++-- openvpn/tunnelUp.sh | 4 ++-- plugins/rss/environment-variables.tmpl | 2 +- scripts/healthcheck.sh | 11 +++++---- tinyproxy/stop.sh | 2 +- transmission/environment-variables.tmpl | 2 +- transmission/start.sh | 22 +++++++++--------- transmission/stop.sh | 6 ++--- transmission/updatePort.sh | 30 ++++++++++++------------- 9 files changed, 41 insertions(+), 42 deletions(-) diff --git a/openvpn/tunnelDown.sh b/openvpn/tunnelDown.sh index a2a2a2620..72a881ad3 100755 --- a/openvpn/tunnelDown.sh +++ b/openvpn/tunnelDown.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash /etc/transmission/stop.sh -[ ! -f /opt/tinyproxy/stop.sh ] || /opt/tinyproxy/stop.sh +[[ ! -f /opt/tinyproxy/stop.sh ]] || /opt/tinyproxy/stop.sh diff --git a/openvpn/tunnelUp.sh b/openvpn/tunnelUp.sh index 29e4299b3..3f4effd33 100755 --- a/openvpn/tunnelUp.sh +++ b/openvpn/tunnelUp.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash /etc/transmission/start.sh "$@" -[ ! -f /opt/tinyproxy/start.sh ] || /opt/tinyproxy/start.sh +[[ ! -f /opt/tinyproxy/start.sh ]] || /opt/tinyproxy/start.sh diff --git a/plugins/rss/environment-variables.tmpl b/plugins/rss/environment-variables.tmpl index 1a2f90ce6..1ee200cfc 100644 --- a/plugins/rss/environment-variables.tmpl +++ b/plugins/rss/environment-variables.tmpl @@ -1,4 +1,4 @@ -#! /bin/sh +#! /bin/bash export TRANSMISSION_DOWNLOAD_DIR={{ .Env.TRANSMISSION_DOWNLOAD_DIR }} export RSS_URL="{{ .Env.RSS_URL }}" \ No newline at end of file diff --git a/scripts/healthcheck.sh b/scripts/healthcheck.sh index dc669e277..bef1597c9 100755 --- a/scripts/healthcheck.sh +++ b/scripts/healthcheck.sh @@ -1,9 +1,9 @@ -#!/bin/sh +#!/bin/bash # Ping uses both exit codes 1 and 2. Exit code 2 cannot be used for docker health checks, # therefore we use this script to catch error code 2 -HOST=$HEALTH_CHECK_HOST +HOST=${HEALTH_CHECK_HOST} -if [ -z "$HOST" ] +if [[ -z "$HOST" ]] then echo "Host not set! Set env 'HEATH_CHECK_HOST'. For now, using default google.com" HOST="google.com" @@ -11,12 +11,11 @@ fi ping -c 1 $HOST STATUS=$? -if [ $STATUS -ne 0 ] +if [[ ${STATUS} -ne 0 ]] then echo "Network is down" exit 1 fi echo "Network is up" -exit 0 - +exit 0 \ No newline at end of file diff --git a/tinyproxy/stop.sh b/tinyproxy/stop.sh index 12677c38e..9a504f151 100755 --- a/tinyproxy/stop.sh +++ b/tinyproxy/stop.sh @@ -1,6 +1,6 @@ #!/bin/bash -if [ "${WEBPROXY_ENABLED}" = "true" ]; then +if [[ "${WEBPROXY_ENABLED}" = "true" ]]; then /etc/init.d/tinyproxy stop diff --git a/transmission/environment-variables.tmpl b/transmission/environment-variables.tmpl index f1816046c..128ee3395 100644 --- a/transmission/environment-variables.tmpl +++ b/transmission/environment-variables.tmpl @@ -1,4 +1,4 @@ -#! /bin/sh +#! /bin/bash export TRANSMISSION_HOME={{ .Env.TRANSMISSION_HOME }} export TRANSMISSION_ALT_SPEED_DOWN={{ .Env.TRANSMISSION_ALT_SPEED_DOWN }} export TRANSMISSION_ALT_SPEED_ENABLED={{ .Env.TRANSMISSION_ALT_SPEED_ENABLED }} diff --git a/transmission/start.sh b/transmission/start.sh index bafbf38d1..e2dcd92cd 100755 --- a/transmission/start.sh +++ b/transmission/start.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Source our persisted env variables from container startup . /etc/transmission/environment-variables.sh @@ -6,7 +6,7 @@ # This script will be called with tun/tap device name as parameter 1, and local IP as parameter 4 # See https://openvpn.net/index.php/open-source/documentation/manuals/65-openvpn-20x-manpage.html (--up cmd) echo "Up script executed with $*" -if [ "$4" = "" ]; then +if [[ "$4" = "" ]]; then echo "ERROR, unable to obtain tunnel address" echo "killing $PPID" kill -9 $PPID @@ -14,7 +14,7 @@ if [ "$4" = "" ]; then fi # If transmission-pre-start.sh exists, run it -if [ -x /scripts/transmission-pre-start.sh ] +if [[ -x /scripts/transmission-pre-start.sh ]] then echo "Executing /scripts/transmission-pre-start.sh" /scripts/transmission-pre-start.sh "$@" @@ -24,17 +24,17 @@ fi echo "Updating TRANSMISSION_BIND_ADDRESS_IPV4 to the ip of $1 : $4" export TRANSMISSION_BIND_ADDRESS_IPV4=$4 -if [ "combustion" = "$TRANSMISSION_WEB_UI" ]; then +if [[ "combustion" = "$TRANSMISSION_WEB_UI" ]]; then echo "Using Combustion UI, overriding TRANSMISSION_WEB_HOME" export TRANSMISSION_WEB_HOME=/opt/transmission-ui/combustion-release fi -if [ "kettu" = "$TRANSMISSION_WEB_UI" ]; then +if [[ "kettu" = "$TRANSMISSION_WEB_UI" ]]; then echo "Using Kettu UI, overriding TRANSMISSION_WEB_HOME" export TRANSMISSION_WEB_HOME=/opt/transmission-ui/kettu fi -if [ "transmission-web-control" = "$TRANSMISSION_WEB_UI" ]; then +if [[ "transmission-web-control" = "$TRANSMISSION_WEB_UI" ]]; then echo "Using Transmission Web Control UI, overriding TRANSMISSION_WEB_HOME" export TRANSMISSION_WEB_HOME=/opt/transmission-ui/transmission-web-control fi @@ -47,7 +47,7 @@ dockerize -template /etc/transmission/settings.tmpl:${TRANSMISSION_HOME}/setting echo "sed'ing True to true" sed -i 's/True/true/g' ${TRANSMISSION_HOME}/settings.json -if [ ! -e "/dev/random" ]; then +if [[ ! -e "/dev/random" ]]; then # Avoid "Fatal: no entropy gathering module detected" error echo "INFO: /dev/random not found - symlink to /dev/urandom" ln -s /dev/urandom /dev/random @@ -55,7 +55,7 @@ fi . /etc/transmission/userSetup.sh -if [ "true" = "$DROP_DEFAULT_ROUTE" ]; then +if [[ "true" = "$DROP_DEFAULT_ROUTE" ]]; then echo "DROPPING DEFAULT ROUTE" ip r del default || exit 1 fi @@ -63,11 +63,11 @@ fi echo "STARTING TRANSMISSION" exec su --preserve-environment ${RUN_AS} -s /bin/bash -c "/usr/bin/transmission-daemon -g ${TRANSMISSION_HOME} --logfile ${TRANSMISSION_HOME}/transmission.log" & -if [ "$OPENVPN_PROVIDER" = "PIA" ] +if [[ "$OPENVPN_PROVIDER" = "PIA" ]] then echo "CONFIGURING PORT FORWARDING" exec /etc/transmission/updatePort.sh & -elif [ "$OPENVPN_PROVIDER" = "PERFECTPRIVACY" ] +elif [[ "$OPENVPN_PROVIDER" = "PERFECTPRIVACY" ]] then echo "CONFIGURING PORT FORWARDING" exec /etc/transmission/updatePPPort.sh ${TRANSMISSION_BIND_ADDRESS_IPV4} & @@ -76,7 +76,7 @@ else fi # If transmission-post-start.sh exists, run it -if [ -x /scripts/transmission-post-start.sh ] +if [[ -x /scripts/transmission-post-start.sh ]] then echo "Executing /scripts/transmission-post-start.sh" /scripts/transmission-post-start.sh "$@" diff --git a/transmission/stop.sh b/transmission/stop.sh index 7ce93d7e1..0db720484 100755 --- a/transmission/stop.sh +++ b/transmission/stop.sh @@ -1,7 +1,7 @@ -#! /bin/sh +#! /bin/bash # If transmission-pre-stop.sh exists, run it -if [ -x /scripts/transmission-pre-stop.sh ] +if [[ -x /scripts/transmission-pre-stop.sh ]] then echo "Executing /scripts/transmission-pre-stop.sh" /scripts/transmission-pre-stop.sh "$@" @@ -11,7 +11,7 @@ fi kill $(pidof transmission-daemon) # If transmission-post-stop.sh exists, run it -if [ -x /scripts/transmission-post-stop.sh ] +if [[ -x /scripts/transmission-post-stop.sh ]] then echo "Executing /scripts/transmission-post-stop.sh" /scripts/transmission-post-stop.sh "$@" diff --git a/transmission/updatePort.sh b/transmission/updatePort.sh index e79c816c6..8c2e701d4 100755 --- a/transmission/updatePort.sh +++ b/transmission/updatePort.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#! /bin/bash echo "Wait for tunnel to be fully initialized and PIA is ready to give us a port" sleep 15 @@ -9,8 +9,8 @@ sleep 15 # Settings TRANSMISSION_PASSWD_FILE=/config/transmission-credentials.txt -transmission_username=$(head -1 $TRANSMISSION_PASSWD_FILE) -transmission_passwd=$(tail -1 $TRANSMISSION_PASSWD_FILE) +transmission_username=$(head -1 ${TRANSMISSION_PASSWD_FILE}) +transmission_passwd=$(tail -1 ${TRANSMISSION_PASSWD_FILE}) pia_client_id_file=/etc/transmission/pia_client_id transmission_settings_file=${TRANSMISSION_HOME}/settings.json @@ -19,11 +19,11 @@ transmission_settings_file=${TRANSMISSION_HOME}/settings.json # new_client_id() { - head -n 100 /dev/urandom | sha256sum | tr -d " -" | tee $pia_client_id_file + head -n 100 /dev/urandom | sha256sum | tr -d " -" | tee ${pia_client_id_file} } -pia_client_id="$(cat $pia_client_id_file 2>/dev/null)" -if [ -z "${pia_client_id}" ]; then +pia_client_id="$(cat ${pia_client_id_file} 2>/dev/null)" +if [[ -z "${pia_client_id}" ]]; then echo "Generating new client id for PIA" pia_client_id=$(new_client_id) fi @@ -33,26 +33,26 @@ port_assignment_url="http://209.222.18.222:2000/?client_id=$pia_client_id" pia_response=$(curl -s -f "$port_assignment_url") pia_curl_exit_code=$? -if [ -z "$pia_response" ]; then +if [[ -z "$pia_response" ]]; then echo "Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding" fi # Check for curl error (curl will fail on HTTP errors with -f flag) -if [ $pia_curl_exit_code -ne 0 ]; then +if [[ ${pia_curl_exit_code} -ne 0 ]]; then echo "curl encountered an error looking up new port: $pia_curl_exit_code" exit fi # Check for errors in PIA response error=$(echo "$pia_response" | grep -oE "\"error\".*\"") -if [ ! -z "$error" ]; then +if [[ ! -z "$error" ]]; then echo "PIA returned an error: $error" exit fi # Get new port, check if empty new_port=$(echo "$pia_response" | grep -oE "[0-9]+") -if [ -z "$new_port" ]; then +if [[ -z "$new_port" ]]; then echo "Could not find new port from PIA" exit fi @@ -65,7 +65,7 @@ echo "Got new port $new_port from PIA" # Check if transmission remote is set up with authentication auth_enabled=$(grep 'rpc-authentication-required\"' "$transmission_settings_file" \ | grep -oE 'true|false') -if [ "true" = "$auth_enabled" ] +if [[ "true" = "$auth_enabled" ]] then echo "transmission auth required" myauth="--auth $transmission_username:$transmission_passwd" @@ -76,8 +76,8 @@ fi # get current listening port transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+') -if [ "$new_port" != "$transmission_peer_port" ]; then - if [ "true" = "$ENABLE_UFW" ]; then +if [[ "$new_port" != "$transmission_peer_port" ]]; then + if [[ "true" = "$ENABLE_UFW" ]]; then echo "Update UFW rules before changing port in Transmission" echo "denying access to $transmission_peer_port" @@ -87,11 +87,11 @@ if [ "$new_port" != "$transmission_peer_port" ]; then ufw allow "$new_port" fi - transmission-remote $myauth -p "$new_port" + transmission-remote ${myauth} -p "$new_port" echo "Checking port..." sleep 10 - transmission-remote $myauth -pt + transmission-remote ${myauth} -pt else echo "No action needed, port hasn't changed" fi