Outputting Results to Indexed List

I'm looking for some suggestions on a command line utility I am making. I would like to use 'find' to locate some files and index the results so the user can choose the correct results from the list and push that input into an open command. A simple example below:


/Users/admin/Desktop/misc/Untitled 4.txt
/Users/admin/Desktop/misc/Untitled.txt
/Users/admin/Desktop/output 2.txt
/Users/admin/Desktop/output.txt
/Users/admin/Desktop/testr.txt

My intention is to build an interface that shows the index on each line and the end user can choose the index number, like so:


[0]/Users/admin/Desktop/misc/Untitled 4.txt
[1]/Users/admin/Desktop/misc/Untitled.txt
[2]/Users/admin/Desktop/output 2.txt
[3]/Users/admin/Desktop/output.txt
[4]/Users/admin/Desktop/testr.txt

Any thoughts?
Thanks!

OSX10.9

Maybe you can find something useful in this example:

#!/bin/ksh
IAm=${0##*/}
tf="/tmp/$IAm.$$"
trap 'rm -f "$tf"' EXIT
find . -type f > "$tf"
nl "$tf"
printf 'Enter number of file to process or 0 to exit: '
read -r line
if [ "$line" == 0 ] || [ "$line" == "" ]
then	exit 0
fi
if [ "$line" != "${line#*[^0-9]}" ]
then	printf '%s: Response must be numeric.\n' "$IAm" >&2
	exit 1
fi
file=$(sed -n "${line}{p;q;}" < "$tf")
if [ "$file" = "" ]
then	printf '%s: Response out of range.\n' "$IAm" >&2
	exit 2
fi
printf 'Do whatever you want with file: %s\n' "$file"
1 Like

Would be a perfect candidate for the select (bash/ksh) builtin:

select CHOICE in $(find ... ); do echo $CHOICE; done

, if available on your system...

1 Like