Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Unlike sh, bash has arithmetic evaluation that would make your script much simpler. I also wouldn’t check for the dotfile’s existence, just read it with error messages suppressed. It’s also bizarre that you’re using date +%s to get the timestamp in one place and perl -e 'print time' in another – and the backticks around that second place are even more so. All in all here’s how I’d write that:

    #!/usr/local/bin/bash

    [ -r ~/.bashrc ] && source ~/.bashrc

    timestamp=.login_time

    cd /home/cpoe/work/main/

    last_time=$( cat "$timestamp" 2>&- )
    curr_time=$( date +%s )

    if (( curr_time - last_time > 600 ))  # five (???) minutes
    then
        echo "$curr_time" > "$timestamp"
        /usr/bin/cvs up -d
        /usr/local/bin/runtests -lr
    fi

    But actually, “run the tests if it’s been 5 minutes since my last login” strikes me as a hack. I think what you rather want to say is “skip the tests if cvs up didn’t find any updates to pull.” That would look something like this:

    #!/usr/local/bin/bash

    [ -r ~/.bashrc ] && source ~/.bashrc

    timestamp=.login_time

    cd /home/cpoe/work/main/

    tmp=$( mktemp ) && {
        /usr/bin/cvs up -d | tee "$tmp"
        [ -s "$tmp" ] && /usr/local/bin/runtests -lr
        rm -f "$tmp"
    }

    This is based on the fact that cvs prints its U foo/bar.c lines to stdin, but its chatter to stderr. So it copies cvs’s stdout to a temporary file whose size will be nonzero only if cvs actually did any updates. The tests run only if that’s the case.

    • By the way, I did implement many of your suggestions, thank you. However, I deliberately didn't want to go with the 'don't run tests if there are no updates to CVS'. There are plenty of other ways -- admittedly less common -- that tests can fail even without CVS updates. When those failures occur, I'd like to catch them!

      • Ah. Btw, a much more direct way to do this just occured to me: use the file’s mtime.

        #!/usr/local/bin/bash

        [ -r ~/.bashrc ] && source ~/.bashrc

        timestamp=.login_time

        cd /home/cpoe/work/main/

        if [[ `find "$timestamp" -mmin +5 -print` ]] ; then
            touch "$timestamp"
            /usr/bin/cvs up -d
            /usr/local/bin/runtests -lr
        fi

        The idiomatic bit here is using find to check the file’s age. The -mmin +5 predicate means “if the mtime of the file under considerat