Sunday, December 22, 2013

"Rotating" files in Unix

I like to have a set of images on my desktop, and have used Geektool but because it's not open source and therefore I can't examine what appear to be bugs, I've switched to Nerdtool I have a few directories that have multiple files that I want displayed in turn, but neither tool does this well. So I wrote the following bash script that takes a list of directories to "rotate" a ".current" link between. The files themselves aren't changed at all, and I set Nerdtool to update the display of that one file every ten minutes.
#! /bin/bash

# create a symbolic link named ".current" to the "next" file in a directory.
# keep track of the current file by use of a ".count" file.

for i in $*
    list=`ls -1 $i`
    total=`echo $list | wc -w`

    if [[ "$total" -eq "0" ]]; then continue; fi

    count=`cat $i/.count`

    # if this is the first time
    if [[ "$count" = "" ]]; then count=1; fi

    # if we'd wrap around, start again
    if [[ $count -gt $total ]]; then count=1; fi

    # make the count zero based to index the array

    # bump and save count
    echo $count > $i/.count

    # turn words into array

    rm -f $i/.current
    ln -s $name $i/.current

No comments: