The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to copy one folder to another with existing files lalelle Shell Programming and Scripting 2 08-21-2008 09:50 AM
how can i change the date of an existing file adityam UNIX for Dummies Questions & Answers 2 11-22-2007 08:11 AM
Add Multiple Lines in an existing file hkhan12 Shell Programming and Scripting 5 09-08-2006 12:11 PM
Print one line of Existing File danhodges99 UNIX for Dummies Questions & Answers 2 02-25-2003 11:56 AM
pasting text into an existing file darthur UNIX for Dummies Questions & Answers 3 12-13-2001 04:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
crowman crowman is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 12
folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file

So

If /myserver/myfolder/
contains a folder AND that folder conatins a .pdf file
do X
Else
do Z

I may have multiple folders and multiple .pdf files under myfolder. I don't know ahead of time what the folder should be called to do a test. I don't care about the folder name. I don't know what the pdf should be named ahead of time to do a test either. I just care that something ending in .pdf is in the folder under myfolder.
  #2 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
So for each subdirectory, if the subdirectory contains a PDF file, do X, else do Y. What if there are multiple PDF files in a directory? The following will loop over them.


Code:
set -o nullglob
for f in /myserver/myfolder/*/; do
  pdf=false
  for p in "$f"/*.pdf; do
    X
    pdf=true
  done
  if ! $pdf; then
    Z
  fi
done

  #3 (permalink)  
Old 08-28-2008
crowman crowman is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 12
It is ok if a subdirectory contains multiple pdf files. It only needs to have at least one. If I have NO subdirectory OR any subdirectory withOUT a pdf, that should arrive at the same Z error.
  #4 (permalink)  
Old 08-28-2008
crowman crowman is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 12
Thanks for your help! Is that something I put into a script or put into a C program that I can call from the script. Forgive my newbie question.
  #5 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's a script, but it doesn't really conform to your requirements. Specifically, it ignores the case when there is no subdirectory.

The following is a bit contorted but should perhaps work.


Code:
#!/bin/sh
set -o nullglob
pdf=false
for f in /myserver/myfolder/*; do
  test -d "$f" || continue
  for p in "$f"/*.pdf; do
    pdf=true
    X
    break
  done
  $pdf || break
done
$pdf || Z


Last edited by era; 08-28-2008 at 07:10 PM.. Reason: Changed to cope correctly with "any subdirectory without a PDF" requirement
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0