[Bash-completion-devel] mount label|uuid

gibboris at gmail.com gibboris at gmail.com
Mon Apr 13 21:17:10 UTC 2009


Hi,
Modified as suggested, tested (but not "heavily") and it seems to work.
As a note :
- if no completion meet : filedir is used (maybe it shouldn't happens
  because of the -[UL] swith)
- I postulated uuidgen always output lower-case hex
- don't know if solaris &| cygwin are label|uuid-capable

Raph

On Mon, Apr 13, 2009 at 11:04:02PM +0300, Ville Skyttä wrote:
> On Sunday 12 April 2009, gibboris at gmail.com wrote:
> 
> > COMPREPLY=( $( sed -n "/UUID/s/^UUID=\($cur[0-9a-f-]\{,36\}\).*/\1/Ip" 
> /etc/fstab ) )
> 
> I think this would be better put as:
> 
> COMPREPLY=( $( compgen -W '$( sed -ne "s/^UUID=\([^[:space:]]*\).*/\1/p" 
> /etc/fstab )' -- $cur ) )
> 
> "compgen -W ... -- $cur" is safer against unusual characters in $cur, and "I" 
> in sed's s///I only works with GNU sed as far as I know.
> 
> > COMPREPLY=( $( sed -n "/LABEL/s/^LABEL=\($cur[0-9a-z_]*\).*/\1/Ip" 
> /etc/fstab ) )
> 
> Ditto here:
> 
> COMPREPLY=( $( compgen -W '$( sed -ne "s/^LABEL=\([^[:space:]]*\).*/\1/p" 
> /etc/fstab )' -- $cur ) )
> 
> Could you test these changes and submit a new patch if they work for you?
-------------- next part --------------
diff --git a/bash_completion b/bash_completion
index 688adbd..021d820 100644
--- a/bash_completion
+++ b/bash_completion
@@ -855,11 +855,12 @@ complete -F _umount $dirnames umount
 #
 _mount()
 {
-	local cur i sm host
+	local cur i sm host prev
 
 	COMPREPLY=()
 	cur=`_get_cword`
 	[[ "$cur" == \\ ]] && cur="/"
+	prev=${COMP_WORDS[COMP_CWORD-1]}
 
 	for i in {,/usr}/{,s}bin/showmount; do [ -x $i ] && sm=$i && break; done
 
@@ -884,8 +885,14 @@ _mount()
 				 | grep "^$cur" ) )
 	else
 		# probably Linux
-		COMPREPLY=( $( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' \
+		if [ $prev = -L ]; then
+			COMPREPLY=( $( compgen -W '$(sed -n "/LABEL/s/^LABEL=\([^[:space:]]*\).*/\1/p" /etc/fstab)' -- $cur ) )
+		elif [ $prev = -U ]; then
+			COMPREPLY=( $( compgen -W '$(sed -n "/UUID/s/^UUID=\([0-9a-f-]\{36\}\).*/\1/p" /etc/fstab)' -- $cur ) )
+		else
+			COMPREPLY=( $( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' \
 				/etc/fstab | grep "^$cur" ) )
+		fi
 	fi
 
 	return 0


More information about the Bash-completion-devel mailing list