![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|