LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   tcsh How to execute foreach in subshell? (https://www.linuxquestions.org/questions/programming-9/tcsh-how-to-execute-foreach-in-subshell-566823/)

kaz2100 07-05-2007 08:43 AM

tcsh How to execute foreach in subshell?
 
Hya,

Following is the digest of my code
Code:

#!/bin/tcsh

foreach a ("`cat inputfile`")
++ do some work for each line ++
end

and I want to pipe entire output into "sort".

I can do
Code:

myProgram inputfile | sort
, but I want everything in one shell script for easier maintenance. So I tried to send foreach to subshell
Code:

#!/bin/tcsh
(
foreach a ("`cat inputfile`")
++ do some work for each line ++
end
) | sort

and tcsh chokes saying too many ('s.

Question is
How to send foreach to subshell?
Or any other idea?

My tcsh is 6.14.00 Astron 2005-3-15 (Debian etch one)

Thanks

Happy Penguins!

makyo 07-07-2007 07:10 AM

Hi.

In Section 2b of http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ there is the statement:
Quote:

You can't mix flow-control and commands, like this:

who | while read line; do
echo "gotta $line"
done
Although tcsh is different from csh, I think most of the pros and cons of csh apply to tcsh.

You may have seen in man tcsh that foreach and end keywords must "appear in a single simple command on an input line".

I'd be interested in hearing if there is a solution to your problem, but I don't think it would lure me into doing much scripting in tcsh.

As you are no doubt weary of hearing: most people use Bourne family shells for scripting ... cheers, makyo


All times are GMT -5. The time now is 10:18 AM.