PDA

View Full Version : UNIX Shell script... help!




Flying Llama
Oct 3, 2009, 02:14 AM
Hi. I'm keeping a photojournal of my computer and created a shell script that runs every time I log in which takes a screenshot every 5 minutes. I'm on a Mac.

Right now, the script writes over its own screenshots when I log in. For example, I'll end up with "Capture1", "Capture2", etc. However when I log back in, it starts over at "Capture1" and overwrites as it goes.

I have added a line that says:

mkdir folder/$(date +%Y%m%d.%H%M%S)

which creates a timestamped folder every time I log in.

How can I modify my script so that the pictures will be placed into that new different folder every time I log in?

Is there a better way to do this?

Thanks



electroshock
Oct 3, 2009, 02:20 AM
Can you post your shell script? Otherwise, going to be kinda hard to help you out. (Not enough details of how you're doing the initial capture in the script, filename-wise.)

Flying Llama
Oct 3, 2009, 02:26 AM
Ok here's the code



#/bin/sh

mkdir ~/journal/$(date +%Y%m%d.%H%M%S)

keeparound=1000
counter=1
screendir="~/journal/"
delay=300 # in seconds. 300 = five minutes


if [ ! -d $screendir ] ; then
mkdir $screendir
chmod a+rwx $screendir
fi

while [ /bin/true ]
do
screencapture -Smx $screendir/capture-$current.png

current=$(( $current + 1 ))

if [ $current -gt $keeparound ] ; then
current=1
fi

sleep $delay

done

exit 0


Basically, I want to create a new folder on login, the way I already did, but then assign/"update" screendir to that new, different folder every time I log back in. I'm guessing that setting the $(date +%Y%m%d.%H%M%S) folder to screendir would create a new folder for every shot? so that's no good.

electroshock
Oct 3, 2009, 02:33 AM
Could do it as a slight tweak:



#/bin/sh

screendir="~/journal/$(date +%Y%m%d.%H%M%S)"
mkdir -p ${screendir}

keeparound=1000
counter=1
delay=300 # in seconds. 300 = five minutes

while [ /bin/true ]
do
screencapture -Smx $screendir/capture-$current.png

current=$(( $current + 1 ))

if [ $current -gt $keeparound ] ; then
current=1
fi

sleep $delay

done

exit 0

The trick here is, you DEFINE screendir only ONCE, upon start of the script. So even if the day wraps around to the next day... or days afterwards, all captures will still go to the same directory. And it'll be differentiated by each login's date/time as desired.

Flying Llama
Oct 3, 2009, 02:41 AM
That makes sense, and it works perfectly! Thank you!