bash-it
bash-it copied to clipboard
A more convenient method for controlling the sourcing order of customized scripts.
Hi,
Currently, the sourcing order of the customized scripts are controlled by the following code snippet in https://github.com/Bash-it/bash-it/blob/master/bash_it.sh:
# Custom
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash"
for _bash_it_config_file in $CUSTOM
do
if [ -e "${_bash_it_config_file}" ]; then
# shellcheck disable=SC1090
source "$_bash_it_config_file"
fi
done
But this is not so convenient if we wan to have more control on the sourcing order of the customized scripts.
So, I suggest the following codes for doing the job instead of the above ones:
# Custom
# A more convenient method for controlling the sourcing order of customized scripts:
# The scripts in begin folder will be sourced firstly and the ones in the end folder will be
# sourced lastly.
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/begin/*.bash \
${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash \
${BASH_IT_CUSTOM:=${BASH_IT}/custom}/end/*.bash"
for _bash_it_config_file in $CUSTOM
do
if [ -e "${_bash_it_config_file}" ]; then
# shellcheck disable=SC1090
source "$_bash_it_config_file"
fi
done
Any comments on this idea? If you agree, I will make a PR.
Regards
Couldn't you achieve the same effect by using a naming convention for the custom scripts, with a number prefix, e.g. like this:
- 00_begin_custom.bash
- 10_init_custom.bash
- 99_end_custom.bash
The glob that lists the files (CUSTOM
variable in the above snippet) should order them alphabetically by default, I think.
The problem is that the scripts to be sourced are symlinks pointing to somewhere outside of the custom folder. And I have already some other naming specifications for these scripts. In this case, I don't want to change the naming spec for these scripts.
Any hints?
Any I have more scripts in the custom folder which maybe startwith alphabets, in this case, they will be sourced after number-beginning ones.
Regards
For your symlinked custom scripts, you could use a different name for the symlink than for the source script in a different location, e.g. like this:
ln -s ~/.bash_it/custom/00_aliases.bash ~/foo/bar/aliases.bash
This is the way the Bash-it plugins, aliases and completions are enabled and then loaded.
I'm not a fan of introducing more complexity in the way these things are handled. Maybe you can look into renaming/linking your custom scripts to work like that.
What are your naming standards for the custom scripts? Maybe there's something there to be reused...
Thanks a lot. This is more graceful then my idea and still can solve my problem. Thanks for you suggestions.
I mainly use some code like the following in the script to obtain the script's pyhisical dirname, basename, and so on:
topdir=$(
cd -P -- "$(dirname -- "$(realpath -e -- "${BASH_SOURCE[0]}")" )" &&
pwd -P
)
# In the following method, the $DIRNAME is equivalent to $topdir otained above in this script:
#https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
FULLPATH="$( realpath -e -- "${BASH_SOURCE[0]}")"
FILENAME="${FULLPATH##*/}" # Strip longest match of */ from start
# Revise, removed the trailling /:
DIRNAME="${FULLPATH:0:${#FULLPATH} - ${#FILENAME} - 1}" # Substring from 0 thru pos of filename
FILE_BASENAME="${FILENAME%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
FILE_EXTNAME="${FILENAME:${#FILE_BASENAME} + 1}" # Substring from len of base thru end
if [[ -z "$FILE_BASENAME" && -n "$FILE_EXTNAME" ]]; then # If we have an extension and no base, it's really the base
FILE_BASENAME=".$FILE_EXTNAME"
ext=""
fi
echo -e "\tFULLPATH = \"$FULLPATH\"\n\tDIRNAME = \"$DIRNAME\"\n\tFILE_BASENAME = \"$FILE_BASENAME\"\n\tFILE_EXTNAME = \"$FILE_EXTNAME\""
Regards
Couldn't you achieve the same effect by using a naming convention for the custom scripts, with a number prefix, e.g. like this:
* 00_begin_custom.bash * 10_init_custom.bash * 99_end_custom.bash
If I want to use this spec for the scripts added by me in the system's /etc/profile.d folder, due to there are some scripts created by system and these scripts are not named after the above spec.
So, in this case, the above spec may not ensure the souring order.
Any hints for this case?
The glob that lists the files (
CUSTOM
variable in the above snippet) should order them alphabetically by default, I think.