List files with number to select based on number

Hi experts,

I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number.
I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for further scripting.

ls -1 | cat -n

Did you consider ksh 's select command:

?

PS3="Enter file number: "
echo "File list:"
select file in *
do
  [[ -a "$file" ]] && break
  REPLY=""
  echo "Invalid number."
  echo ""
  echo "File list:"
done
echo "file selected: $file"

Thanks for the input. Somehow it shows me error "Select: not found".

add to top of script:

#!/bin/ksh
1 Like

This worked for me. Thanks! :slight_smile: