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

2009-01-10 Thread Hongyi Zhao
On Fri, 09 Jan 2009 07:36:26 -0700, Eric Blake e...@byu.net wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

According to Hongyi Zhao on 1/9/2009 6:38 AM:
 But I cann't figure this out, see the following results on my box:
 
 $ echo [ -f *.cache-2 ]
 [ -f *.cache-2 ]

That means there aren't any *.cache-2 files in your directory.  Next, try
this:

$ 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?

Regards,

-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.


--
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/



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

2009-01-10 Thread Eric Blake
-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 e...@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/



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

2009-01-09 Thread Eric Blake
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

According to Hongyi Zhao on 1/8/2009 8:55 PM:
 if [ -f *.cache-2 ] rm *.cache-2
 
 But it seems that this -f parameter in the _if_ command will not work
 with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
 hints on this issue? 

Not cygwin specific.  Your problem is that using a glob can expand to more
than one argument, whereas [ -f ... ] expects exactly one argument in
place of ...; using echo will show you where you went wrong:

echo [ -f *.cache-2 ]

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

Eric Blake e...@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

iEYEARECAAYFAklnTTsACgkQ84KuGfSFAYAIMQCgkGgi59V+gMaWTBoHt+IVg4eL
VmQAoILJS58ZpN1rNerzxw06WxY7ku/T
=bKCT
-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/



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

2009-01-09 Thread Hongyi Zhao
On Fri, 09 Jan 2009 06:12:27 -0700, Eric Blake e...@byu.net wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

According to Hongyi Zhao on 1/8/2009 8:55 PM:
 if [ -f *.cache-2 ] rm *.cache-2
 
 But it seems that this -f parameter in the _if_ command will not work
 with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
 hints on this issue? 

Not cygwin specific.  Your problem is that using a glob can expand to more
than one argument, whereas [ -f ... ] expects exactly one argument in
place of ...;

Thanks for your analyse.

 using echo will show you where you went wrong:

echo [ -f *.cache-2 ]

But I cann't figure this out, see the following results on my box:

$ echo [ -f *.cache-2 ]
[ -f *.cache-2 ]

I cann't see any differences between your code and the following one:

$ echo something and something
something and something

Could you please give me some more hints?

Regards,

-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.


--
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/



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

2009-01-09 Thread Eric Blake
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

According to Hongyi Zhao on 1/9/2009 6:38 AM:
 But I cann't figure this out, see the following results on my box:
 
 $ echo [ -f *.cache-2 ]
 [ -f *.cache-2 ]

That means there aren't any *.cache-2 files in your directory.  Next, try
this:

$ touch a.cache-2 b.cache-2
$ echo [ -f *.cache-2 ]
[ -f a.cache-2 b.cache-2 ]

See how you just gave 2 arguments (instead of the expected 1) to [ -f?

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

Eric Blake e...@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

iEYEARECAAYFAklnYOoACgkQ84KuGfSFAYCkNACgx8OENtoEVdq74p/xmNi+2xUY
YogAn2P59SahElPzMaV6F4zMpBeod0MG
=hW6u
-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/



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

2009-01-08 Thread Yaakov (Cygwin/X)
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Hongyi Zhao wrote:
 I want to delete the all of the cache-2 files under a directory, so I
 use the following line to do this:
 
 if [ -f *.cache-2 ] rm *.cache-2
 
 But it seems that this -f parameter in the _if_ command will not work
 with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
 hints on this issue? 

find /path/to/directory -name '*.cache-2' -delete


Yaakov
Cygwin/X
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEAREIAAYFAklmzBYACgkQpiWmPGlmQSOmSQCfZvId1CpcIoMd944rX6BKtGv7
8o0AoOdW/rs3MjBagIV/qVbBx2odSUZT
=pJb/
-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/



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

2009-01-08 Thread Hongyi Zhao
On Thu, 08 Jan 2009 22:01:26 -0600, Yaakov (Cygwin/X)
yselkow...@users.sourceforge.net wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Hongyi Zhao wrote:
 I want to delete the all of the cache-2 files under a directory, so I
 use the following line to do this:
 
 if [ -f *.cache-2 ] rm *.cache-2
 
 But it seems that this -f parameter in the _if_ command will not work
 with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
 hints on this issue? 

find /path/to/directory -name '*.cache-2' -delete

Good, thanks, I've got it.

-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.


--
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/



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

2009-01-08 Thread Christopher Faylor
On Fri, Jan 09, 2009 at 11:55:46AM +0800, Hongyi Zhao wrote:
Hi all,

I want to delete the all of the cache-2 files under a directory, so I
use the following line to do this:

if [ -f *.cache-2 ] rm *.cache-2

But it seems that this -f parameter in the _if_ command will not work
with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
hints on this issue? 

Why not just

  rm -f *.cache-2

?

--
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/



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

2009-01-08 Thread Hongyi Zhao
On Fri, 9 Jan 2009 00:42:21 -0500, Christopher Faylor
cgf-use-the-mailinglist-ple...@cygwin.com wrote:

On Fri, Jan 09, 2009 at 11:55:46AM +0800, Hongyi Zhao wrote:
Hi all,

I want to delete the all of the cache-2 files under a directory, so I
use the following line to do this:

if [ -f *.cache-2 ] rm *.cache-2

But it seems that this -f parameter in the _if_ command will not work
with wildcard, i.e., the  _[ -f *.cache-2 ]_ isn't a valid arg.  Any
hints on this issue? 

Why not just

  rm -f *.cache-2

A good idea.  Thanks.

-- 
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.


--
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/