This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Delete a specific type of files when they exist.


-----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@byu.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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]