Subject: bug#9742: closed (Re: bug#9742: touch option for existence?)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 13 Oct 2011 13:39:03 +0000

Your bug report

#9742: touch option for existence?

which was filed against the coreutils package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
9742: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9742
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#9742: touch option for existence?
From: Jim Meyering <[email-masked]>
To: Morty <[email-masked]>
Cc: Pádraig Brady <[email-masked]>, [email-masked]
Date: Thu, 13 Oct 2011 15:37:42 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

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.

Subject: touch option for existence?
From: Morty <[email-masked]>
To: [email-masked]
Date: Wed, 12 Oct 2011 21:45:25 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

[This is more of a feature request than a bug request.  But I don't
see where I can make feature requests.]

It would be nice if touch had an option to only "touch" if the file
doesn't already exist.  Sort of like -c, but the other way around.
This is useful because often, the reason one is using "touch" in a
script is because one wants to make sure that a file exists before
doing an operation that expects the file to already exist.  But if the
file already exists, then touch has a side effect of changing the
mtime or the atime.  [On many systems, one can work on a file without
changing the atime thanks to mount options such as noatime or
relatime.]

It's easy enough to wrap touch in an if:

  if [ ! -e $file ]; then touch $file; fi

But it would be nicer if this common case were built in.  I would
suggest -e, for exists.  Thanks!

- Morty