I am frequently copy-pasting files from a Windows share to a local folder. For this, I would like to have a small bash script where I can search for a file name, have a menu based on this search to select from and then finally copy the selected file to my local folder. As searching the local share every time again would cost me a lot of time (and the files don't change frequently), I have a list of all files in filelist.txt
.
Through copy-pasting from the many help sites on the select menu I came up with the following code. However, there is a problem with the grep output and the select menu. Somehow, newline is not recognized by this and as an option #1 I have a list of all filenames and option #2 is quit. That means, the script doesn't split the grep output into different options...
How can I avoid this behaviour? What am I doing wrong? Can I use grep in this way or do I need to use a different function?
Script example_script
:
#!/bin/bash
read -p 'What are you searching for? ' file_search_name
prompt="Please select a file:"
options=$(grep $file_search_name $HOME/Local/Folder/filelist.txt)
PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
exit
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
echo "You picked $opt which is file $REPLY"
break
else
echo "Invalid option. Try another one."
fi
done
cp ~/Windows/Share/"$opt" $HOME/Local/Folder/"$opt"
sample output:
$ example_script
What are you searching for? File
1) File1.txt
File2.txt
File3.txt
...
2) Quit
Please select a file: