Unix/Linux
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Unix/Linux

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 2nd, 2008, 06:49 AM
pk
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Check for existence (style question)

Wednesday 2 July 2008 13:33, RValentinFischer wrote:

I would like to test, whether a directory mydir contains at least one
file which has abc in its name.
This is one way to do it on Solaris ( Linux, the option used on
fgrep must be -q):
>

if { ls mydir | fgrep -s abc }
then
: # yes, one or more files with names containing abc exist in mydir
fi
>

But I don't like this solution. First, it is not portable between
Solaris and Linux.

It is, if you use "grep -F -q" instead of fgrep.

Second, it looks
to me overly complicated.
>

Is there a nicer way this can be done entirely within bash or zsh?
bash would be preferred
because I'm supposed to use bash for the task, so I would need to have
compelling reasons
for using zsh.

A (really) ugly trick with bash would probably be

shopt -s nullglob
for i in *abc*; do
# your code here
break;
done

But if you're worried about style, then the above code most certainly won't
fit your needs :-)

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Reply With Quote
  #2  
Old July 2nd, 2008, 06:49 AM
RonaldOttoValentinFischer
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Check for existence (style question)

I would like to test, whether a directory mydir contains at least one
file which has abc in its name.
This is one way to do it on Solaris ( Linux, the option used on
fgrep must be -q):

if { ls mydir | fgrep -s abc }
then
: # yes, one or more files with names containing abc exist in mydir
fi

But I don't like this solution. First, it is not portable between
Solaris and Linux. Second, it looks
to me overly complicated.

Is there a nicer way this can be done entirely within bash or zsh?
bash would be preferred
because I'm supposed to use bash for the task, so I would need to have
compelling reasons
for using zsh.

Ronald

Reply With Quote
  #3  
Old July 2nd, 2008, 07:30 AM
Joachim Schmitz
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Check for existence (style question)

RValentinFischer wrote:
I would like to test, whether a directory mydir contains at least one
file which has abc in its name.
This is one way to do it on Solaris ( Linux, the option used on
fgrep must be -q):
>

if { ls mydir | fgrep -s abc }
then
: # yes, one or more files with names containing abc exist in mydir
fi
>

But I don't like this solution. First, it is not portable between
Solaris and Linux. Second, it looks
to me overly complicated.
>

Is there a nicer way this can be done entirely within bash or zsh?
bash would be preferred
because I'm supposed to use bash for the task, so I would need to have
compelling reasons
for using zsh.
Hmm, maybe this:
for f in *abc*; do [ "$f" != '*abc*' ] && echo Yes; break; done

Bye, Jojo



Reply With Quote
  #4  
Old July 2nd, 2008, 12:30 PM
Janis Papanagnou
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Check for existence (style question)

RValentinFischer wrote:
I would like to test, whether a directory mydir contains at least one
file which has abc in its name.
This is one way to do it on Solaris ( Linux, the option used on
fgrep must be -q):
>

if { ls mydir | fgrep -s abc }
then
: # yes, one or more files with names containing abc exist in mydir
fi
>

But I don't like this solution. First, it is not portable between
Solaris and Linux. Second, it looks
to me overly complicated.

If you want to keep the if/ls pattern and don't mind redirection of
irrelevant output

if ls mydir/*abc* >/dev/null 2>&1
then
echo yes
fi



set - mydir/*abc*
[ -f "$1" ] && echo Yes

It surely depends what one considers complicated. Is using a pipe
and two external processes complicated? Is using only standard shell
features complicated? You decide.

Janis

>

Is there a nicer way this can be done entirely within bash or zsh?
bash would be preferred
because I'm supposed to use bash for the task, so I would need to have
compelling reasons
for using zsh.
>

Ronald

Reply With Quote
  #5  
Old July 2nd, 2008, 05:10 PM
pk
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Check for existence (style question)

Wednesday 2 July 2008 21:16, bsh wrote:

>set - mydir/*abc*
>[ -f "$1" ] && echo Yes
>

This is not a determinate test. If there is a file named
"*abc*", this test will also fail.

No, since '*abc*' is a valid expansion for *abc*, after all.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Reply With Quote
Reply

Viewing: Web Development Archives FAQs Unix/Linux > Check for existence (style question)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway