Andrew M. on 18 Apr 2019 15:24:27 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Determining when a shell script is sourced


Had a few minutes to kill... How about this... 

#!/usr/bin/env sh

# Try to execute a `return` statement,
# but do it in a sub-shell and catch the results.
# If this script isn't sourced, that will raise an error.
$(return >/dev/null 2>&1)

# What exit code did that give?
if [ "$?" -eq "0" ]
then
    echo "This script is sourced."
else
    echo "This script is not sourced."
fi

https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced#34642589


On Thu, Apr 18, 2019, at 5:28 PM, K.S. Bhaskar wrote:
We have a shell script (starts with #!/binsh and is at https://gitlab.com/YottaDB/DB/YDB/blob/master/sr_unix/ydbinstall.sh) that should preferably not be source'd. Is there a way to test within the script whether someone has source'd it and generate an error without terminating the shell session that source'd it? Thank you very much.

Regards
– Bhaskar
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug


___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug