From 6361f99f2d0829825d972915ef10aced2d9fb4df Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Mon, 25 Nov 2019 13:57:47 +0800
Subject: [PATCH] clean up shell scripts for shellcheck linter checks

---
 meson.build                         |  28 ++-
 meson/bash_ninja_autocomplete.sh    |  12 +-
 meson/c_main_sourcefiles.sh         |   3 +-
 meson/check_binary_grid2.sh         |   4 +-
 meson/clean_data_objects.sh         |   4 +-
 meson/cpu_frequency.sh              |   4 +-
 meson/data_object_list.sh           |   2 +-
 meson/data_object_list_and_build.sh |   2 +-
 meson/git_revision.sh               |   8 +-
 meson/list_shared_symbols.sh        |   2 +-
 meson/pgo.sh                        |  49 ++---
 tbse                                | 278 ++++++++++------------------
 12 files changed, 156 insertions(+), 240 deletions(-)

diff --git a/meson.build b/meson.build
index 1f726eda6..a244ac1e7 100644
--- a/meson.build
+++ b/meson.build
@@ -36,21 +36,14 @@
 # as a useful cookbook.
 #
 ############################################################
-#
-# A typical clean build with gcc and "time make" (without ccache) gives 
-#
-# 82.20user 19.66system 0:30.18elapsed 337%CPU (0avgtext+0avgdata 330368maxresident)k
-#
-# While with ninja (no ccache) we get
-# 55.83user 15.86system 0:20.14elapsed 355%CPU (0avgtext+0avgdata 327448maxresident)k
-############################################################
 # define the binary_c project
+#
 project(
     'binary_c','c',
     default_options : [
         'c_std=gnu99',
     ],
-    version : '2.1.5',
+    version : '2.1.5', # should agree with binary_c_version.h
 )
 
 ############################################################
@@ -68,6 +61,9 @@ endif
 
 ############################################################
 # System information
+#
+# e.g. cpu frequency
+#
 if(host_machine.cpu_family() == 'x86_64')
     # test on 64-bit Intels
     _cpufreq = compiler.run( '''
@@ -107,10 +103,12 @@ message('CPU frequency ' + cpufreq + 'MHz')
 
 ############################################################
 # get home directory
+#
 homedir = run_command('sh','-c','echo $HOME').stdout().strip()
 
 ############################################################
 # get binary_c root and src locations
+#
 binary_c = meson.source_root()
 binary_c_src = meson.source_root() + '/src'
 
@@ -305,12 +303,6 @@ if os == 'linux'
         '-DLARGEFILE_SOURCE'
     ]
     unix = true
-elif os == 'windows'
-    # windows
-    cflags += [
-        '-DWINDOWS',
-    ]
-    unix = false
 elif os == 'darwin'
     # darwin (MacOSX)
     cflags += [
@@ -318,6 +310,12 @@ elif os == 'darwin'
         '-DPOSIX',
     ]
     unix = true
+elif os == 'windows'
+    # windows
+    cflags += [
+        '-DWINDOWS',
+    ]
+    unix = false
 else
     cflags += [
         '-DUNKNOWN_OPERATING_SYSTEM'
diff --git a/meson/bash_ninja_autocomplete.sh b/meson/bash_ninja_autocomplete.sh
index 2466bd1b1..01c18dfc6 100755
--- a/meson/bash_ninja_autocomplete.sh
+++ b/meson/bash_ninja_autocomplete.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+#
 # Copyright 2011 Google Inc. All Rights Reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,7 +16,9 @@
 
 # Add the following to your .bashrc to tab-complete ninja targets
 #   . path/to/ninja/misc/bash-completion
-
+#
+# Edited for shellcheck compliance 26/11/2019 (Robert Izzard)
+#
 _ninja_target() {
     local cur prev targets dir line targets_command OPTIND
 
@@ -41,9 +45,9 @@ _ninja_target() {
 	COMPREPLY=($(compgen -P '--' -W 'version' -- "${cur:2}"))
     else
 	dir="."
-	line=$(echo ${COMP_LINE} | cut -d" " -f 2-)
+	line=$(echo "${COMP_LINE}" | cut -d" " -f 2-)
         # filter out all non relevant arguments but keep C for dirs
-	while getopts :C:f:j:l:k:nvd:t: opt $line; do
+	while getopts :C:f:j:l:k:nvd:t: opt "$line"; do
 	    case $opt in
                 # eval for tilde expansion
 		C) eval dir="$OPTARG" ;;
@@ -56,7 +60,7 @@ _ninja_target() {
         # RGI autocomplete list : bit of trial and error here!
         targets_command="eval ninja -C \"${dir}\" -t targets all |grep '\(LINKER\|COMMAND\|SHSYM\)'| cut -d: -f1"
         #echo "Targets: $targets_command"
-        COMPREPLY=($(compgen -W '`${targets_command}`' -- "$cur"))
+        COMPREPLY=($(compgen -W "$(targets_command)" -- "$cur"))
         #echo "COMPREPLY $COMPREPLY"
     fi
     return
diff --git a/meson/c_main_sourcefiles.sh b/meson/c_main_sourcefiles.sh
index 395085b70..89f1d7fad 100755
--- a/meson/c_main_sourcefiles.sh
+++ b/meson/c_main_sourcefiles.sh
@@ -1,4 +1,5 @@
 #!/bin/bash
 
 # list binary_c C source files for meson
-ls -1 src/*.c src/*/*.c |grep main.c
+ls -1 src/main.c
+
diff --git a/meson/check_binary_grid2.sh b/meson/check_binary_grid2.sh
index fb0c695f2..841fe3b7d 100755
--- a/meson/check_binary_grid2.sh
+++ b/meson/check_binary_grid2.sh
@@ -6,8 +6,8 @@
 #
 # otherwise, return 1
 FILE="$HOME/progs/perl/modules/astro/binary_grid/lib/binary_grid2.pm"
-if [ -f $FILE ]; then
-    echo $FILE
+if [ -f "$FILE" ]; then
+    echo "$FILE"
     exit 0
 else
     exit 1
diff --git a/meson/clean_data_objects.sh b/meson/clean_data_objects.sh
index 47f37c5e9..1188a60dd 100755
--- a/meson/clean_data_objects.sh
+++ b/meson/clean_data_objects.sh
@@ -2,5 +2,5 @@
 
 # clean data objects for a complete rebuild
 
-FILES=`./meson/data_object_list.sh`
-rm $FILES
+FILES="$(./meson/data_object_list.sh)"
+rm "$FILES"
diff --git a/meson/cpu_frequency.sh b/meson/cpu_frequency.sh
index 294035406..33a778ec9 100755
--- a/meson/cpu_frequency.sh
+++ b/meson/cpu_frequency.sh
@@ -4,11 +4,11 @@
 
 SYSFILE="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
 
-if [ -f $SYSFILE ]; then
+if [ -f "$SYSFILE" ]; then
     # standard on Linux
     gawk "{print int(\$1/1000.0)}" $SYSFILE
          
-elif [ command sysctl >/dev/null 2>/dev/null ]; then
+elif [ "$(sysctl >/dev/null 2>/dev/null)" ]; then
     # this might work on a Mac
     sysctl -a |gawk "{print \$2}"
     
diff --git a/meson/data_object_list.sh b/meson/data_object_list.sh
index 275a69d72..2d63dfd20 100755
--- a/meson/data_object_list.sh
+++ b/meson/data_object_list.sh
@@ -3,7 +3,7 @@
 # wrapper for data_object_list.sh
 
 # list them
-cd src
+cd src || exit
 env INCDIRS="-I. -I/usr/include  -I/home/izzard/include  -I/usr/include/libiberty " ../data_object_list.sh | sed -e s/^/src\\// | sed s/\ /\ src\\//g
 
 
diff --git a/meson/data_object_list_and_build.sh b/meson/data_object_list_and_build.sh
index 2b2dc86f1..ab616ae32 100755
--- a/meson/data_object_list_and_build.sh
+++ b/meson/data_object_list_and_build.sh
@@ -9,7 +9,7 @@
 
 ############################################################
 # list them
-cd src
+cd src || exit
 env INCDIRS="-I. -I/usr/include  -I/home/izzard/include  -I/usr/include/libiberty " ../data_object_list.sh | sed -e s/^/src\\// | sed s/\ /\ src\\//g
 
 
diff --git a/meson/git_revision.sh b/meson/git_revision.sh
index 4d2d1ae21..f0cffbe0f 100755
--- a/meson/git_revision.sh
+++ b/meson/git_revision.sh
@@ -2,10 +2,10 @@
 
 # wrapper to output the git revision
 # (outputs nothing on failure)
-d=`date +%Y%m%d 2>/dev/null`
-c=`git rev-list --full-history --all --abbrev-commit 2>/dev/null | /usr/bin/wc -l | sed -e 's/^ *//'`
-h=`git rev-list --full-history --all --abbrev-commit 2>/dev/null | head -1`
+d=$(date +%Y%m%d 2>/dev/null)
+c=$(git rev-list --full-history --all --abbrev-commit 2>/dev/null | /usr/bin/wc -l | sed -e 's/^ *//')
+h=$(git rev-list --full-history --all --abbrev-commit 2>/dev/null | head -1)
 
 if [[  ! -z "$d" && ! -z "$c" && ! -z "$h" ]]; then
-    echo ${c}:${d}:${h}
+    echo "${c}:${d}:${h}"
 fi
diff --git a/meson/list_shared_symbols.sh b/meson/list_shared_symbols.sh
index b9c524948..6e5a6bed3 100755
--- a/meson/list_shared_symbols.sh
+++ b/meson/list_shared_symbols.sh
@@ -6,7 +6,7 @@ SOFILE=$1
 # red
 echo -e "\e[31m"
 
-nm -CD -f p $SOFILE | awk '{if ($2 == "T"){print $1}}' | grep -v _fini|grep -v _init | tr "\n" " "
+nm -CD -f p "$SOFILE" | awk '{if ($2 == "T"){print $1}}' | grep -v _fini|grep -v _init | tr "\n" " "
 
 #reset
 echo -e "\e[0m"
diff --git a/meson/pgo.sh b/meson/pgo.sh
index 50b9960d4..e895f8969 100755
--- a/meson/pgo.sh
+++ b/meson/pgo.sh
@@ -5,16 +5,16 @@
 
 # set defaults
 BUILDDIR="builddir"
-N=$1
-: ${N:="1000"}
-NCORES=`cat /proc/cpuinfo |grep -i Processor | wc -l`
-: ${NCORES:="1"}
+N="$1"
+: "${N:='1000'}"
+NCORES=$(grep -c -i Processor /proc/cpuinfo)
+: "${NCORES:='1'}"
 TMP="/tmp/"
 NCORES1=$((NCORES - 1))
-REPEAT_PER_CORE=$(($N/$NCORES))
+REPEAT_PER_CORE=$((N/NCORES))
 SLEEP=0
-MESON=meson
-NINJA=ninja
+MESON="meson"
+NINJA="ninja"
 
 # where to redirect stderr, stdout on meson and ninja calls
 MESON_STDERR=/dev/stderr
@@ -53,34 +53,33 @@ trap '[ -n "$(jobs -pr)" ] && KillChildren $$' INT QUIT TERM EXIT
 # first build: use builddir if already avaialble
 echo "Configure meson for PGO build 1"
 if [ -d $BUILDDIR ] ; then
-    meson configure $BUILDDIR -Db_pgo=generate > $MESON_STDOUT 2> $MESON_STDERR
+    $MESON configure $BUILDDIR -Db_pgo=generate > $MESON_STDOUT 2> $MESON_STDERR
 else
-    meson $BUILDDIR -Db_pgo=generate > $MESON_STDOUT 2> $MESON_STDERR
+    $MESON $BUILDDIR -Db_pgo=generate > $MESON_STDOUT 2> $MESON_STDERR
 fi
 echo "Build (1) with ninja"
-#ninja -C $BUILDDIR > $NINJA_STDOUT 2> $NINJA_STDERR
-
-
+$NINJA -C $BUILDDIR > $NINJA_STDOUT 2> $NINJA_STDERR
 
+############################################################
 ############################################################
 # run and filter to files
-START=`date +%s%N`
+START=$(date +%s%N)
 echo "Running binary_c"
-for i in `seq 0 $NCORES1`; do
+for i in $(seq 0 $NCORES1); do
     echo "Launch tbse on core $i"
-    taskset -c $i $TBSE $COMMAND1 | grep At | awk '{print substr($0, index($0,$5))}' > /tmp/binary_c_pgo.$i &
-    sleep $SLEEP
+    taskset -c "$i" "$TBSE" "$COMMAND1" | grep At | awk '{print substr($0, index($0,$5))}' > "$TMP/binary_c_pgo.$i" &
+    sleep "$SLEEP"
 done
 wait
-END=`date +%s%N`
+END=$(date +%s%N)
 PGO1_DURATION=$(echo "(0.000000001*($END - $START))"|bc)
 
 ############################################################
 # rebuild
 echo "Configure meson for PGO build 2"
-meson configure $BUILDDIR -Db_pgo=use > $MESON_STDOUT 2> $MESON_STDERR
+$MESON configure $BUILDDIR -Db_pgo=use > $MESON_STDOUT 2> $MESON_STDERR
 echo "Build (2) with ninja" 
-ninja -C $BUILDDIR > $NINJA_STDOUT 2> $NINJA_STDERR
+$NINJA -C $BUILDDIR > $NINJA_STDOUT 2> $NINJA_STDERR
 
 # remove trap
 trap - INT QUIT TERM EXIT
@@ -91,14 +90,15 @@ trap - INT QUIT TERM EXIT
 # kill children on exit
 trap '[ -n "$(jobs -pr)" ] && KillChildren $$' INT QUIT TERM EXIT
 
-START=`date +%s%N`
+START=$(date +%s%N)
 echo "Running binary_c"
-for i in `seq 0 $NCORES1`; do
-    env FIRST_TBSE="warmup_cpu $WARMUP_SECS" taskset -c $i $TBSE /tmp/binary_c_pgo.$i 2>&1  >/dev/null &
+
+for i in $(seq 0 $NCORES1); do
+    env FIRST_TBSE="warmup_cpu $WARMUP_SECS" taskset -c "$i" "$TBSE" "$TMP/binary_c_pgo.$i" >/dev/null 2>&1  &
     sleep $SLEEP
 done
 wait
-END=`date +%s%N`
+END=$(date +%s%N)
 PGO2_DURATION=$(echo "(0.000000001*($END - $START))"|bc)
 
 # remove trap
@@ -108,7 +108,8 @@ echo "Run 1 (without PGO) took : $PGO1_DURATION s"
 echo "Run 2 (   with PGO) took : $PGO2_DURATION s"
 
 # final speedup %
+SPEEDUP=$(echo "(100.0*($PGO1_DURATION-$PGO2_DURATION)/$PGO1_DURATION)"|bc)
 echo -n "Profile guided optimization speedup is "
-echo -n $(echo "(100.0*($PGO1_DURATION-$PGO2_DURATION)/$PGO1_DURATION)"|bc)
+echo -n "$SPEEDUP"
 echo "%"
 exit
diff --git a/tbse b/tbse
index 98fd595a8..4e40be8f9 100755
--- a/tbse
+++ b/tbse
@@ -42,7 +42,7 @@ then
 fi
 
 # allow comments
-[ -z $BASH ] || shopt -s expand_aliases
+[ -z "$BASH" ] || shopt -s expand_aliases
 alias BEGINCOMMENT="if [ ]; then"
 alias ENDCOMMENT="fi"
 
@@ -65,7 +65,7 @@ WARMUP_SECONDS=0
 export PATH=$PATH:.
 
 # binary_c executable, defaults to ./binary_c
-: ${BIN_C:="./binary_c"}
+: "${BIN_C:="./binary_c"}"
 
 # run system $REPEAT times
 REPEAT=1
@@ -1598,7 +1598,6 @@ BEGINCOMMENT
     AGB_RADIUS_ALGORITHM=AGB_RADIUS_ALGORITHM_KARAKAS
     AGB_CORE_ALGORITHM=AGB_CORE_ALGORITHM_KARAKAS
     AGB_3DUP_ALGORITHM=AGB_THIRD_DREDGE_UP_ALGORITHM_KARAKAS
-    AGB_THIRD_DREDGE_UP_ALGORITHM=AGB_THIRD_DREDGE_UP_ALGORITHM_KARAKAS
     GB_REIMERS_ETA=0.5
     ## todo check Eqs.14 and 15 of C14
     WIND_ANGULAR_MOMENTUM_LOSS=WIND_ANGMOM_LOSS_LW
@@ -1632,10 +1631,10 @@ IONICELEVEL=3
 killtree() {
     local parent=$1 child
     for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do
-        killtree $child
+        killtree "$child"
     done
-    kill $parent
-    kill -s INT $parent
+    kill "$parent"
+    kill -s INT "$parent"
 }
 KillChildren() {
         local pid="${1}"
@@ -1674,7 +1673,7 @@ function filesize
         wc -c <"$file" 2>/dev/null
     ) 
     q=$?; [ $q -eq 0 ] || exit $q
-    SIZE=`echo $SIZE | awk '{print $1}'`
+    SIZE="$(echo "$SIZE" | awk '{print "$1"}')"
 }
 
 # custom exit function that uses the $EXIT_CODE specified
@@ -1695,7 +1694,7 @@ function exit
 # put them in a list
 for first in "$@"
 do
-    if [ $first -a -f $first ]; 
+    if [ "$first" ] && [ -f "$first" ]; 
     then
         if [ -z "$argfiles" ]; then
             argfiles="$first"
@@ -1719,31 +1718,30 @@ fi
 
 #  remove first argument if one of the following runmodes
 
-if [ "$1" == "debug" \
-    -o "$1" == "gdb" \
-    -o "$1" == "valgrind" \
-    -o "$1" == "valgrind_args" \
-    -o "$1" == "valgrind_cmd" \
-    -o "$1" == "massif" \
-    -o "$1" == "callgrind" \
-    -o "$1" == "cachegrind" \
-    -o "$1" == "ptrcheck" \
-    -o "$1" == "sgcheck" \
-    -o "$1" == "drd" \
-    -o "$1" == "gprof" \
-    -o "$1" == "gprof_lines" \
-    -o "$1" == "gprof_with_stdout" \
-    -o "$1" == "gcov" \
-    -o "$1" == "args" \
-    -o "$1" == "echo" \
-    -o "$1" == "echolines" \
-    -o "$1" == "bug" \
-    -o "$1" == "multicore" \
-    -o "$1" == "multicore_valgrind" \
-    -o "$1" == "pgo" \
-    -o "$1" == "arglines" \
-    -o "$1" == "clip" \
-   ]
+if [ "$1" == "debug" ] \
+    || [ "$1" == "gdb" ] \
+    || [ "$1" == "valgrind" ] \
+    || [ "$1" == "valgrind_args" ] \
+    || [ "$1" == "valgrind_cmd" ] \
+    || [ "$1" == "massif" ] \
+    || [ "$1" == "callgrind" ] \
+    || [ "$1" == "cachegrind" ] \
+    || [ "$1" == "ptrcheck" ] \
+    || [ "$1" == "sgcheck" ] \
+    || [ "$1" == "drd" ] \
+    || [ "$1" == "gprof" ] \
+    || [ "$1" == "gprof_lines" ] \
+    || [ "$1" == "gprof_with_stdout" ] \
+    || [ "$1" == "gcov" ] \
+    || [ "$1" == "args" ] \
+    || [ "$1" == "echo" ] \
+    || [ "$1" == "echolines" ] \
+    || [ "$1" == "bug" ] \
+    || [ "$1" == "multicore" ] \
+    || [ "$1" == "multicore_valgrind" ] \
+    || [ "$1" == "pgo" ] \
+    || [ "$1" == "arglines" ] \
+    || [ "$1" == "clip" ]
 then
     RUNMODE=$1
     shift
@@ -1773,8 +1771,8 @@ fi
 # check for external commands
 if [ -x "$(command -v ionice)" ]
 then
-    IONICE=`which ionice`
-    if [ -f $IONICE ]
+    IONICE=$(which ionice)
+    if [ -f "$IONICE" ]
     then
 	IONICE="$IONICE -c$IONICELEVEL"
     fi
@@ -1782,8 +1780,8 @@ fi
 
 if [ -x "$(command -v nice)" ]
 then
-    NICE=`which nice`
-    if [ -f $NICE ]
+    NICE=$(which nice)
+    if [ -f "$NICE" ]
     then
 	NICE="$NICE -n +$NICELEVEL"
     fi
@@ -1791,8 +1789,8 @@ fi
 
 if [ -x "$(command -v stdbuf)" ]
 then
-    STDBUF=`which stdbuf`
-    if [ -f $STDBUF ]
+    STDBUF=$(which stdbuf)
+    if [ -f "$STDBUF" ]
     then
         STDBUF="$STDBUF -o 0"
     fi
@@ -1808,20 +1806,20 @@ then
     if [ -x "$(command -v taskset)" ]
     then
 	# CPU affinity: lock the process to one CPU
-	CPUNUM=`ps h -o psr --pid $$`
-	TASKSET=`which taskset`
-	if [ -f $TASKSET ]
+	CPUNUM="$(ps h -o psr --pid $$)"
+	TASKSET="$(which taskset)"
+	if [ -f "$TASKSET" ]
 	then
 	    # lock this script to this CPU
-	    taskset -c -p $CPUNUM $$ >/dev/null
+	    taskset -c -p "$CPUNUM" $$ >/dev/null
 	    # lock binary_c to this CPU
-	    AFFINITY="$TASKSET -c $CPUNUM"
+	    #AFFINITY="$TASKSET -c $CPUNUM"
 	fi
     fi
 fi
 
-if [ "$RUNMODE" == "multicore" \
-    -o "$RUNMODE" == "multicore_valgrind" ];
+if [ "$RUNMODE" == "multicore" ] || \
+   [ "$RUNMODE" == "multicore_valgrind" ];
 then
     NCORES=$1
     if [[ -z "$NCORES" ]]
@@ -1832,13 +1830,13 @@ then
     shift
 fi
 
-if [ "$RUNMODE" == "clip" ];
-then
-    # output is sent to the clipboard
-    PIPE=" xclip -selection c "
-else
-    PIPE=" cat "
-fi
+#if [ "$RUNMODE" == "clip" ];
+#then
+#    # output is sent to the clipboard
+#    PIPE=" xclip -selection c "
+#else
+#    PIPE=" cat "
+#fi
 
 # construct the argument list
 DEFAULT_ARGS=" \
@@ -2190,14 +2188,15 @@ VALGRIND_CMD="$VALGRIND $VALGRIND_OPTS $VALGRIND_ARGS"
 ARGS_POSTFIX="$BINARY_C_ARGS_POSTFIX"
 ARGS_PREFIX="$BIN_C"
 
-if [ "$RUNMODE" == "debug" -o "$RUNMODE" == "gdb" ];
+if [ "$RUNMODE" == "debug" ] || \
+   [ "$RUNMODE" == "gdb" ];
 then
     # run through the GNU debugger (gdb)
     ARGS_PREFIX="gdb -ex=r $BIN_C --args $ARGS_PREFIX "
    
-elif [ "$RUNMODE" == "valgrind" -o\
-                  "$RUNMODE" == "valgrind_args"\
-                  -o "$RUNMODE" == "valgrind_cmd" ];
+elif [ "$RUNMODE" == "valgrind" ] || \
+         [ "$RUNMODE" == "valgrind_args" ] || \
+         [ "$RUNMODE" == "valgrind_cmd" ];
 then
     # run through valgrind's memcheck
     # See: http://valgrind.org/docs/manual/manual.html
@@ -2210,7 +2209,7 @@ then
         ARGS_PREFIX="echo $ARGS_PREFIX"
     fi
     if [ "$RUNMODE" == "valgrind_cmd" ]; then
-        echo $VALGRIND $VALGRIND_OPTS
+        echo "$VALGRIND $VALGRIND_OPTS"
         exit
     fi
 elif [ "$RUNMODE" == "massif" ];
@@ -2279,7 +2278,7 @@ elif [ "$RUNMODE" == "gprof_with_stdout" ];
 then
     # run through gprof, showing stdout
     # (requires CFLAGS="-pg" during configure step)
-    POSTCOMMAND=("gprof $BIN_C") 
+    POSTCOMMANDS=("gprof $BIN_C") 
 elif [ "$RUNMODE" == "gcov" ];
 then
 
@@ -2320,20 +2319,20 @@ then
     FILE=$1
     echo "Detected args file $FILE, reading from lines" 
     shift
-    while read ARGS; do
+    while read -r ARGS; do
         echo "READ $ARGS from $FILE"
         # run binary_c
-        $ARGS_PREFIX $ARGS $ARGS_POSTFIX $@
+        "$ARGS_PREFIX $ARGS $ARGS_POSTFIX" "$@"
 
         ERROR=$? # save return code and exit if non-zero
-        if (($ERROR != 0)); then
+        if ((ERROR != 0)); then
             echo "Error $ERROR in binary_c run, aborting args from file"
             echo "Args were"
-            echo $ARGS
+            echo "$ARGS"
             echo
             exit $ERROR
         fi
-    done < <(cat $FILE | grep -v \# |    sed ':a;N;$!ba;s/\n/ /g')
+    done < <(grep -v \# "$FILE" |    sed ':a;N;$!ba;s/\n/ /g')
     exit
 elif [[ -n "$argfiles" ]];
 then
@@ -2350,23 +2349,23 @@ then
         }
 
         # loop over the args file, strip dodgy data
-        while read ARGS; do
+        while read -r ARGS; do
             # ignore empty strings
-            if nonemptystring $ARGS; then
+            if nonemptystring "$ARGS"; then
                 # remove leading 'binary_c' if given
-                ARGS=$(echo $ARGS | sed -e s/^binary_c//) 
+                ARGS="$(echo "$ARGS" | sed -e s/^binary_c//)" 
                 
                 # remove leading date if given
-                ARGS=$(echo $ARGS | sed -e s/At\ 20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]\ [0-9][0-9]\:[0-9][0-9]\:[0-9][0-9]\ \:\ //)
+                ARGS="$(echo "$ARGS" | sed -e s/At\ 20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]\ [0-9][0-9]\:[0-9][0-9]\:[0-9][0-9]\ \:\ //)"
 
                 # run binary_c
-                $ARGS_PREFIX $DEFAULT_ARGS $ARGS $ARGS_POSTFIX $@ $FIRST_TBSE 
+                "$ARGS_PREFIX $DEFAULT_ARGS $ARGS $ARGS_POSTFIX" "$@" "$FIRST_TBSE" 
 
                 ERROR=$? # save return code and exit if non-zero
-                if (($ERROR != 0)); then
+                if (("$ERROR" != 0)); then
                     echo "Error $ERROR in binary_c run, aborting args from file"
                     echo "Args were"
-                    echo $ARGS
+                    echo "$ARGS"
                     echo
                     exit $ERROR
                 fi
@@ -2375,7 +2374,7 @@ then
                     FIRST_TBSE=""
                 fi
             fi
-        done < <(grep -v \# $argfile )
+        done < <(grep -v \# "$argfile" )
     done
     exit
 
@@ -2383,24 +2382,24 @@ then
 elif [[ $USE_CMDLINE_ARGS_ONLY -eq "1" ]];
 then
     # use only passed in arguments
-    ARGS="$@"
+    ARGS="$*"
 else
     # use default arg string
-    ARGS="$DEFAULT_ARGS $@"
+    ARGS="$DEFAULT_ARGS $*"
 fi
 
 
 if [ "$RUNMODE" == "echo" ];
 then
     # print out the current arguments (in one line) and exit
-    echo $ARGS;
+    echo "$ARGS";
     exit
 fi
 
 if [ "$RUNMODE" == "echolines" ];
 then
     # print out the current arguments (with newlines) and exit
-    echo $ARGS | sed s/--/\\n/g
+    echo "$ARGS" | sed s/--/\\n/g
     exit
 fi
 
@@ -2416,97 +2415,12 @@ where N is the number of systems to run, per CPU core, in a timing test.
 "
     exit
     
-    # profile-guided optimisation build: this option 
-    # configures, builds, runs, rebuilds and reruns bonnfires
-    # so that it uses profile-guided optimisation, i.e.
-    # this is as fast as it gets
-    # note that this is NOT for debugging: output is sent to /dev/null
-
-    # options
-    REPEAT=500
-    SLEEP=0
-    NCORES=`cat /proc/cpuinfo |grep -i Processor | wc -l`
-    REPEAT_PER_CORE=$(($REPEAT/$NCORES))
-    verbose=0
-    PRE_COMMAND="ionice -c3 nice -n $NICELEVEL"
-    # args to apply for the test
-    POST_COMMAND="--repeat $REPEAT_PER_CORE --random_systems 1 --warmup_cpu 3"
-
-    # kill children on exit
-    trap '[ -n "$(jobs -pr)" ] && kill $(jobs -pr)' INT QUIT TERM EXIT
-
-    # set up files 3,4 which are captured output
-    if ((verbose)); then
-        echo "Profile-guided optimisation : verbose mode"
-        exec 3>&1
-        exec 4>&2
-    else
-        echo "Profile-guided optimisation : quiet mode"
-        exec 3>/dev/null
-        exec 4>/dev/null
-    fi
-    TMP=/var/tmp
-
-    ### phase 1
-    echo "Build PGO phase 1"
-    export CFLAGS="-fprofile-generate"
-    export LDFLAGS="-fprofile-generate"
-    ./configure 1>&3 2>&4
-    make -j $NCORES cleanall 1>&3 2>&4
-    ./configure 1>&3 2>&4
-    make -j $NCORES 1>&3 2>&4
-
-    BIN_C_PGO1=$TMP/binary_c_pgo1
-    cp $BIN_C $BIN_C_PGO1
-
-    echo "Run PGO phase 1 (repeat $REPEAT random systems on $NCORES cores; $REPEAT_PER_CORE per core, executable $BIN_C_PGO1)"
-    START=`date +%s%N`
-    NCORES1=$((NCORES - 1))
-    for i in `seq 0 $NCORES1`; do
-        $PRE_COMMAND taskset -c $i $BIN_C_PGO1 $ARGS $POST_COMMAND >/dev/null &
-        sleep $SLEEP
-    done
-    wait
-    END=`date +%s%N`
-    PGO1_DURATION=$(echo "(0.000000001*($END - $START))"|bc)
-    echo "... took $PGO1_DURATION seconds to run all $REPEAT systems (run 1)"
-    
-    ### phase 2
-    export CFLAGS="-fprofile-use -Wno-error=coverage-mismatch"
-    export LDFLAGS="-fprofile-use"
-    echo "Build PGO phase 2"
-    ./configure 1>&3 2>&4
-    make -j $NCORES clean 1>&3 2>&4
-    make -j $NCORES 1>&3 2>&4
-    BIN_C_PGO2=$TMP/binary_c_pgo2
-    cp $BIN_C $BIN_C_PGO2
-
-    echo "Run PGO phase 2 (repeat $REPEAT random systems on $NCORES cores; $REPEAT_PER_CORE per core, executable $BIN_C_PGO2)"
-    START=`date +%s%N`
-    for i in `seq 0 $NCORES1`; do
-        $PRE_COMMAND taskset -c $i $BIN_C_PGO2 $ARGS $POST_COMMAND >/dev/null &
-        sleep $SLEEP
-    done
-    wait
-    END=`date +%s%N`
-    PGO2_DURATION=$(echo "(0.000000001*($END - $START))"|bc)
-    echo "... took $PGO2_DURATION seconds to run all $REPEAT systems (run 2)"
-    echo
-
-    echo "Run 1 took : $PGO1_DURATION s"
-    echo "Run 2 took : $PGO2_DURATION s"
-    
-    # final speedup %
-    echo -n "Profile guided optimization speedup is "
-    echo -n $(echo "(100.0*($PGO1_DURATION-$PGO2_DURATION)/$PGO1_DURATION)"|bc)
-    echo "%"
-    exit
 
 elif [ "$RUNMODE" == "bug" ];
 then
     # run in bug report mode
-    DATE=`date +%Y%m%d%H%M%S`
-    TMPDIR=$(mktemp -d /tmp/tbsebug.XXXXXX)
+    DATE="$(date +%Y%m%d%H%M%S)"
+    TMPDIR="$(mktemp -d /tmp/tbsebug.XXXXXX)"
     ERRFILE="$TMPDIR/err"
     OUTFILE="$TMPDIR/out"
     ARGFILE="$TMPDIR/args"
@@ -2515,32 +2429,32 @@ then
     SVNSTATFILE="$TMPDIR/svnstat"
     SVNVERSIONFILE="$TMPDIR/svnversion"
     MAKEFILE="$TMPDIR/Makefile"
-    VERSION=`binary_c --dumpversion`
+    VERSION=$(binary_c --dumpversion)
     TARBZFILE="binary_c$VERSION.bugreport$DATE.tar.bz2"
     FILES="args out err version svninfo svnstat svnversion Makefile"
     
     echo "Running tbse for standard bug report, please wait..."
 
     # collect information    
-    echo "$ARGS_PREFIX $ARGS" > $ARGFILE
-    svn info 2>&1 > $SVNINFOFILE
-    svn stat 2>&1 > $SVNSTATFILE
-    svn --version 2>&1 > $SVNVERSIONFILE
-    cp src/Makefile $MAKEFILE
-    $ARGS_PREFIX $ARGS --version 2>&1 > $VERSIONFILE
+    echo "$ARGS_PREFIX $ARGS" > "$ARGFILE"
+    svn info > "$SVNINFOFILE" 2>&1 
+    svn stat > "$SVNSTATFILE" 2>&1
+    svn --version > "$SVNVERSIONFILE" 2>&1
+    cp src/Makefile "$MAKEFILE"
+    "$ARGS_PREFIX" "$ARGS" --version > "$VERSIONFILE" 2>&1
 
     # run tbse
-    $ARGS_PREFIX $ARGS 2> $ERRFILE > $OUTFILE
+    "$ARGS_PREFIX" "$ARGS" > "$OUTFILE" 2> "$ERRFILE" 
     
     # make tarball
-    tar -C $TMPDIR -j -c -f $TARBZFILE $FILES
-    filesize $TARBZFILE
+    tar -C "$TMPDIR" -j -c -f "$TARBZFILE" "$FILES"
+    filesize "$TARBZFILE"
 
     # recommand user emails it to Rob
     echo "Please send $TARBZFILE (size $SIZE bytes) to Rob"
 
-elif [ "$RUNMODE" == "multicore" \
-    -o "$RUNMODE" == "multicore_valgrind" ];
+elif [ "$RUNMODE" == "multicore" ] || \
+         [ "$RUNMODE" == "multicore_valgrind" ];
 then
 
     if [ "$RUNMODE" == "multicore_valgrind" ]
@@ -2552,21 +2466,19 @@ then
     
     # run on many cores at once,each with their own logfile
     #echo "Run binary_c on $NCORES cores"
-    for i in `seq 0 $NCORES`; do
+    for i in $(seq 0 "$NCORES"); do
         LOGFILE="/tmp/c_log$i.dat"
         echo "Run on core $i, log in $LOGFILE"
-        $VALGRIND_ $ARGS_PREFIX $ARGS $ARGS_POSTFIX --log_filename $LOGFILE  &
+        "$VALGRIND_" "$ARGS_PREFIX" "$ARGS" "$ARGS_POSTFIX" --log_filename "$LOGFILE"  &
         PIDLIST="$PIDLIST $!"
     done
     
     # wait for each binary_c to finish, or exit on first error
     # taken from http://jeremy.zawodny.com/blog/archives/010717.html
     # and the subsequent comments
-    FAIL=0
     for job in $PIDLIST
     do
-        #wait $job || let "FAIL+=1" 
-        wait $job
+        wait "$job"
         STATUS=$?
         FAILEDPID=$$
     done
@@ -2585,7 +2497,7 @@ else
     ############################################################
     ### normal single-system run
     ############################################################
-    $ARGS_PREFIX $ARGS $ARGS_POSTFIX
+    "$ARGS_PREFIX" "$ARGS" "$ARGS_POSTFIX"
     STATUS=$?
     if [ "$VERBOSE" -eq "1" ];
     then
@@ -2598,7 +2510,7 @@ else
     fi   
 
     # save exit code for later
-    EXIT_CODE=$STATUS
+    EXIT_CODE="$STATUS"
 fi
 
 # run any given postcommands if binary_c exited with code 0 :
@@ -2609,7 +2521,7 @@ if [ "$EXIT_CODE" -eq "0" ]; then
     for c in "${POSTCOMMANDS[@]}"; do
         echo "Run postcommand $I = '$c'"
         eval "$c"
-        I=$(( $I+1 ))
+        I=$(( I+1 ))
     done
 fi
 
-- 
GitLab