Unfortunately no one can be told what fun_plug is - you have to see it for yourself.
You are not logged in.
Need some help identifying why my script isn't outputting what it should be. Obviously I've coded it incorrect, but can't figure out what I've done wrong.
Basic purpose: Have an IP camera that sits on the network and is configured to record 30sec clips in video files as it detects motion and saves to /mnt/HD_a2/ipcamera. Now, I have a script that runs every night which basically does a few things:
1) Tests if there are any recordings present in the ipcamera folder
2) If so, creates a folder for the current date
3) Moves the recordings to the newly created folder
4) Does some chmod actions so I don't have access issues on other computers
5) Then COUNTS how many folders (days) of recordings there are
6) Compares this count to the maximum days variable I've set (7, one week)
7) Deletes any excess days to keep the backlog of days at 7
My issue is on step 5. It seems that when I do the following:
numberOfDays=$(( $(${lsCmd} \-1d \/mnt\/HD_a2\/ipcamera\/\*\/ | ${wcCmd} -l) ))
${echoCmd} "${numberOfDays} days found." >> ${dstLog}
if test ${numberOfDays} -gt ${maxNumberOfDays}; then
${echoCmd} ${numberOfDays} is greater than ${maxNumberOfDays}. >> ${dstLog}
excessDaysCount=$(( ${numberOfDays} - ${maxNumberOfDays} ))
daysToRemove=$(( ${lsCmd} -1d \*\/ | ${headCmd} -n ${excessDaysCount} ))
${echoCmd} [${logDate}] Removing old backups: >> ${dstLog}
${echoCmd} "${daysToRemove}" >> ${dstLog}
${rmCmd} -r ${daysToRemove} >> ${dstLog} 2>&1
else
${echoCmd} "Found ${numberOfDays} where ${maxNumberOfDays} allowed. None removed." >> ${dstLog}
0 is returned. When actually there are about 9 folders. When I run the command at the prompt (ls -1d /mnt/HD_a2/ipcamera/*/ | wc -l) I get 9. But for some reason, the way it is written above returns 0 when the script is run.
Here is the full script. Not a pro at scripting, so I'm sure there are ways to tidy this up. But it gets the job done....except for this one issue.
#!/ffp/bin/sh ########### # Settings ########### cd /mnt/HD_a2/ipcamera dateCmd="/ffp/bin/date" mkdirCmd="/ffp/bin/mkdir" chmodCmd="/ffp/bin/chmod" echoCmd="/ffp/bin/echo" headCmd="/ffp/bin/head" wcCmd="/ffp/bin/wc" mvCmd="/ffp/bin/mv" findCmd="/ffp/bin/find" lsCmd="/ffp/bin/ls" 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") maxNumberOfDays=7 if [ ! -d "${dstDir}" ]; then numOfRec=$(${findCmd} . -type f -name "${theRecDate}*" | ${wcCmd} -l) ${echoCmd} "${numOfRec} recordings found." >> ${dstLog} 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} cd /mnt/HD_a2/ipcamera ${echoCmd} [${logDate}] Moving recording\(s\) into directory \"${dstDir}\" >> ${dstLog} ${findCmd} . -type f -name "${theRecDate}*" -exec ${mvCmd} {} /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 if [ -d "${dstDir}" ]; then ${echoCmd} ${dstDir} exists. >> ${dstLog} cd /mnt/HD_a2/ipcamera ${echoCmd} cd suceeded. >> ${dstLog} numberOfDays=$(( $(${lsCmd} \-1d \/mnt\/HD_a2\/ipcamera\/\*\/ | ${wcCmd} -l) )) ${echoCmd} "${numberOfDays} days found." >> ${dstLog} if test ${numberOfDays} -gt ${maxNumberOfDays}; then ${echoCmd} ${numberOfDays} is greater than ${maxNumberOfDays}. >> ${dstLog} excessDaysCount=$(( ${numberOfDays} - ${maxNumberOfDays} )) daysToRemove=$(( ${lsCmd} -1d \*\/ | ${headCmd} -n ${excessDaysCount} )) ${echoCmd} [${logDate}] Removing old backups: >> ${dstLog} ${echoCmd} "${daysToRemove}" >> ${dstLog} ${rmCmd} -r ${daysToRemove} >> ${dstLog} 2>&1 else ${echoCmd} "Found ${numberOfDays} where ${maxNumberOfDays} allowed. None removed." >> ${dstLog} fi else ${echoCmd} "No current day folder found. Not deleting any backups." >> ${dstLog} fi exit 0
Offline
The script style looks familiar
What's going in:
numberOfDays=$(( $(${lsCmd} \-1d \/mnt\/HD_a2\/ipcamera\/\*\/ | ${wcCmd} -l) ))
is that the shell replaces "\/mnt\/HD_a2\/ipcamera\/\*\/" with the list matching entries in the directory and then ls works on each one.
Just a few thoughts: It seems to me that all of the '\' are redundant. Are there spaces in the folder names? that could cause problems. I'd take a closer look at how you compute daysToRemove as well.
If you're trying to fix this, try adding a line that does the ls in the script and see what's the output when the script is run and produces the wrong result:
${lsCmd} \-1d \/mnt\/HD_a2\/ipcamera\/\*\/ > /tmp/list_of_folders.txt 2>&1
(this will also output all error messags to the file)
If you'd like to try a different approach, try something like this:
find /mnt/sda2 -type d -maxdepth 1 -mindepth 1 | wc -l
Last edited by scaramanga (2013-01-10 14:10:37)
Offline
Of course this isn't MY script, if I could code this I could figure out my problem
Here is my error in the txt file:
ls: /mnt/HD_a2/ipcamera/*/: No such file or directory
but it DOES exist. After running the script, I go straight to the shell and type:
ls -1d /mnt/HD_a2/ipcamera/*/ | wc -l
and I get 16 (because there are 16 folders). But I can't figure out why it doesn't output 16 in the script.
Offline
bound4h wrote:
Of course this isn't MY script, if I could code this I could figure out my problem
Here is my error in the txt file:ls: /mnt/HD_a2/ipcamera/*/: No such file or directory
but it DOES exist. After running the script, I go straight to the shell and type:
ls -1d /mnt/HD_a2/ipcamera/*/ | wc -l
and I get 16 (because there are 16 folders). But I can't figure out why it doesn't output 16 in the script.
Now we're homing-in on the problem.
lsCmd is /ffp/bin/ls
so don't run ls (which uses the PATH variable to pick which ls it should run), instead run:
/ffp/bin/ls -1d /mnt/HD_a2/ipcamera/*/ | wc -l
if you'd like to know exacly which ls is executed when you call ls, run the following command:
which ls
Offline
ls *
is not the same as
ls \*
ls * = list all
ls \* = list things which contains a * character
ls: /mnt/HD_a2/ipcamera/*/: No such file or directory
I think ls tells you dont have any folder exactly named as this, that is with a * char.
Last edited by bjby (2013-01-12 08:43:44)
Offline