Manipulating input into the shell "read" command

Hi All

I have a migration program that creates directories based on dates, e.g 20090714 20090812 etc.. Based on their requirements, the user will select the directory they want to perform an action on.

Currently, this is a snippet of the code I use

no_of_versions=`ls | wc -l`
        if [ $no_of_versions -gt 1 ]; then
                export dirs=`ls -d [2]*`
                echo ""
                echo "Select Version:   "
                echo ""$dirs""
                read version
       fi

Currently the user has to type in the value of the date and this isn't the most elegant of ways. Consequently, I wanted to simplify this process and this is where I need help.

Is it possible to have a situation where I display the list of date directories along the lines of

(1) 20090714 (2) 20090812 (3) 20090901

They can then select either 1 or 20090714

I have no way of knowing how many dates there could be, 1 or 10 is possible.

I'm not even entirely sure this is something I can do in shell or even perl. All help as always appreciated

You could use the select statement, available in ksh and bash, e.g.:

no_of_versions=$(ls -d [2]* | wc -l)
if [[ $no_of_versions -gt 1 ]]; then
  PS3="Select Version:   "
  select version in $(ls -d [2]*); do
  if [[ -f $version ]]; then
    break
  else
    echo please select one of the options above
  fi
  done
fi
echo $version

Fantastic, Works a treat.. Much appreciated..