LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-08-2010, 12:52 AM   #1
platypuus
LQ Newbie
 
Registered: May 2010
Posts: 4

Rep: Reputation: 0
bash completion


hello,

I have been trying for a couple days to figure this out to no avail. I am converting some csh code to bash. I have converted everything except the completion code.

#bashrc (I set this alias in my bashrc)
alias test='source ${PATH}/test.sh'

#${PATH}/test.sh (returns some aliases and env variables to the current shell)
ARGS="$@"
eval "`/usr/bin/python ${BPATH}/test $ARGS`"

#${PATH}/test.py (has options -h "help" -c "config" -p "packages")

#${PATH}/configurationsandpackages.py
# returns available configurations and packages
# so if I run /usr/bin/python ${PATH}/configurationsandpackages.py in a shell it returns a list of configurations and if I run /usr/bin/python ${PATH}/configurationsandpackages.py 1 it returns a list of packages

so my problem is that for -c and -p I want to set up completion to give me that list of configs or packages, ie:

localhost:~ medwards$ test -c<TAB>
default development etc
localhost:~ medwards$ test -c

localhost:~ medwards$ test -p<TAB>
environment defaults etc
localhost:~ medwards$ test -p

so in my bashrc after create the alias test='source ${PATH}/test.sh'
I want to create completion code to accomplish the above stated.

this works in csh.
complete test 'n@-c@`/usr/bin/python '${PATH}'/configurationsandpackages.py `@' 'n@-p@`/usr/bin/python '${PATH}'/configurationsandpackages.py 1`@'

I appreciate any insight, I have never programmed completion before and can find very little useful info from google.

regards, mark
 
Old 05-08-2010, 10:58 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,497

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Bash manual: Programmable Completion.

Also the Debian bash-completion package adds completion for the package tools (among others) so you could look at how it's done there.
 
Old 05-08-2010, 12:12 PM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,768

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Using the name 'test' for a function, alias or script is a bad idea since it is a shell builtin.
 
Old 05-08-2010, 12:49 PM   #4
platypuus
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
Bash manual: Programmable Completion.

Also the Debian bash-completion package adds completion for the package tools (among others) so you could look at how it's done there.
yeah, I dont really want to add a package for what I can do in one line in tcsh. I want to replicate what I have in tcsh in bash.
 
Old 05-08-2010, 12:50 PM   #5
platypuus
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by gnashley View Post
Using the name 'test' for a function, alias or script is a bad idea since it is a shell builtin.
yeah, my script isnt named test I just put a generic name in my post to simplify things.
 
Old 05-08-2010, 02:28 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,497

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Quote:
yeah, I dont really want to add a package for what I can do in one line in tcsh. I want to replicate what I have in tcsh in bash.
I wasn't suggesting you install that package, just look at the source to see how it's done.

Anyways it looks like the bash completion functions aren't as terse as tcsh, the following seems to work for me:
Code:
#!/bin/bash

_test()
{
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    COMPREPLY=()

    case "$prev" in
        -p) COMPREPLY=( $(compgen -W "$(echo first-pkg second-pkg third-pkg)" -- "$cur") );;
        -c) COMPREPLY=( $(compgen -W "$(echo first-conf second-conf third-conf)" -- "$cur") );;
        *) COMPREPLY=( $(compgen -W '-p -c' -- "$cur") );;
    esac
}

complete -F _test test
Where you would replace $(echo first-pkg second-pkg third-pkg) with the appropriate command.
 
1 members found this post helpful.
Old 05-09-2010, 09:50 PM   #7
platypuus
LQ Newbie
 
Registered: May 2010
Posts: 4

Original Poster
Rep: Reputation: 0
thanks, that worked great. This is what I ended up with in my bashrc...

alias test='source ${PATH}/test.sh'
complete -F _test test
test -c config-1
_test()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()

case "$prev" in
-p) COMPREPLY=( $(compgen -W "$(echo '`/usr/bin/python ${PATH}/configurationsandpackages.py`')" -- "$cur") );;
-c) COMPREPLY=( $(compgen -W "$(echo '`/usr/bin/python ${PATH}/configurationsandpackages.py`')" -- "$cur") );;
*) COMPREPLY=( $(compgen -W "$(echo -c -p -a -d -D -v -l -L)" -- "$cur") );;
esac
}
 
Old 05-09-2010, 10:47 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,497

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Code:
"$(echo '`/usr/bin/python ${PATH}/configurationsandpackages.py`')"
can replaced with
Code:
"$(/usr/bin/python ${PATH}/configurationsandpackages.py)"
and
Code:
"$(echo -c -p -a -d -D -v -l -L)"
with
Code:
'-c -p -a -d -D -v -l -L'
 
Old 05-18-2010, 11:17 PM   #9
JanTomka
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
To figure out how to write a bash completion script, please, have a look at the code I've put together: http://jan.tomka.name/project/clearcase-completion-bash
 
  


Reply

Tags
bash, bashrc, complete, csh, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling wordbreaks in bash completion azrael_ru Linux - General 2 02-18-2010 08:06 AM
Tab-Completion in bash script Buto Linux - General 1 12-04-2007 06:50 PM
xterm and bash completion athanatos Slackware 12 07-22-2006 12:47 PM
question about bash completion Guest1234 Linux - Software 4 02-25-2006 04:09 PM
Can we enable bash completion in Sarge?How?Please help... deepclutch Debian 6 12-02-2005 07:02 PM


All times are GMT -5. The time now is 06:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration