From 0a465125cbd589d12d3e8f56c5a4abfa32a49494 Mon Sep 17 00:00:00 2001 From: Christophe Biocca Date: Fri, 12 Sep 2014 10:28:47 -0400 Subject: [PATCH 1/2] Allows profile file to work on pathmunge-less systems. Will check for pathmunge's existence and use it, or fallback to prepending to the path. --- tools/profile.d-usrblackbox.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/profile.d-usrblackbox.sh b/tools/profile.d-usrblackbox.sh index 6c69b47..5b1106b 100644 --- a/tools/profile.d-usrblackbox.sh +++ b/tools/profile.d-usrblackbox.sh @@ -1,2 +1,7 @@ # Prepend to $PATH. -pathmunge /usr/blackbox/bin + +if type pathmunge > /dev/null 2>&1 ; then + pathmunge /usr/blackbox/bin +elif ! echo $PATH | grep -Eq "(^|:)/usr/blackbox/bin($|:)" ; then + PATH=/usr/blackbox/bin:$PATH +fi From 8db38905def57e609ac7c415da3dfec0bc52ce7e Mon Sep 17 00:00:00 2001 From: "tlimoncelli@stackexchange.com" Date: Fri, 12 Sep 2014 16:17:35 +0000 Subject: [PATCH 2/2] Reduce size of profile.d-usrblackbox.sh to below 132 bytes. Add test framework to make sure we don't break bash or sh. --- tools/profile.d-usrblackbox-test.sh | 44 +++++++++++++++++++++++++++++ tools/profile.d-usrblackbox.sh | 13 ++++----- 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100755 tools/profile.d-usrblackbox-test.sh diff --git a/tools/profile.d-usrblackbox-test.sh b/tools/profile.d-usrblackbox-test.sh new file mode 100755 index 0000000..df89f9b --- /dev/null +++ b/tools/profile.d-usrblackbox-test.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +# Test profile.d-usrblackbox.sh + +# Make sure profile.d-usrblackbox.sh works. + +# Test variations including /usr/blackbox/bin is not in the path, is +# already there in the front, middle, or end, and tests if the path has : +# in weird places (front, middle, or both). + +# To run the test: +# bash tools/profile.d-usrblackbox-test.sh | fgrep --color /usr/blackbox/bin +# sh profile.d-usrblackbox-test.sh | fgrep --color /usr/blackbox/bin + +# NOTE: profile.d-usrblackbox.sh is written to be so small that it fits as an "inline" file. +# https://lwn.net/Articles/468678/ +# To remove the last newline in the file: +# perl -i -pe 'chomp if eof' profile.d-usrblackbox.sh + +for p in \ + '/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin' \ + '/usr/blackbox/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin' \ + '/usr/local/bin:/bin:/usr/blackbox/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin' \ + '/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/blackbox/bin' \ + '/Apple spaces/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin' \ + ; do + + export PATH="$p" + . tools/profile.d-usrblackbox.sh + echo NEW: "$PATH" + + export PATH=":$p" + . tools/profile.d-usrblackbox.sh + echo NEW: "$PATH" + + export PATH="$p:" + . tools/profile.d-usrblackbox.sh + echo NEW: "$PATH" + + export PATH=":$p:" + . tools/profile.d-usrblackbox.sh + echo NEW: "$PATH" + +done diff --git a/tools/profile.d-usrblackbox.sh b/tools/profile.d-usrblackbox.sh index 5b1106b..9a7f51b 100644 --- a/tools/profile.d-usrblackbox.sh +++ b/tools/profile.d-usrblackbox.sh @@ -1,7 +1,6 @@ -# Prepend to $PATH. - -if type pathmunge > /dev/null 2>&1 ; then - pathmunge /usr/blackbox/bin -elif ! echo $PATH | grep -Eq "(^|:)/usr/blackbox/bin($|:)" ; then - PATH=/usr/blackbox/bin:$PATH -fi +x=/usr/blackbox/bin +if type pathmunge >/dev/null 2>&1;then +pathmunge $x +elif ! grep -sqF :$x:<<<":$PATH:";then +PATH="$x:$PATH" +fi \ No newline at end of file