Click to See Complete Forum and Search --> : Cannot find "foreach" command
FaStAndFuRiouS
05-31-2004, 04:29 AM
I'm creating a shell script and I need to use the "foreach" command. However, I get the following error: "bash: foreach: command not found." How do install this or make it run?
I'm running RedHat 9. Thanks.
deathadder
05-31-2004, 05:06 AM
Have a search on your Red Hat cd's for "foreach", without the speach marks, if you can't find it have a search on www.google.com/linux or www.rpmfind.net Then its just a case of installing the rpm you get for it.
bwkaz
05-31-2004, 08:37 AM
"foreach" is a command in the C shell, not the Bourne shell, and your shell is the Bourne shell (on Linux, just about every user's shell is).
The C shell is broken (http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ -- this is most of the reason why on Linux, most everybody's shell is the Bourne shell).
Therefore, you don't actually need foreach -- using it means that you'll be cultivating practices that are going to bite you when you need to (a) modify init scripts, (b) use a machine that doesn't have csh, or (c) modify anything else that requires that you use the Bourne shell.
Why do you think you need foreach?
FaStAndFuRiouS
05-31-2004, 07:41 PM
Thanks for the responses. I'm really not sure if I was right in using foreach. Anyway, here's what I need to do. I need to convert hundreds of HTML files located in a directory automatically to PDF using unix. And I found that HTMLDOC can do the job. Its syntax is:
htmldoc --webpage -f outpputfile.pdf inputfile.html
The problem is that it doesn't support wildcards or batch processing. So the only way I have to go about it is create a script. I was thinking of doing something like:
ls *.html > dir_list
HTML_FILES=`sed s/.html//g dir_list`
foreach ${HTML_FILES}
{htmldoc --webpage -f ${HTML_FILES}.pdf {${HTML_FILES}.html}
I'm not sure about the syntax I used for "foreach" but basically that's the logic I was going for. Any ideas in going about this using the Bourne shell? I'm really not an expert in scripts so I'd appreciate any help I can get. Thanks a lot.
Originally posted by bwkaz
"foreach" is a command in the C shell, not the Bourne shell, and your shell is the Bourne shell (on Linux, just about every user's shell is).
The C shell is broken (http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ -- this is most of the reason why on Linux, most everybody's shell is the Bourne shell).
Therefore, you don't actually need foreach -- using it means that you'll be cultivating practices that are going to bite you when you need to (a) modify init scripts, (b) use a machine that doesn't have csh, or (c) modify anything else that requires that you use the Bourne shell.
Why do you think you need foreach?
bwkaz
05-31-2004, 09:10 PM
#!/bin/bash
for file in *.html ; do
htmldoc --webpage -f "$(basename "$file" .html).pdf" "$file"
done The quotes are necessary to handle the case where a filename might have spaces in it.
FaStAndFuRiouS
05-31-2004, 10:34 PM
Great! It's working now. Thanks a lot, man. I appreciate the help.:D