[meta-freescale] [PATCH][fsl-community-bsp-base] setup-environment: check script has been sourced

Trevor Woerner trevor.woerner at linaro.org
Wed Jul 9 08:25:28 PDT 2014


Check that this script has been invoked correctly (i.e. that it has been
"source"d and not merely run directly). If it has been run directly, don't
exit immediately in case the user specified the "help" option, allow the help
to be displayed, then exit. Adjust how this script is terminated based on
whether it has been sourced or run directly (i.e. use either "return" or
"exit" as required).

This change fixes an infinite loop that is caused if the user runs this script
directly and specifies the help option.

These changes have been tested on: bash, dash, and zsh.

Signed-off-by: Trevor Woerner <trevor.woerner at linaro.org>
---
 setup-environment | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/setup-environment b/setup-environment
index 5931e2b..d3f89ce 100755
--- a/setup-environment
+++ b/setup-environment
@@ -24,6 +24,23 @@ NCPU=`grep -c processor /proc/cpuinfo`
 CWD=`pwd`
 PROGNAME="setup-environment"
 
+# try to determine if this script has been invoked correctly
+# in other words, make sure it has been "source"d
+# don't exit out immediately, allow the user to ask for -h
+INVOKEGOOD=1
+if [ -n "$ZSH_NAME" ]; then
+    # check for zsh
+    if [ -n "$BASH_SOURCE" ]; then
+        INVOKEGOOD=0
+    fi
+else
+    # check for bash, sh, and dash
+    echo $0 | grep -q $PROGNAME
+    if [ $? -eq 0 ]; then
+        INVOKEGOOD=0
+    fi
+fi
+
 usage()
 {
     echo -e "\nUsage: source $PROGNAME <build-dir>
@@ -55,6 +72,10 @@ ARGS=$(getopt --options $SHORTOPTS  \
   --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
 # Print the usage menu if invalid options are specified
 if [ $? != 0 -o $# -lt 1 ]; then
+   if [ $INVOKEGOOD = 0 ]; then
+       echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
+       exit 1
+   fi
    usage && clean_up
    return 1
 fi
@@ -66,6 +87,9 @@ do
         -h|--help)
            usage
            clean_up
+           if [ $INVOKEGOOD = 0 ]; then
+               exit 0
+           fi
            return 0
            ;;
         --)
@@ -75,6 +99,11 @@ do
     esac
 done
 
+if [ $INVOKEGOOD = 0 ]; then
+    echo "Error: This script needs to be sourced. Please run as '. $PROGNAME'"
+    exit 1
+fi
+
 if [ "$(whoami)" = "root" ]; then
     echo "ERROR: do not use the BSP as root. Exiting..."
 fi
-- 
2.0.0.5.gbce14aa



More information about the meta-freescale mailing list