tags:

views:

128

answers:

3

I am writing the following script to copy *.nzb files to a folder to queue them for Download.

I wrote the following script

#!/bin/bash

#This script copies NZB files from Downloads folder to HellaNZB queue folder.

${DOWN}="/home/myusuf3/Downloads/"
${QUEUE}="/home/myusuf3/.hellanzb/nzb/daemon.queue/"


for a in $(find ${DOWN}  -name  *.nzb)
do
cp ${a} ${QUEUE}
rm *.nzb
done

it gives me the following error saying:

HellaNZB.sh: line 5: =/home/myusuf3/Downloads/: No such file or directory
HellaNZB.sh: line 6: =/home/myusuf3/.hellanzb/nzb/daemon.queue/: No such file or directory

Thing is that those directories exsist, I do have right to access them.

Any help would be nice.

Please and thank you.

+4  A: 

Variable names on the left side of an assignement should be bare.

foo="something"
echo "$foo"

Here are some more improvements to your script:

#!/bin/bash

#This script copies NZB files from Downloads folder to HellaNZB queue folder.

down="/home/myusuf3/Downloads/"
queue="/home/myusuf3/.hellanzb/nzb/daemon.queue/"

find "${down}" -name "*.nzb" | while read -r file
do
    mv "${file}" "${queue}"
done

Using while instead of for and quoting variables that contain filenames protects against filenames that contain spaces from being interpreted as more than one filename. Removing the rm keeps it from repeatedly producing errors and failing to copy any but the first file. The file glob for -name needs to be quoted. Habitually using lowercase variable names reduces the chances of name collisions with shell variables.

If all your files are in one directory (and not in multiple subdirectories) your whole script could be reduced to the following, by the way:

mv /home/myusuf3/Downloads/*.nzb /home/myusuf3/.hellanzb/nzb/daemon.queue/

If you do have files in multiple subdirectories:

find /home/myusuf3/Downloads/ -name "*.nzb" -exec mv {} /home/myusuf3/.hellanzb/nzb/daemon.queue/ +

As you can see, there's no need for a loop.

Dennis Williamson
haha that was dumb of me thanks! I couldn't figure what the problem was for the life of me.
garbagecollector
@garbagecollector: see my edit for additional hints.
Dennis Williamson
thanks for your help! :)
garbagecollector
A: 

The correct syntax is:

DOWN="/home/myusuf3/Downloads/"
QUEUE="/home/myusuf3/.hellanzb/nzb/daemon.queue/"
David Harris
A: 
for a in $(find ${DOWN}  -name  *.nzb)
   # escape the * or it will be expanded in the current directory
   # let's just hope no file has blanks in its name
do
  cp ${a} ${QUEUE}  # ok, although I'd normally add a -p
  rm *.nzb          # again, this is expanded in the current directory
                    # when you fix that, it will remove ${a}s before they are copied

done

Why don't you just use rm $(a}?

Why use a combination of cp and rm anyway, instead of mv?

Do you realize all files will end up in the same directory, and files with the same name from different directories will overwrite each other?

What if the cp fails? You'll lose your file.

reinierpost
all very true. thanks for the feedback!
garbagecollector