From fc5d3955112166e56950d7d25e0f8218d2f30f39 Mon Sep 17 00:00:00 2001 From: nyxnor Date: Fri, 15 Oct 2021 15:09:03 +0200 Subject: [PATCH] posix compliant shell scripts --- setup.sh | 56 ++++++++++++++++--------------------------- vanguards_parallel.sh | 24 +++++++++---------- 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/setup.sh b/setup.sh index 4ae1b69..0308071 100755 --- a/setup.sh +++ b/setup.sh @@ -1,52 +1,38 @@ -#!/bin/bash -e +#!/bin/sh -e -SYS_PYTHON=$(which pypy3 || which pypy || which pypy2) +DEST="${1}" +DEST="${DEST:=vanguardenv}" +mkdir -p "${DEST}" -if [ -z $1 ] -then - DEST=vanguardenv -else - DEST=$1 -fi - -mkdir -p $DEST +SYS_PYTHON=$(command -v pypy3 || command -v pypy || command -v pypy2) # 1. Install python if needed -if [ -z "$(which $SYS_PYTHON)" ] -then - echo "We need pypy or pypy3 to be in the path." - echo - echo "If you are on a Debian or Ubuntu system, you can try: " - echo " sudo apt-get install pypy virtualenv" - echo - echo "If there is no pypy for your arch, edit this script to set" - echo "SYS_PYTHON=\"python\"" +if [ -z "${SYS_PYTHON}" ]; then + printf "We need pypy or pypy3 to be in the path.\n + \b\bIf you are on a Debian or Ubuntu system, you can try: + \tsudo apt-get install pypy virtualenv\n + \b\bIf there is no pypy for your arch, edit this script to set + \tSYS_PYTHON=\"python\"\n" exit 1 fi -if [ -z "$(which virtualenv)" ] -then - echo "We need virtualenv to be in the path. If you are on a debian system, try:" - echo " sudo apt-get install virtualenv" +if [ -z "$(command -v virtualenv)" ]; then + printf "We need virtualenv to be in the path. If you are on a debian system, try: + \tsudo apt-get install virtualenv\n" exit 1 fi # 2. Initialize virtualenv -if [ ! -f ${DEST}/bin/activate ] -then - virtualenv --never-download -p $SYS_PYTHON $DEST -fi -source ${DEST}/bin/activate +[ ! -f "${DEST}"/bin/activate ] && virtualenv --never-download -p "${SYS_PYTHON}" "${DEST}" +. "${DEST}"/bin/activate # 3. Install stem+setuptools pip install --require-hashes -r requirements.txt -$(basename $SYS_PYTHON) setup.py install +BASE_SYS_PYTHON=${SYS_PYTHON##*/} +"${BASE_SYS_PYTHON}" setup.py install # 4. Inform user what to do -echo -echo "If we got this far, everything should be ready!" -echo -echo "Run 'source ${DEST}/bin/activate' to start the environment." -echo -echo "Then run 'vanguards' or './vanguards_parallel.sh'" +printf %s"\nIf we got this far, everything should be ready!\n +Run '. ${DEST}/bin/activate' to start the environment.\n +Then run 'vanguards' or './vanguards_parallel.sh'\n" diff --git a/vanguards_parallel.sh b/vanguards_parallel.sh index f82739a..e2c05ae 100755 --- a/vanguards_parallel.sh +++ b/vanguards_parallel.sh @@ -11,27 +11,25 @@ # Use pypy or pypy3, if available -SYS_PY=$(which pypy3 || which pypy || which pypy2 || which python3 || which python2) +SYS_PY=$(command -v pypy3 || command -v pypy || command -v pypy2 || command -v python3 || command -v python2) -VANGUARDS_LOCATION=$(which vanguards || echo "$SYS_PY ./src/vanguards.py") +VANGUARDS_LOCATION=$(command -v vanguards || printf %s"${SYS_PY} ./src/vanguards.py\n") -OTHER_OPTIONS="$@" +OTHER_OPTIONS="$*" # Vanguards instance -$VANGUARDS_LOCATION --disable_bandguards --disable_rendguard --logfile :syslog: $OTHER_OPTIONS & +"${VANGUARDS_LOCATION}" --disable_bandguards --disable_rendguard --logfile :syslog: "${OTHER_OPTIONS}" & # Bandguards instance -$VANGUARDS_LOCATION --disable_vanguards --disable_rendguard --logfile :syslog: $OTHER_OPTIONS & +"${VANGUARDS_LOCATION}" --disable_vanguards --disable_rendguard --logfile :syslog: "${OTHER_OPTIONS}" & # Rendguards instance -$VANGUARDS_LOCATION --disable_vanguards --disable_bandguards --logfile :syslog: $OTHER_OPTIONS & +"${VANGUARDS_LOCATION}" --disable_vanguards --disable_bandguards --logfile :syslog: "${OTHER_OPTIONS}" & jobs -l -echo -echo "Vanguards is now running in the background as the above jobs." -echo "Note that they log to syslog to avoid overwriting eachother's logs" -echo -echo "If you still are experiencing high CPU from the vanguards process," -echo "remember that it can be run with --one_shot_vanguards, once per hour" -echo "from cron." +printf "\nVanguards is now running in the background as the above jobs. +Note that they log to syslog to avoid overwriting eachother's logs\n +If you still are experiencing high CPU from the vanguards process, +remember that it can be run with --one_shot_vanguards, once per hour +from cron.\n"