Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload complete dynamically bash

Tags:

bash

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'?

like image 567
ShadowFlame Avatar asked Jun 11 '26 04:06

ShadowFlame


2 Answers

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
like image 194
gniourf_gniourf Avatar answered Jun 13 '26 18:06

gniourf_gniourf


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.

like image 31
David C. Rankin Avatar answered Jun 13 '26 16:06

David C. Rankin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!