Skip to main content
Changed $(echo $0) to $0, thanks to scott
Source Link

This is a my version of test, based on Hiroshi_U's answer.

I want to test a function in mylib.sh with $ source mylib.sh;sysLog Hey, the function will not be up-to-date if I have changed something afterwards and try to get the newer result out of it. I have to unset the _NAME_OF_THIS_LIBSCRIPT to make it new. Since $ echo $0 produces something like -sh and -bash, so I test for - to determine whether it run in a script file or under the shell.

mylib.sh

#!/bin/sh

MY_BASE=$(echo $0)MY_BASE=$0
[[ "${_NAME_OF_THIS_LIBSCRIPT:-""}" == "yes" ]] && return 0
[[ "${MY_BASE:0:1}" != "-" ]] && _NAME_OF_THIS_LIBSCRIPT=yes

function sysLOG() {
    echo $1 $_NAME_OF_THIS_LIBSCRIPT
}

test.sh

#!/bin/sh

source /tmp/mylib.sh
sysLOG 1
source /tmp/mylib.sh
sysLOG 2
source /tmp/mylib.sh
sysLOG 3

$ test.sh;echo $_NAME_OF_THIS_LIBSCRIPT
1 yes
2 yes
3 yes

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes

Without [[ "${MY_BASE:0:1}" != "-" ]] &&

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes
yes

This is a my version of test, based on Hiroshi_U's answer.

I want to test a function in mylib.sh with $ source mylib.sh;sysLog Hey, the function will not be up-to-date if I have changed something afterwards and try to get the newer result out of it. I have to unset the _NAME_OF_THIS_LIBSCRIPT to make it new. Since $ echo $0 produces something like -sh and -bash, so I test for - to determine whether it run in a script file or under the shell.

mylib.sh

#!/bin/sh

MY_BASE=$(echo $0)
[[ "${_NAME_OF_THIS_LIBSCRIPT:-""}" == "yes" ]] && return 0
[[ "${MY_BASE:0:1}" != "-" ]] && _NAME_OF_THIS_LIBSCRIPT=yes

function sysLOG() {
    echo $1 $_NAME_OF_THIS_LIBSCRIPT
}

test.sh

#!/bin/sh

source /tmp/mylib.sh
sysLOG 1
source /tmp/mylib.sh
sysLOG 2
source /tmp/mylib.sh
sysLOG 3

$ test.sh;echo $_NAME_OF_THIS_LIBSCRIPT
1 yes
2 yes
3 yes

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes

Without [[ "${MY_BASE:0:1}" != "-" ]] &&

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes
yes

This is my version of test, based on Hiroshi_U's answer.

I want to test a function in mylib.sh with $ source mylib.sh;sysLog Hey, the function will not be up-to-date if I have changed something afterwards and try to get the newer result out of it. I have to unset the _NAME_OF_THIS_LIBSCRIPT to make it new. Since $ echo $0 produces something like -sh and -bash, so I test for - to determine whether it run in a script file or under the shell.

mylib.sh

#!/bin/sh

MY_BASE=$0
[[ "${_NAME_OF_THIS_LIBSCRIPT:-""}" == "yes" ]] && return 0
[[ "${MY_BASE:0:1}" != "-" ]] && _NAME_OF_THIS_LIBSCRIPT=yes

function sysLOG() {
    echo $1 $_NAME_OF_THIS_LIBSCRIPT
}

test.sh

#!/bin/sh

source /tmp/mylib.sh
sysLOG 1
source /tmp/mylib.sh
sysLOG 2
source /tmp/mylib.sh
sysLOG 3

$ test.sh;echo $_NAME_OF_THIS_LIBSCRIPT
1 yes
2 yes
3 yes

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes

Without [[ "${MY_BASE:0:1}" != "-" ]] &&

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes
yes
Source Link

This is a my version of test, based on Hiroshi_U's answer.

I want to test a function in mylib.sh with $ source mylib.sh;sysLog Hey, the function will not be up-to-date if I have changed something afterwards and try to get the newer result out of it. I have to unset the _NAME_OF_THIS_LIBSCRIPT to make it new. Since $ echo $0 produces something like -sh and -bash, so I test for - to determine whether it run in a script file or under the shell.

mylib.sh

#!/bin/sh

MY_BASE=$(echo $0)
[[ "${_NAME_OF_THIS_LIBSCRIPT:-""}" == "yes" ]] && return 0
[[ "${MY_BASE:0:1}" != "-" ]] && _NAME_OF_THIS_LIBSCRIPT=yes

function sysLOG() {
    echo $1 $_NAME_OF_THIS_LIBSCRIPT
}

test.sh

#!/bin/sh

source /tmp/mylib.sh
sysLOG 1
source /tmp/mylib.sh
sysLOG 2
source /tmp/mylib.sh
sysLOG 3

$ test.sh;echo $_NAME_OF_THIS_LIBSCRIPT
1 yes
2 yes
3 yes

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes

Without [[ "${MY_BASE:0:1}" != "-" ]] &&

$ source mylib.sh;sysLog Hey;echo $_NAME_OF_THIS_LIBSCRIPT
Hey! yes
yes