ash and select

Working on gc-utils, I discovered that bash, pdksh, ksh and zsh support the select statement, but ash doesnot.

The select statement creates a numbered list out of a list of words and let you choose one of them. This is done until a break statement is reached. The statement is a great way to create simple menus.

For example

select action in list show exit
do
echo $action
done

will generate a list in the form

1) list
2) show
3) exit
Select>

My /bin/sh (ash) doesn’t know that command. I didn’t tested it with csh or tcsh, as tcsh and csh doesn’t support functions and therefore I allready dropped the support for those shells completly. For ash, I created a workaround. Maybe it’s not the nicest solutions, but it works. If someone has a better way to solve the problem, just drop me a comment.

The following code generates a list of the sha1 commit ids that differ between two branches in git using git cherry-pick.

selecting ()
{
RESULT=”"
while true
do
i=1
for word in $WORDS
do
echo “${i}) $word”
i=`expr $i + 1`
done
echo -n “Select> ”
read select
if test $i -gt $select
then
break
fi
done
i=1
for word in $WORDS
do
if test $i -eq $select
then
RESULT=$word
break
fi
i=`expr $i + 1`
done
}

WORDS=`git cherry origin | egrep -o -e “[a-f0-9]{40}.*”`; selecting; sha=$RESULT

I used that code for my multi commit tool, which wrapps the gc-commit tool, and makes it a lot more comfortable to commit back into cvs. The gc-multi-commit script is available at the experimental branch.

Leave a Reply