bash library

 

 


判斷是否作 Library 使用

 

[1]

if [ "${BASH_SOURCE[0]}" -ef "$0" ]
then
    echo "Hey, you should source this script, not execute it!"
    exit 1
fi

Under bash, ${BASH_SOURCE[0]} will contain the name of the current file
    that the shell is reading regardless of whether it is being sourced or executed.

[2]

#!/bin/false "This script should be sourced, not executed directly"

原因: 防治有人用 shell 去 call 佢 `bash somefile.sh`

 

Creative Commons license icon Creative Commons license icon