Sponsored Content
Top Forums UNIX for Advanced & Expert Users Bash-Completion, one list shown, the other not. Post 302940983 by sea on Friday 10th of April 2015 02:02:32 PM
Old 04-10-2015
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 which is hardcoded into the bash_completion file, but not with variables from outside, though a simple echo at the wrong place, shows the value is read... Smilie

Code:
list1="a123 a456 a789"
list2="abc def ghi"

Then, for the actual bash completion, i'd expect this:
Code:
XY -c a${list[1} 
XY -c b${list[2}

But what i get is this:
Code:
+ files $ ll [fr]* ; set -x ; . repro* ;  cat file.list ; set +x
-rw-rw-r--. 1 sea sea   61 10. Apr 19:14 file.list
-rw-rw-r--. 1 sea sea 1.7K 10. Apr 19:49 reproducable.bash
+ . reproducable.bash
++ complete -F _XY_module XY
++ complete -F _XY_module XY.sh
+ cat file.list
section_task3="babc bdef bghi bjkg blmn bopq brst buvw bxyz"
+ set +x

:) files $ ll [fr]* ; set -x ; . repro*XY -c 
a  b  

:) files $ XY -c a
a1024  a128   a192   a256   a384   a512   a768   a96 
   
:) files $ XY -c b 
a  b  

:) files $ XY -c b b 
a  b  

:) files $ XY -c b b b 
a  b  

:) files $ XY -c b b b a
a1024  a128   a192   a256   a384   a512   a768   a96

:) files $ XY -c b b b a1
a1024  a128   a192

This is a short version of the full bash_completition script, and beeing reproducable:
Code:
_XY_module()
{
#
#	Variables
#
	local cur prev OPTS
	COMPREPLY=()
	OPTS="-a -b"
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	
	[ -f file.list ] || printf "%s\n" "section_task2=\"abc def ghi jkg lmn opq rst uvw xyz\"" > file.list
	section_task1="a96 a128 a192 a256 a384 a512 a768 a1024"
	section_task2="b$(grep section_task2 file.list | sed s,section_task2=,, | sed s,\ ,\nb,)"
#
#	Action
#
	# This shows a list of words applying to your last argument
	# These need manual maintainance
	case "$prev" in
	-c)
		case $cur in
		a*)		COMPREPLY=( $( compgen -W "$(echo $section_task1|grep $cur*)" -- "$cur" ) ) 
				return 0
				;;
		b*)		COMPREPLY=( $( compgen -W "$(echo $section_task2|grep $cur*)" -- "$cur" ) ) 
				return 0
				;;
		"")		COMPREPLY=( $( compgen -W "a b" -- $cur) )
				return 0
				;;
		esac
		;;
	XY|XY.sh)
		COMPREPLY=( $(compgen -f -- $cur) )
		return 0
		;;
	esac
	# This completes the word you are currently writing
	# These need manual maintainance
	case $cur in
	-*)	COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
		return 0
		;;
	a*)	COMPREPLY=( $( compgen -W "$(echo $section_task1|grep $cur*)" -- "$cur" ) ) 
		return 0
		;;
	b*)	COMPREPLY=( $( compgen -W "$(echo $section_task2|grep $cur*)" -- "$cur" ) ) 
		return 0
		;;
	*)	COMPREPLY=( $( compgen -W "a b" -- $cur) )
		return 0
		;;
	esac
	
}
# Actualy make it available to the shell
# Cover with and without extension
complete -F _XY_module XY
complete -F _XY_module XY.sh

Any advice or hints please?
Thank you in advance and have a nice weekend Smilie

Last edited by sea; 04-10-2015 at 03:09 PM..
 

9 More Discussions You Might Find Interesting

1. 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

2. Ubuntu

set completion-ignore-case on doesn't work in bash

completion-ignore-case option doesn't work in my version: /home/user $ echo $BASH_VERSION 3.2.48(1)-release /home/user $ ls -l * -rw-r--r-- 1 user user 0 2009-10-18 00:09 somefile -rw-r--r-- 1 user user 0 2009-10-18 00:09 Somefile /home/user $ set completion-ignore-case on But when I... (2 Replies)
Discussion started by: Sapfeer
2 Replies

3. 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

4. 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

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

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

8. 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

9. 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
All times are GMT -4. The time now is 09:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy