GNU bug report logs - #9742
touch option for existence?

Previous Next

Package: coreutils;

Reported by: Morty <morty+coreutils <at> frakir.org>

Date: Thu, 13 Oct 2011 01:52:01 UTC

Severity: normal

Tags: notabug

Done: Jim Meyering <jim <at> meyering.net>

Bug is archived. No further changes may be made.

Full log


Message #13 received at control <at> debbugs.gnu.org (full text, mbox):

From: Jim Meyering <jim <at> meyering.net>
To: Morty <morty+coreutils <at> frakir.org>
Cc: Pádraig Brady <P <at> draigBrady.com>,
	9742-close <at> debbugs.gnu.org
Subject: Re: bug#9742: touch option for existence?
Date: Thu, 13 Oct 2011 15:37:42 +0200
tags 9742 + notabug
thanks

Morty wrote:
> On Thu, Oct 13, 2011 at 10:05:05AM +0100, Pádraig Brady wrote:
>
>> Note the above is easier to express in shell like:
>>
>>   [ -e "$file" ] || touch "$file"
>
> Thanks, I'm familiar with it.  :)
>
>> But that is racy.  If you were using touch for locking purposes
>
> I'm using touch to make sure the file exists before an operation that
> will yield an error if it doesn't exist.  For example:

Thanks for the suggestion.

However, when there's such an easy and portable way to do what you want,

  touch_if_absent() { test -e "$1" || touch "$1"; }

it is counterproductive (and contrary to the "Unix way") to encumber
a tool like "touch" with an option to provide that functionality.
Besides, then you'd have to wait until a release including your
option makes it into whatever distribution you use (or build from source).
With the above, you have something that is portable and works now.

> resolv=/etc/resolv.conf
> touch $resolv
> ci -l -t-$resolv -m"check-in existing" $resolv
> grep -q domain $resolv || echo domain $domain >> $resolv
> ci -l -m"add domain $domain" $resolv
>
> The intent of code like the above is to make sure a certain file is in
> revision control and contains a certain setting.  If the file already
> is in revision control and has the setting, the code should do
> nothing.  But touch changes the mtime.  The problem could be fixed by
> slinging around ifs, but that's inelegant.




This bug report was last modified 13 years and 228 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.