DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2012-03-17 06:07:25

bound4h
Member
Registered: 2010-04-12
Posts: 209

Can someone help me with my script?

I'm trying to run the following script.  I have a camera that, on its own, saves .asf files to a directory on the NAS (/mnt/HD_a2/ipcamera).  Well, every night at 11:55 I want to take the daily files and create a folder and move them into the folder. 

My script creates the folder, but for some reason is not moving the files.  What am I doing wrong?

#!/ffp/bin/sh


###########
# Settings
###########

cd /mnt/HD_a2/ipcamera


dateCmd="/bin/date"
mkdirCmd="/bin/mkdir"
chmodCmd="/bin/chmod"
echoCmd="/bin/echo"
wcCmd="/ffp/bin/wc"

dstLog="/mnt/HD_a2/ipcamera/ipcam_cleanup.log"
theDirDate=$(${dateCmd} "+%Y-%m-%d")
dstDir=${theDirDate}
theRecDate=$(`${dateCmd} "+rec_%Y%m%d"`)
logDate=$(${dateCmd} "+%Y_%m_%d-%H:%M:%S")

if [ ! -d "${dstDir}" ];

then

numOfRec=$(find . -name "${theRecDate}*" | ${wcCmd} -l)

if test ${numOfRec} -gt 0;

then

${echoCmd} "++" >> ${dstLog}
${echoCmd} "=================="${theDirDate}"===================" >> ${dstLog}
${echoCmd} [${logDate}] Making new directory \"${dstDir}\">> ${dstLog}
${mkdirCmd} ${dstDir}

${echoCmd} [${logDate}] Changing directory persmissions >> ${dstLog}
${chmodCmd} 777 ${dstDir}

${echoCmd} [${logDate}] Finding recordings that match today\'s date >> ${dstLog}
${echoCmd} [${logDate}] ${numOfRec} recording\(s\) found >> ${dstLog}

${echoCmd} [${logDate}] Moving recording\(s\) into directory \"${dstDir}\" >> ${dstLog}
find . -name "${theRecDate}*" -exec mv {} /mnt/HD_a2/ipcamera/${dstDir} \;
${chmodCmd} 777 -R ${dstDir}

${echoCmd} "==================[end of log]======================" >> ${dstLog}

else

${echoCmd} "++" >> ${dstLog}
${echoCmd} "=================="${theDirDate}"===================" >> ${dstLog}
${echoCmd} [${logDate}] No recordings found, nothing to move. >> ${dstLog}
${echoCmd} "==================[end of log]======================" >> ${dstLog}

fi

else

${echoCmd} [${logDate}] \"${dstDir}\" already exists, nothing will be done. >> ${dstLog}
${echoCmd} "==================[end of log]======================" >> ${dstLog}

fi

exit 0

The recording files are prefixed by the date and time.  An example is: "rec_20120316163357.asf".  I added the ` and ` in the $theRecDate variable and it is moving EVERY file and folder inside /mnt/HD_a2/ipcamera to a NEW folder called 2012-03-16.  But I only want the files that match the date of the day (not folders and not other days files).

Thanks for anyone who is able to help

Last edited by bound4h (2012-03-17 06:12:23)

Offline

 

#2 2012-03-17 11:24:49

scaramanga
Member
Registered: 2010-08-04
Posts: 251

Re: Can someone help me with my script?

First of all, I think that:

Code:

$(`${dateCmd} "+rec_%Y%m%d"`)

should be:

Code:

$(${dateCmd} "+rec_%Y%m%d")

$(...) is the same as `...`, so it's either one or the other.

The script you wrote forces you to run it just before the day ends, which is a bit limiting. Why not use sed to parse the file name and convert it to the date. Try running this code example, that for each file will generate it's "correct" destination directory (it doesn't make any changes to your system):

Code:

for file in $( ls rec_*.asf ) ;
do 
    echo $file | sed 's@rec_\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).*@\1-\2-\3@';
done

sed expressions tend to be a bit cryptic, because of all the escaping ('\') but it's a very useful tool. This should get you up to speed in not time: http://www.grymoire.com/Unix/Sed.html

good luck!

Last edited by scaramanga (2012-03-17 11:36:57)


DNS-323 HW Rev. C1 FW 1.10 fun-plug 0.5
2 x WD10EARS-00Y5B1 in Standard mode (LCC set to 5 min; Aligned to 4K)
Transmission with Transmission Remote GUI

Offline

 

#3 2012-03-18 12:06:46

FunFiler
Member
Registered: 2010-05-23
Posts: 577

Re: Can someone help me with my script?

Assuming everything else is ok, could it be as simple as:

find . -name "${theRecDate}*" -exec /ffp/bin/mv {} /mnt/HD_a2/ipcamera/${dstDir} \;


3 * (DNS-323 with 2 * 2TB) = 12TB Running FW v1.08 & FFP v0.5
Useful Links: Transmission, Transmission Remote, Automatic

Offline

 

#4 2012-03-19 03:34:40

bound4h
Member
Registered: 2010-04-12
Posts: 209

Re: Can someone help me with my script?

Thanks Scara, I am considering re-writing it using your suggestion.

And thanks FunFiler, that did it!  Oddly enough

Offline

 

#5 2012-03-19 13:03:45

FunFiler
Member
Registered: 2010-05-23
Posts: 577

Re: Can someone help me with my script?

Nothing odd about it really since you said everything else worked and this was the only statement missing the full path.


3 * (DNS-323 with 2 * 2TB) = 12TB Running FW v1.08 & FFP v0.5
Useful Links: Transmission, Transmission Remote, Automatic

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB