Reload bash dynamically
I wrote a custom complete line that I upload on Solaris using the following line in my .profile:
source .deploymentrc
Content of the specified file:
TODAY=`date +%Y%m%d`
scriptDir="/bea/user_projects/deployment/scripts/$TODAY"
complete -W "$(echo `ls $scriptDir | uniq `;)" ./deploy
However, the / bea / user_projects / deployment / scripts / $ TODAY folder is populated automatically by another script, and when something is added, the results of my tab completion are out of date. Is there a way to keep this update or rerun the full command when I press TAB twice?
source to share
You will always have a possible file mismatch problem scriptDir
due to the time elapsed between you sourcing .deploymentrc
and your command execution. A better approach would be to create a function in ~/.bashrc
this script:
function deployment {
TODAY=`date +%Y%m%d`
scriptDir="/bea/user_projects/deployment/scripts/$TODAY"
complete -W "$(echo `ls $scriptDir | uniq `;)" ./deploy
}
Then enter a short alias:
alias depupdate='deployment'
Instead of searching and then using a tab, you can simply execute the command depupdate
to recreate your uniq list at the time the alias is issued. Otherwise, you will always be able to add additional files between source and run.
This is one way to help solve the problem.
source to share
Don't use a fixed set of words, but use a function that generates completion:
today=$(date +%Y%m%d)
scriptDir="/bea/user_projects/deployment/scripts/$today"
complete_deploy() {
local oldnullglob=$(shopt -p nullglob)
shopt -s nullglob
COMPREPLY=( "$scriptDir"/* )
eval "$oldnullglob"
}
complete -F complete_deploy ./deploy
The magic in complete_deploy
is just a string COMPREPLY=( "$scriptDir"/* )
. The other lines only apply to the shell option nullglob
: it saves the state, sets it (since we're using glob), and restores its state.
You can also automatically create the date in this function:
complete_deploy() {
local today=$(date +%Y%m%d)
local scriptDir="/bea/user_projects/deployment/scripts/$today"
local oldnullglob=$(shopt -p nullglob)
shopt -s nullglob
COMPREPLY=( "$scriptDir"/* )
eval "$oldnullglob"
}
complete -F complete_deploy ./deploy
or, if you have Bash β₯4.2, you will save the outer process every time you press the tab key:
complete_deploy() {
local scriptDir;
printf -v scriptDir "/bea/user_projects/deployment/scripts/%(%Y%m%d)" -1
local oldnullglob=$(shopt -p nullglob)
shopt -s nullglob
COMPREPLY=( "$scriptDir"/* )
eval "$oldnullglob"
}
complete -F complete_deploy ./deploy
source to share