Expanding on @Cyrus's comment about the utility of bash's select
directive ....
If it weren't for your stipulation of being able to select by number or name, bash's select
might be all you need:
$ select name in *; do printf "You chose %s\n" "$name"; done
1) file AA 6) file_E 11) file_J 16) file_O 21) file_T 26) file_Y
2) file_A 7) file_F 12) file_K 17) file_P 22) file_U 27) file_Z
3) file_B 8) file_G 13) file_L 18) file_Q 23) file_V 28) foo.sh
4) file_C 9) file_H 14) file_M 19) file_R 24) file_W
5) file_D 10) file_I 15) file_N 20) file_S 25) file_X
#? 1
You chose file AA
#? 4
You chose file_C
#? 28
You chose foo.sh
#? ^D
While the interface might be slightly Spartan for some needs, when you need something quick and dirty, but yet shell-grade reliable, select
is a useful tool to have in your bag.
The man
page for bash cites some aspects of select
's behavior that we can utilize:
The PS3 prompt is then displayed and a line read from the
standard input. If the line consists of a number corresponding
to one of the displayed words, then the value of name is set to
that word. If the line is empty, the words and prompt are
displayed again. If EOF is read, the command completes. Any
other value read causes name to be set to null. The line read
is saved in the variable REPLY.
So by putting some supporting code around the call to select
, this is possible:
unset name
_PS3="$PS3"
PS3="Which file do you want to use? "
while [ -z "$name" ]
do
select name in *; do break; done
if [ -z "$name" ]
then
if [ -f "$REPLY" ]
then
name="$REPLY"
printf "You chose by name: '%s'\n" "$name"
else
printf "There is no file by that name.\n"
fi
else
printf "You chose by number: '%s'\n" "$name"
fi
done
PS3="$_PS3"; unset _PS3
Output:
$ ./foo.sh
1) file AA 6) file_E 11) file_J 16) file_O 21) file_T 26) file_Y
2) file_A 7) file_F 12) file_K 17) file_P 22) file_U 27) file_Z
3) file_B 8) file_G 13) file_L 18) file_Q 23) file_V 28) foo.sh
4) file_C 9) file_H 14) file_M 19) file_R 24) file_W
5) file_D 10) file_I 15) file_N 20) file_S 25) file_X
Which file do you want to use? 12
You chose by number: 'file_K'
$ ./foo.sh
1) file AA 6) file_E 11) file_J 16) file_O 21) file_T 26) file_Y
2) file_A 7) file_F 12) file_K 17) file_P 22) file_U 27) file_Z
3) file_B 8) file_G 13) file_L 18) file_Q 23) file_V 28) foo.sh
4) file_C 9) file_H 14) file_M 19) file_R 24) file_W
5) file_D 10) file_I 15) file_N 20) file_S 25) file_X
Which file do you want to use? file W
There is no file by that name.
1) file AA 6) file_E 11) file_J 16) file_O 21) file_T 26) file_Y
2) file_A 7) file_F 12) file_K 17) file_P 22) file_U 27) file_Z
3) file_B 8) file_G 13) file_L 18) file_Q 23) file_V 28) foo.sh
4) file_C 9) file_H 14) file_M 19) file_R 24) file_W
5) file_D 10) file_I 15) file_N 20) file_S 25) file_X
Which file do you want to use? 29
There is no file by that name.
1) file AA 6) file_E 11) file_J 16) file_O 21) file_T 26) file_Y
2) file_A 7) file_F 12) file_K 17) file_P 22) file_U 27) file_Z
3) file_B 8) file_G 13) file_L 18) file_Q 23) file_V 28) foo.sh
4) file_C 9) file_H 14) file_M 19) file_R 24) file_W
5) file_D 10) file_I 15) file_N 20) file_S 25) file_X
Which file do you want to use? file AA
You chose by name: 'file AA'
1
select x in *; do echo "$x"; break; done
? – Cyrus – 2019-11-04T22:44:51.810What if the third file in the list of 20 files is a file named
14
and the user enters14
? Should that select the file named14
or the 14th entry in the list? – Jim L. – 2019-11-05T00:11:25.373