Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need an interactive bash script template to copy files to predefined destination folders. If possible, when prompted for user input tab completion would be enabled for a predefined source directory. The basic logic would go something like this:

script example 1:

/bin/bash    
enable tab completion to load from /src/dir #perhaps using pwd as script is loaded    
echo "filename or pattern?" #interactively ask user to provide filename or pattern    
'user input'=$FILENAME #define interactive user input as filename    
rsync -rv /src/dir/$FILENAME /dest/dir/ && rm -r /src/dir/$FILENAME    
done

I know it's rough/incorrect but hopefully the desired outcome is clear enough. Thanks in advance for any guidance!! Links to specific bash scripting techniques would be much appreciated.

I tried this (thanks for the help Noam N. Kremen and slhk!!!):

#!/bin/bash  
read -e -p "Filename or pattern? " SRC_FILE  
rsync "$SRC_FILE" /dest/dir && rm -r "$SRC_FILE"

So, tab completion works great, and after "quoting" the strings

"$SRC_FILE"

rsync stopped throwing errors. Looks like I now have a great little script doing EXACTLY what I need it to!! And I learned how to format code correctly on here, Thanks!!

share|improve this question
    
Please indent code by 4 spaces. What file are you copying? Does it contain a space in its name or path? Try quoting rsync "$SRC_FILE", and see mywiki.wooledge.org/Quotes –  slhck May 1 '13 at 11:36
    
Thank you very much sir, problem fixed, and looking forward to perusing your site for more tips. –  njtuneguy May 1 '13 at 12:03
    
No problem. I think at this point you should accept Noam's answer below, by clicking the green checkmark next to it, as he helped you figure out the solution. And welcome to Super User, of course! –  slhck May 1 '13 at 12:05

1 Answer 1

up vote 0 down vote accepted

try read -e -p as in:

read -e -p "Enter destination filename" DEST_FILE

-e enables autocompletion and $DEST_FILE will hold the data you need.

share|improve this answer
    
Fantastic and quick help, thank you my friend! –  njtuneguy May 1 '13 at 12:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.