I wrote the following bash script. The goal of the script is to read in a file with a list of file names, then copy those files from one directory to another directory. The list of file names has 197 file names, the source directory has over 3000 plus files.

This worked for me, but I am looking for suggestions on how I can improve this script.

I am new to bash scripting and I'm not a programmer. I was thrilled I got this to work, and I'll be using it on and off to copy files for various different groups. Doing this got me interested in learning more about bash.

Thanks!

pan64

10-02-2012 06:21 AM

this looks like a one-liner in awk or perl or something like this in bash:

Code:

for image in `cat ./imagefilenames_g1.csv`
do
cp "$sourcedir/$image" $targetdir;
done

how do you use the loop variable line in your original script?

jamesvjj

10-02-2012 07:45 AM

I was looping through each line of the file and using the sed command. Too complicated, your solution is much better. Thanks!

pan64

10-02-2012 07:51 AM

while read line will read config file line by line. You could use it in cp, but you did not do that:

1) Please use ***[code][/code] tags*** around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

2)

Code:

sourcedir=~/Desktop/images

"~" is an interactive alias. In a script it's usually better to use the built-in "$HOME" variable.

3)

Code:

exec<$file

a. QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.