LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-08-2008, 09:58 AM   #1
mmn357157
LQ Newbie
 
Registered: May 2007
Location: Bangalore
Distribution: RHEL
Posts: 20

Rep: Reputation: 0
commandline autocompletion for my shell script


Hi Guys,

I want to create a Linux shell script which will ask you for input and unlike a normal read command it should allow to use 'TAB' character to auto complete the input value. I will be able to use a list of words/names in a file to look for this auto completion.


The real requirement is like this:

I have a websites name-list in a file one name per line. My program should ask for a website name and if the user enters like 'www.mysi' and press 'TAB' for auto completion, it should expand like 'www.mysite.com'.

Any solution we have?

Thanks in advance

mmn357157
 
Old 09-08-2008, 10:47 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The read command can use the -e option to use readline functionality. However, there appears to be a bug with auto-completion and read, which can be seen with:

Code:
$ cat complete.sh 
#!/bin/bash

# set of URLs to match against
urls="http://example.com http://example.us http://example.net"

set -o emacs
bind 'set show-all-if-ambiguous on'
bind 'set completion-ignore-case on'
bind 'TAB:dynamic-complete-history'

# load bash's history list against which we'll attempt completion
for i in $urls ; do
    history -s $i
done

read -e -p "URL? " word
echo Value read: $word
If you run complete.sh and hit the TAB key, you'll see partial completion of the URL list specified in $urls. Once that partial completion is done, bash no longer continues working on completion from that point. The good news is that Control-P/Control-N and arrow keys work to cycle through the list.

Another possibility, which you'll have to work out, is to read only 1 character at a time, and test for the TAB character. When you read TAB, you perform your own completion of the current input against your word list using compgen (eg. compgen -W "$urls" $current_input_word). You do this until you get only 1 exact match from compgen. With this mechanism, you'll also have to handle moving the cursor back to the beginning of the line (use tputs).

Last edited by Mr. C.; 09-08-2008 at 10:48 PM.
 
Old 09-09-2008, 05:20 AM   #3
mmn357157
LQ Newbie
 
Registered: May 2007
Location: Bangalore
Distribution: RHEL
Posts: 20

Original Poster
Rep: Reputation: 0
hi,

Following code is working for me exactly like in a command line. It seems no partial completion with below one, but can check always. Thanks for the help.

Quote:
#get arguments from tomcat configuration file
TOMCAT_CONFIG="/root/wsbuild/server/tomcat/conf/server.xml"
args="$(cat $TOMCAT_CONFIG | sed -n 's/^[ ]*<Host[ ]*name[ ]*=[ ]*"[ ]*\([^"]*\)[ ]*".*$/\1/gp')"

set -o emacs
bind 'TAB:dynamic-complete-history'

for vhost in $args
do
history -s $vhost
done

#remove any old history other than site names
HISTSIZE=$(echo $args | wc -w)

read -e -p "Enter sitename: " site
Regards,

MMN
 
Old 09-10-2008, 12:59 PM   #4
otho
LQ Newbie
 
Registered: Oct 2003
Posts: 8

Rep: Reputation: 0
you could also use rlwrap which will intermediate terminal input to another program using readline. That way you can make your shell without and extra code, and just call

rlwrap myshell

All of the basics are made up, including tab completion, where the list for tab completion would be a specified file or ~/.myshell_completion
 
Old 09-10-2008, 01:49 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Or rlfe (from the readline source, in the readline/examples directory), which is available on many distributions.

I reported the bug above to the bash maintainers. We'll see what results.
 
Old 09-11-2008, 11:29 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Ok, it appears I was in error thinking the history completion was not working with read -e. The problem is described in the bash FAQ, item E13.

The : (colon) character is special to readline, and must be removed from the bash/readline's COMP_WORDBREAK variable to function with the URLs required in the OPs problem.

So, the new solution is:

Code:
#!/bin/bash

set -o emacs
bind 'set show-all-if-ambiguous on'
bind 'set completion-ignore-case on'

COMP_WORDBREAKS=${COMP_WORDBREAKS//:}

#bind TAB:menu-complete
bind 'TAB:dynamic-complete-history'

urls="http://example.com http://sample.net http://example.us http://example.net"
for i in $urls ; do
    history -s $i
done

echo $urls
compgen -W "$urls" http://sa
read -e -p "What? " arg1
This now works as expected.
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
suse 9.2 + bash autocompletion alaios SUSE / openSUSE 9 05-13-2005 12:34 PM
Encrypting Passwords in a commandline Script vendolis Linux - Newbie 1 08-29-2003 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration