Bash-completion on shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash-completion on shell script
# 1  
Old 11-16-2014
Bash-completion on shell script

Hello
I want use bash_completion on a script but the information on the internet give me not a solution.
The scrip start with "cd" to a direction
Than the script do "ls"
Than I must give a name of a sub-direction and here I want to use <TAB>, so when I have a direction with the name
smith and I give sm and than <TAB> than I want completion to smith.
When I have two names who begin with sm I must see the two possibilities.
When I use <TAB> on the terminal than everything works fine.
Only not from within a script.
Thanks anyway
# 2  
Old 11-16-2014
TAB completion is a feature of the interactive shell. If you are executing a script you are not interactive anymore. If you want tab completion when you are writing a script, that's a feature that your editor must have.

Last edited by Aia; 11-16-2014 at 02:05 PM..
# 3  
Old 11-16-2014
You either want a select menu or read: An Introduction to Programmable Completion

As in:
Code:
select ITEM in $(ls);do
    echo "do your stuff"
    break
done

Hope this helps

---------- Post updated at 22:10 ---------- Previous update was at 19:33 ----------

I wanted to implement this as well for some of my scripts, but me didnt feel confident enough to try.
Now i tried, and YEAH !!! *dance*

This is what i've achieved in about an hour from knowing nothing about it.
Its dirty, its bugy, but when it agrees to work, heck - i love that it lists my prj dir Smilie
Code:
# bash completition for dev-scripts
# 2014.11.16 by sea
#
#	Dynamic attempt
#
	_ds_base_dir()
	{
		# NOT USED YET
		echo /usr/share/dev-scripts/menu
	}
	_ds_list_prjs()
	{
	    COMPREPLY=( $( compgen -W "$(cd $HOME/.config/dev-scripts/prjs;ls)" -- "$2" ) )
	    return 0
	}
#
#	Fixed lists
#
	_ds_mod_git()
	{
		COMPREPLY=( $( compgen -W "add commit edit status upl" -- "$2" ) )
	}
	_ds_mod_ks()
	{
		COMPREPLY=( $( compgen -W "add edit make" -- "$2" ) )
	}
	_ds_mod_rpm()
	{
		COMPREPLY=( $( compgen -W "add changelog edit list make" -- "$2" ) )
	}
	_ds_mod_prj()
	{
		COMPREPLY=( $( compgen -W "git ks rpm edit list" -- "$2" ) )
	}
	_ds_mod_ssh()
	{
		COMPREPLY=( $( compgen -W "info make setup" -- "$2" ) )
	}
	_ds_mod_new()
	{
		COMPREPLY=( $( compgen -W "manpage" -- "$2" ) )
	}
	_ds_mod_menu()
	{
		COMPREPLY=( $( compgen -W "new prj ssh add make review setup" -- "$2" ) )
	}
	_dev_scripts()
	{
		COMPREPLY=()
		case $3 in
		add|make|list|edit|review)
			complete -F _ds_list_prjs -o filenames ds	;;
		setup)	return 0						;;
		new)	complete -F _ds_mod_new -o filenames ds			;;
		prj)	complete -F _ds_mod_prj -o filenames ds			;;
		ks)	complete -F _ds_mod_ks -o filenames ds			;;
		rpm)	complete -F _ds_mod_rpm -o filenames ds			;;
		git)	complete -F _ds_mod_git -o filenames ds			;;
		ssh)	complete -F _ds_mod_ssh -o filenames ds			;;
		esac
	}
	complete -F _dev_scripts -W 'add make review setup new prj ssh' ds

@ thailand: Hope this helps

@ others:
One question remains, anyone knows how i can 'fix' the read/shown values?
I assume i need to implement another 'layer' of case statement within functions?

Thank you in advance
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

2. UNIX for Advanced & Expert Users

Bash-Completion, installed but not applied

Heyas For my project TUI i had prepared bash completion. Bash-completion works, at least if i source that file manualy. However, when i'm installing it, it wont apply, not even for new opened terminals. Allthough i had it working once, i dont get why it doesnt work now. <...> + '' +... (0 Replies)
Discussion started by: sea
0 Replies

3. UNIX for Advanced & Expert Users

Bash-Completion, one list shown, the other not.

Heyas I'm trying to read out a file which contains a variable and use that list to complete a bash command. Difficult part on this is, that 2 (actualy 3) different lists are to be used, but the 'dynamic' ones from the external file dont work properly. It only seems to work with the list... (2 Replies)
Discussion started by: sea
2 Replies

4. Shell Programming and Scripting

Bash-Completion, an example

Hello I've followed several bash-completion threads, usualy none were answered, because of obvious reasons. (of which i'm just aware since a day or a few) Non-the-less, because i was writing scripts, i never considered it to be possible to add this functionality. Also, because i though that... (0 Replies)
Discussion started by: sea
0 Replies

5. UNIX for Dummies Questions & Answers

Bash Tab Completion Hanging

Hi, I'm having a problem with tab completion at the bash command line. For some reason, whenever I type g<tab>, the terminal will freeze up for 5-10 seconds before asking me if I want to display all 325 possibilities. I thought that maybe it's because of the high number of commands, but I have... (4 Replies)
Discussion started by: Raz716
4 Replies

6. Shell Programming and Scripting

Programmable completion for two arguments in bash

Hi there! I have this script which handles bookmarks. Bookmarks are basically a string that point to a certain path, e.g., project -> ~/code/projects/project. Currently I have working completion for the bookmarks: ~ $ m p<Tab> ~ $ m project What I want to implement now is the cd... (0 Replies)
Discussion started by: KevinSjoberg
0 Replies

7. Shell Programming and Scripting

configure bash completion for multiple possibilities

Hello, Bash completion is great, but there are some features I'd like to change. One thing is the behaviour when there are lots of very similar possibilities. E.g., my directory contains 133 files, from pubmed_result1.txt to pubmed_result133.txt $ ls Lyonprim/p Display all 133... (2 Replies)
Discussion started by: jossojjos
2 Replies

8. Shell Programming and Scripting

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... (0 Replies)
Discussion started by: platypuus
0 Replies

9. Shell Programming and Scripting

[tcsh2bash] Tab completion - 'enhanced' equivalent in bash?

I really like tcsh's: set completion='enhanced' because it treats underscores and dash signs the same. I don't have to reach for the shift key when trying to complete files that have underscores. As far as I know, there is nothing like this in bash. Does such a thing exist in bash? If... (2 Replies)
Discussion started by: sarnobat
2 Replies
Login or Register to Ask a Question