[ncolug] Re: Linux Home Automation

  • From: Chuck Stickelman <cstickelman@xxxxxxxxxx>
  • To: ncolug@xxxxxxxxxxxxx
  • Date: Sat, 15 Dec 2007 21:55:13 -0500

Mike wrote:
> Rob Gibson wrote:
>> #! /bin/bash
>>
>> ping -c 5 www.google.com &> /dev/null || echo "FAILURE" >>
>> ~/.x10/$(date)
>> if [ $(ls -1 ~/.x10/ | wc -l) -gt 2 ]
>> then
>>         br -x /dev/ttyS0 X15 OFF
>>         sleep 5
>>         br -x /dev/ttyS0 X15 ON
>>         rm ~/.x10/*200*
>> fi
>
> Two things I'd likely add would be the use of trap and mktemp.  The
> use of mktemp will help with regards to a race condition and the
> possibility of someone overwriting files as you mentioned.  Using trap
> in a sane manner will keep things cleaned up if strange things
> happen.  Not that likely in this case I'd say.
>
> I think I would also change the file name created to something like this.
>
> date +"%Y-%m-%d-%M%S"
>
> This format is an ISO standard, won't create file names with spaces,
> and  will also sort in a sane manner.  Now much of this doesn't apply
> here, it's more about being consistent.  This will help later when you
> look back to something and try and figure out what you were doing.
>

I was going to suggest date -Is to get the ISO behavior.

I often do something like this before editing a file:
cp somefile{,.$(date -Is)}

Chuck


Other related posts: