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 |
Try using ls:
Code:
for file in `ls /path/to/in/*` |
Using make might be a better idea for this, just as an aside.
|
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 |
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 Code:
find in -type f -name \*.ps|xargs -ix ps2pdf "x" |
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.... Code:
ls: .../in/*.ps: No such file or directory Code:
ls $dir/in/{*.ps,*.prn} 2>/dev/null|while.... Also, can I get the basename command to cope with the 2 different extensions? Dan |
All times are GMT -5. The time now is 07:06 AM. |