UNIX Shell script... help!

Discussion in 'Mac Programming' started by Flying Llama, Oct 3, 2009.

  1. macrumors 6502a

    Flying Llama

    Joined:
    Aug 4, 2004
    Location:
    Los Angeles
    #1
    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
     
  2. macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #2
    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.)
     
  3. thread starter macrumors 6502a

    Flying Llama

    Joined:
    Aug 4, 2004
    Location:
    Los Angeles
    #3
    Ok here's the code

    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.
     
  4. macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #4
    Could do it as a slight tweak:

    Code:
    
    #/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.
     
  5. thread starter macrumors 6502a

    Flying Llama

    Joined:
    Aug 4, 2004
    Location:
    Los Angeles
    #5
    That makes sense, and it works perfectly! Thank you!
     

Share This Page