check file

check file

Post by Kennet » Thu, 31 Jul 2003 22:36:17



I want to check if there exist any filename with test in it by
if [[ -a test* ]]; then
    echo "file exist"
    else
    echo "file not exist"
fi
But it always return not exist. How can I use wildcard in check the
existence of certain type of files? Thanks.
 
 
 

check file

Post by Ben » Fri, 01 Aug 2003 23:05:56



> I want to check if there exist any filename with test in it by
> if [[ -a test* ]]; then
>     echo "file exist"
>     else
>     echo "file not exist"
> fi
> But it always return not exist. How can I use wildcard in check the
> existence of certain type of files? Thanks.

   if [[ -n $(ls test*) ]]; then

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Ben » Fri, 01 Aug 2003 23:07:20



>   if [[ -n $(ls test*) ]]; then

   if [[ -n $(ls test* 2> /dev/null) ]]; then

to avoid stderr output on failure.
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Joe Halpi » Fri, 01 Aug 2003 23:23:40




> >   if [[ -n $(ls test*) ]]; then
>    if [[ -n $(ls test* 2> /dev/null) ]]; then

> to avoid stderr output on failure.
> Ben

or

if [[ -n $(ls test* > /dev/null 2>&1) ]]; then

To avoid stdout output on success :-)

Or more simply

ls test* >/dev/null 2>&1 && echo found it

Joe

 
 
 

check file

Post by Joe Halpi » Fri, 01 Aug 2003 23:24:34





> > >   if [[ -n $(ls test*) ]]; then
> >    if [[ -n $(ls test* 2> /dev/null) ]]; then

> > to avoid stderr output on failure.
> > Ben

> or

> if [[ -n $(ls test* > /dev/null 2>&1) ]]; then

> To avoid stdout output on success :-)

Doh!!

Never mind, I haven't had my coffee yet this morning.

Quote:> Or more simply

> ls test* >/dev/null 2>&1 && echo found it

> Joe

 
 
 

check file

Post by Michael Wa » Sat, 02 Aug 2003 02:36:16





>> I want to check if there exist any filename with test in it by
>> if [[ -a test* ]]; then
>>     echo "file exist"
>>     else
>>     echo "file not exist"
>> fi
>> But it always return not exist. How can I use wildcard in check the
>> existence of certain type of files? Thanks.

>   if [[ -n $(ls test*) ]]; then

Why use "ls" which causes shell to fork()?
--

 
 
 

check file

Post by Ben » Sat, 02 Aug 2003 02:43:17






>>>I want to check if there exist any filename with test in it by
>>>if [[ -a test* ]]; then
>>>    echo "file exist"
>>>    else
>>>    echo "file not exist"
>>>fi
>>>But it always return not exist. How can I use wildcard in check the
>>>existence of certain type of files? Thanks.

>>  if [[ -n $(ls test*) ]]; then

> Why use "ls" which causes shell to fork()?

Guess.

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Ben » Sat, 02 Aug 2003 03:12:51



>>  if [[ -n $(ls test*) ]]; then

> Why use "ls" which causes shell to fork()?

The non-forked method I thought of is on the convoluted side, but the
above is nice and clear.  If you plan on running the above in a tight
loop several million times the convoluted method would be preferable,
but then perhaps a shell script in that case would not be.

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Michael Wa » Sat, 02 Aug 2003 03:18:27








>>>>I want to check if there exist any filename with test in it by
>>>>if [[ -a test* ]]; then
>>>>    echo "file exist"
>>>>    else
>>>>    echo "file not exist"
>>>>fi
>>>>But it always return not exist. How can I use wildcard in check the
>>>>existence of certain type of files? Thanks.

>>>  if [[ -n $(ls test*) ]]; then

>> Why use "ls" which causes shell to fork()?

>Guess.

I can not guess out a good reason. The following no fork() solution
is more efficient, which works in both ksh abd bash.

shopt -s extglob 2>/dev/null


  echo "no files that matches test*"
else
  echo "there are 1 or more files that matches test*"
fi
--

 
 
 

check file

Post by Ben » Sat, 02 Aug 2003 03:24:03



> I can not guess out a good reason. The following no fork() solution
> is more efficient, which works in both ksh abd bash.

> shopt -s extglob 2>/dev/null


>   echo "no files that matches test*"
> else
>   echo "there are 1 or more files that matches test*"
> fi

It's a nice solution, but not immediately clear and whose efficiency
would almost never be realised.

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Dan Merce » Sat, 02 Aug 2003 04:40:39


: I want to check if there exist any filename with test in it by
: if [[ -a test* ]]; then
:     echo "file exist"
:     else
:     echo "file not exist"
: fi
: But it always return not exist. How can I use wildcard in check the
: existence of certain type of files? Thanks.
:
:

x=0
for i in test*
    do
    [[ -a $i ]] || continue
    x=1
    done
if [[ $x -ne 0 ]]
    then
    echo file exists
else
    echo file does not exist
fi
Kudos if you can tell me why you use "|| continue"
instead of "|| break"

Dan Mercer

 
 
 

check file

Post by Ed Morto » Sat, 02 Aug 2003 04:30:56




>>  if [[ -n $(ls test*) ]]; then

>    if [[ -n $(ls test* 2> /dev/null) ]]; then

Or just:

        if ls test* >/dev/null 2>&1; then

Regards,

        Ed.

 
 
 

check file

Post by Ben » Sat, 02 Aug 2003 05:14:12



> x=0
> for i in test*
>     do
>     [[ -a $i ]] || continue
>     x=1
>     done
> if [[ $x -ne 0 ]]
>     then
>     echo file exists
> else
>     echo file does not exist
> fi

This is similar to my convoluted way. I don't think it is worth the
effort though.

Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

check file

Post by Barry Margoli » Sat, 02 Aug 2003 03:50:13





>>>  if [[ -n $(ls test*) ]]; then

>> Why use "ls" which causes shell to fork()?

>The non-forked method I thought of is on the convoluted side, but the
>above is nice and clear.

Couldn't you just use "echo" instead of "ls"?  Why does it need to be
convoluted?

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

check file

Post by Michael Wa » Sat, 02 Aug 2003 07:51:45







>>>>  if [[ -n $(ls test*) ]]; then

>>> Why use "ls" which causes shell to fork()?

>>The non-forked method I thought of is on the convoluted side, but the
>>above is nice and clear.

>Couldn't you just use "echo" instead of "ls"?  Why does it need to be
>convoluted?

echo test* will produces "test*" when no match is found.

I provided a non-forked and non-convoluted method in a separate post.
--

 
 
 

1. A script that will only check file if it has been modified since last check

I have a script that greps the /var/adm/messages file for a pattern, write
it to a file. Then emails me a warning.

The thing is I want to run this regularly, and the messages file is not
rotated regularly. How do I add this to the script that I only want it to
grep the file and email me if there is a new "pattern" not the same
"pattern" from an hour ago, or yesterday.

grep "currently marked as unusable" $ADMSG > $TEMP
grep "No more IP addresses on 192.168.2.0 network" $ADMSG >> $TEMP
if [ -s $TEMP ]
then
        /usr/sbin/pntadm -P 192.168.2 >> $TEMP
fi

#rm -f $TEMP

Yes, a will be installing a logchecker, but for now this is what I have time
for...

2. Routing problem

3. date checking file by file

4. Complete O'Reilly X Programming Series on Ebay

5. Check file size before ftp-ing

6. No verticle alignment in tabular

7. Script that recurses through all directories tries to check file/directory named "*"

8. Deleting $MAIL

9. Check file access right of other user

10. Checking file date

11. Question: Checking Files Open by Other Processes.

12. Check file format (dos or unix)

13. How to check file size and create date < 24hr?