|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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. |
![]() |
| Viewing: Web Development Archives > FAQs > Unix/Linux > Check for existence (style question) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|