Question

I am working on a script that reads the directories in a path and prompts the user for witch directory to choose. The script so far looks like this:

select choises in ${list_files[@]}; do
  if CONTAINS_ELEMENT $choises "${list_files[@]}"; then
    break
  else
    echo "invalid choise (PS use the ID numbers)! Try again:"
  fi
done

the "CONTAINS_ELEMENT" thingy is a function that checks if the variable $choises is a member of the list_files array. This script works fine, and the output is something like this:

1) first_folder 
2) second_folder
3) yet_another_folder

And the user can choose witch folder by typing in the corresponding ID value (ie "2" for "second_folder").

What I am struggling with is this: Is there a way to check if the supplied answer is a correct number -or- a correct file name? And if you can write in the file name manually, is there a way to implement name completion in this script?

The actual reply to select is held in the variable $REPLY, I need to somehow check both $choises and $REPLY. The completion mechanism is much more tricky I suspect..

Does anyone have a good suggestion here? It would be very cool if you could help me get in these features!!

Was it helpful?

Solution

select choises in ${list_files[@]}; do
  if CONTAINS_ELEMENT $choises "${list_files[@]}" || CONTAINS_ELEMENT $REPLY "${list_files[@]}"; then 
    break
  else
  echo "invalid choise (PS use the ID numbers)! Try again:"
  fi
done

This makes both the ID number and the full directory name work, the code is a bit ugly though, and there is no name completion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top