delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/01/10/10:37:35

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_SOFTFAIL
X-Spam-Check-By: sourceware.org
Message-ID: <4968C09A.4090609@byu.net>
Date: Sat, 10 Jan 2009 08:36:58 -0700
From: Eric Blake <ebb9 AT byu DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.19) Gecko/20081209 Thunderbird/2.0.0.19 Mnenhy/0.7.5.666
MIME-Version: 1.0
To: cygwin AT cygwin DOT com, hongyi DOT zhao AT gmail DOT com
Subject: Re: Delete a specific type of files when they exist.
References: <rfidm4hfs5p9h6s40obf5s083onsau7j26 AT 4ax DOT com> <49674D3B DOT 20008 AT byu DOT net> <hjkem49kere1sftdo06po7adjapjqq4s48 AT 4ax DOT com> <496760EA DOT 10008 AT byu DOT net> <hbsgm4hpp273kkr3nbri9k9liv5bpmhjdm AT 4ax DOT com>
In-Reply-To: <hbsgm4hpp273kkr3nbri9k9liv5bpmhjdm@4ax.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Hongyi Zhao on 1/10/2009 3:05 AM:
>> $ touch a.cache-2 b.cache-2
>> $ echo [ -f *.cache-2 ]
>> [ -f a.cache-2 b.cache-2 ]
> 
> In your example, considering that the a.cache-2 and b.cache-2 have
> been created by touch command, both the [ -f a.cache-2] and [ -f
> b.cache-2 ] should have the value: true.  My issue is: how can I grab
> this value, say, by using echo command?

Your question is not cygwin specific; I repeat the advice you have been
given to seek out a more generic introduction or online forum that
discusses basic shell programming constructs, rather than using this list.

That said, there are multiple ways to determine if you have one or more
file matching a given pattern.  Among others, this (bash-specific) way
avoids forking, by using nullglob to avoid confusion when a glob has no
matches, and by using printf -v to assign a variable without a command
substitution:

$ restore=
$ shopt -q nullglob || restore='shopt -u nullglob'
$ shopt -s nullglob
$ printf -v exist %s *.cache-2
$ if [ -n "$exist" ] ; then
>   echo at least one file exists with .cache-2 extension
> else
>   echo no .cache-2 exist
> fi
$ eval $restore

More portable (but at the cost some forks) is this:

$ if [ "`echo *.cache-2`" != "*.cache-2" ] || [ -f "*.cache-2" ] ; then
>   echo at least one file exists with .cache-2 extension
> else
>   echo no .cache-2 exist
> fi

which takes care of the (admittedly rare) case of having a file literally
named *.cache-2.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9 AT byu DOT net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklowJoACgkQ84KuGfSFAYB5BgCghRn3ai8bKZ8ui/3mW0LDRQx9
p8AAnjqsDY42G8/AUbFBjs7p3iIC6B+Z
=+CqH
-----END PGP SIGNATURE-----

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019