From: Darshaka Pathirana Date: Fri, 5 Feb 2021 13:56:00 +0000 (+0100) Subject: usr_bin/random-hostname: Fix a couple of shellcheck warnings X-Git-Tag: v2.11.0~1^2~19 X-Git-Url: http://git.grml.org/?p=grml-scripts.git;a=commitdiff_plain;h=2980f79718344ea7bbb6093beb9b6bac49b5566d usr_bin/random-hostname: Fix a couple of shellcheck warnings - Ignore SC2034: hostnames appears unused. Verify use (or export if used externally). While at it I rewrote the way how the random hostname is retrieved: First find the line number 3 lines after the line which starts with "# EOF" This is the line where the list of hostnames start. This makes the script a bit more robust and avoids the hard coded start line calculation ("num=$(( $num - 16 ))"). Then list all lines from there by excluding all empty commented out lines and the last line (which start with "'"). Use "sort -R" to randomly sort the list and from there take the last line. That way the confusing "randline" variable calculation is avoided. --- diff --git a/usr_bin/random-hostname b/usr_bin/random-hostname index b523523..7386abb 100755 --- a/usr_bin/random-hostname +++ b/usr_bin/random-hostname @@ -5,13 +5,13 @@ # Bug-Reports: see http://grml.org/bugs/ # License: This file is licensed under the GPL v2. ################################################################################ -num=$(wc -l $0) -num=${num%% $0} -num=$(( $num - 16 )) -randline=$(($(date +%Y%M%S) * $$ % $num)) -hostname=$(tail -n $randline "$0" | head -1 | grep -v "'" | grep -v '^#' | grep -v '^$') +hostname_start=$(grep -n "^# EOF" "$0") +hostname_start=${hostname_start%%:#*} +hostname_start=$(( hostname_start + 3 )) +hostname=$(tail -n +$hostname_start "$0" | grep -v "'" | grep -v '^#' | grep -v '^$' | sort -R | tail -1) [ -n "$hostname" ] && echo "$hostname" || echo "grml" -# EOF - line 15; now the hostnames: +# EOF - now the hostnames: +# shellcheck disable=SC2034 hostnames=' 23 42