4

I'm trying to create a restore function for my trash folder. When you run the script, it asks you for the contents of the folder and asks you which one to restore, like this :

yedenn@hal:~/Cours$ res
-rw-r--r-- exemple
-rw-r--r-- test1
-rw-r--r-- test2
-rw-r--r-- test3   

Which to restore? _

I'm wondering if there is a way to complete the file names by pressing <TAB> (for example) ?
The solutions I've seen so far seem to work only when typing on the command line, not while the script is running.

Yedenn
  • 43
  • 4

1 Answers1

3

You can read the input this way, -e enables readline that has the completion ready for you (man readline) but you have to add some modification for the filenames completion to work for your trash directory, and after reading the file, change it back to the current directory, possibly like this:

#!/bin/bash

trash="path/to/trash/directory"

cd "$trash"
read -e -p "Which to restore? " f
cd -

printf "\nInput: %s" "$f"
[ -e "${trash}/$f" ] && printf "\nFile: %s" "${trash}/$f"
printf "\nCurrent directory: %s\n" "$PWD"

Note that later in your script you have to refer the file as above, as f input is relative to trash path.

thanasisp
  • 5,855
  • 3
  • 14
  • 31
  • 1
    Why making our custom completion when linux already does it well, right ? I can't believe how simple this is, thanks ! – Yedenn Oct 15 '20 at 20:28