LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2003, 12:10 PM   #1
deadlock
Member
 
Registered: Apr 2003
Distribution: Red Hat
Posts: 65

Rep: Reputation: 15
bash for loop problem


After some earlier help in this forum I have created a bash script that watches for files in a folder "in" and outputs pdf files to an "out" folder. During testing however, I have found that there is a problem when the loop is blank.

The basic code is as follows:

cd in
for file in *
do
// create pdf for $file
done

However when the folder "in" is empty, linux insists on passing a file "*" back to the script. This that messes up the logging of events and the pdf distiller.

I have tried putting a line such as:

if $file <> "*"

but bash gives an error at the line

Any suggestions?

(the for file in * was the best solution for the earlier problems)

Cheers

Dan
 
Old 09-03-2003, 12:46 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Try using ls:
Code:
for file in `ls /path/to/in/*`
do
// create pdf for $file
done
 
Old 09-03-2003, 02:19 PM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Using make might be a better idea for this, just as an aside.
 
Old 09-04-2003, 02:47 AM   #4
deadlock
Member
 
Registered: Apr 2003
Distribution: Red Hat
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks

I have tried using

for file in ls...

and had problems with spaces and command chars in file names being interpreted by bash - unfortunately I need to be able to treat the names exactly as they are.

How do you mean use make???

Dan
 
Old 09-04-2003, 04:08 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you execute your script in debug mode (sh -x <script>) you'd see the "for" loop breaking the results from "ls" up. If you use a "while" loop it won't. Only listing files with for instance the .ps extension limits the output of "ls", basename strips the extension which comes in handy when moving if your create_pdf_cmd doesn't output to stdout and doesn't have output options.

Code:
ls in/*.ps|while read file; do
filen=$(basename "$file" .ps)
create_pdf_cmd "$file" > in/"$filen".pdf
mv in/"$filen".pdf out; done
...or with "xargs" if you don't want to use a "while" loop. You're not restricted to using "ls" as well. If you know your input filetype always is Postscript and has a postscript extension, you could use
Code:
find in -type f -name \*.ps|xargs -ix ps2pdf "x"
else skip the "-name \*.ps" part. Note I don't use curly braces, I use "-ix" to get quoted variable x.
 
Old 09-04-2003, 04:32 AM   #6
deadlock
Member
 
Registered: Apr 2003
Distribution: Red Hat
Posts: 65

Original Poster
Rep: Reputation: 15
That looks great, one quick question. The source files will either be .ps or .prn, which ghostscript copes with equally well, so the ls command I am using is:

Code:
ls $dir/in/{*.ps,*.prn}|while....
However if the "in" directory is empty, or there are no .ps files (and are .prn files) I get an on-screen error such as:

Code:
ls: .../in/*.ps: No such file or directory
As I want this script to be run on a cron, I obviously don't want to have messages filling up the log. As this error is obviously useless (as I don't need to know the folders empty) would a redirect to /dev/null be OK? e.g.

Code:
ls $dir/in/{*.ps,*.prn} 2>/dev/null|while....
or do I need to redirect the results to the while loop?

Also, can I get the basename command to cope with the 2 different extensions?

Dan

Last edited by deadlock; 09-04-2003 at 04:39 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
compiling bash 3.0 results in a loop bucovaina78 Linux - Software 2 05-26-2005 04:04 AM
bash script for loop drisay Programming 5 12-25-2004 12:32 AM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration