[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: possible compgen bug
From: |
Chet Ramey |
Subject: |
Re: possible compgen bug |
Date: |
Sat, 10 May 2008 22:13:59 -0400 |
User-agent: |
Thunderbird 2.0.0.14 (Macintosh/20080421) |
Nicholas Mc Guire wrote:
probably errornous behavior:
----------------------------
when using compgen to allow shortcuts (in the below example for quit and
exit I get a strange behavior when no input is entered and write times
out after 5 seconds:
#! /bin/bash
read -p "timeout 5 > " -t 5 FOO
CMD=( $(compgen -W "quit exit" -- $FOO) )
if [ "$CMD"x == exitx ] ; then
echo exiting
$CMD
elif [ "$CMD"x == quitx ] ; then
echo quit not a valid command calling exit with -1
exit -1
else
echo invalid command - exiting with -2
exit -2
fi
hofrat@backup:~/shell$ ./2.sh
timeout 5 > quit not a valid command calling exit with -1
hofrat@backup:~/shell$
it seems to expand CMD to quit - not the behavior I expected
I assume that this is a bug in compgen as it indentifies an empty string
with the first word in the options passed - I would have expected it to be
a null-string (just as if one would type x, which has no match, thus the
null-string is assigned to CMD)
When it is not supplied any word to complete, compgen displays all
possible completions -- in this case, the words supplied to `-W'. This
is what happens when you perform filename completion after no characters
have been typed, for instance. You can see this by running the compgen
command:
$ compgen -W "quit exit" -- $FOO
quit
exit
Since the assignment to CMD makes it an array, it as if you assigned
CMD=(quit exit).
You use $CMD in the rest of the script, which is the same as ${CMD[0]},
or `quit'.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
Live Strong. No day but today.
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/