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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Why making our custom completion when linux already does it well, right ? I can't believe how simple this is, thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.