How to list all files of a directory along with an index to allow users to choose file by inputting the index number?

1

I have multiple files in a directory and I am working on a bash script to list down all the files along with an index next to them so that the users can choose which file to select on the basis of the index instead of writing the entire file name.

For eg.

main_dir
 - temp_1
 - temp_2

My attempt:

while [[ 1 ]]; do
        printf "Which file do you want to use? ($(ls -m ${main_dir})) "
        read -r ans
        if [[ -d ${main_dir}/${ans} ]]; then
            break
        else
            echo "[ERROR] Failed to choose. Please choose from ($(ls -m "${main_dir}"))."
        fi
    done

In this case, the output is:

Which file do you want to use? (temp_1, temp_2)

Also, the user has to enter the name of the file to select it.

EXPECTED OUTCOME:

Which file do you want to use? 
1. temp_1 
2. temp_2

And, the user should be able to choose a file by both ways, inputting name or the index.

loadbox

Posted 2019-11-04T22:41:31.997

Reputation: 123

1select x in *; do echo "$x"; break; done? – Cyrus – 2019-11-04T22:44:51.810

What if the third file in the list of 20 files is a file named 14 and the user enters 14? Should that select the file named 14 or the 14th entry in the list? – Jim L. – 2019-11-05T00:11:25.373

Answers

1

loop over * and remember the files in an array:

files=()
i=0
for f in *
do 
  [ -d "$f" ] && continue ## skip directoriries
  files[$i]="$f"
  echo "$i"$'\t'"${files[i]}"
  (( ++i ))
done
if read -p "Which file do you want to use? " ans 
then 
     echo "you choose ${files[ans]}"
     stat "${files[ans]}"
     wc "${files[ans]}"
fi

Jasen

Posted 2019-11-04T22:41:31.997

Reputation: 499

1

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'

Jim L.

Posted 2019-11-04T22:41:31.997

Reputation: 669