shell 实现类似php的require_once函数
config.sh
#/bin/bash require_once() { #File the true path ,To prevent a symbolic link local realpath=$(readlink -f "$1") local basedir=$(dirname "$realpath") local file_full_name=$(basename "$realpath") local file_name=$(remove_suffix "$file_full_name"); #require file once if [ `echo "$FILE_COLLECTION"|sed -n "/$file_name/p"` ]; then return; fi export FILE_COLLECTION=$FILE_COLLECTION:$file_name export PATH=$PATH:$basedir . "$file_full_name" } remove_suffix() { local filename=$(basename "$1") echo "$filename" | sed 's/.sh$//' }
main.sh
#/bin/bash . ./config.sh require_once 'module/go.sh' require_once 'lib/function.sh' started iped
test@test:/data/tmp/bak/shell$ ./main.sh started started iped