I have written a custom complete line, which I load on Solaris using the following line in my .profile:
source .deploymentrc
The content of said file:
TODAY=`date +%Y%m%d`
scriptDir="/bea/user_projects/deployment/scripts/$TODAY"
complete -W "$(echo `ls $scriptDir | uniq `;)" ./deploy
However, the folder /bea/user_projects/deployment/scripts/$TODAY is filled automatically using another script, and when something is added, the results of my tab-completion are out of date. Is there a way to keep this up to date or to re-run the complete command when I double-tap 'TAB'?
Don't use a fixed set of words, but use a function that generates the completions:
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 only the line COMPREPLY=( "$scriptDir"/* ). The other lines only deal with the shell option nullglob: saves it state, set it (since we're using a glob) and restore its state.
You could also have the date generated automatically 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'll save an external process each time you hit 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
You will always have a possible problem with a mismatch of files in scriptDir due to the passage of time between you sourcing .deploymentrc and your execution of the command. A better approach would be to create a function in ~/.bashrc of this script:
function deployment {
TODAY=`date +%Y%m%d`
scriptDir="/bea/user_projects/deployment/scripts/$TODAY"
complete -W "$(echo `ls $scriptDir | uniq `;)" ./deploy
}
Then give it a short alias:
alias depupdate='deployment'
Rather than sourcing and then using tab completion, you could simply issue the depupdate command to recreate your uniq list at the point of issuing the alias. Otherwise, you will always have the possibility of additional files being added between the sourcing and execution.
This is one approach to help with the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With