Is there a way to make this perl code capture stderr as well as stdout from a tcsh?

て烟熏妆下的殇ゞ 提交于 2019-12-22 00:27:21

问题


    open UNIT_TESTER, qq(tcsh -c "gpath $dir/$tsttgt;  bin/rununittests"|);
    while(<UNIT_TESTER>){
        reportError($ignore{testabort},$tsttgt,"test problem detected for $tsttgt:$_ ") if /core dumped/;
        reportError($ignore{testabort},$tsttgt,"test problem detected for $tsttgt:$_ ") if /\[  FAILED  \]/;
        writelog($tsttgt,$_);
    }
    close UNIT_TESTER;

I have tried to redirect stderr to stdout using this syntax but it didn't work:

open UNIT_TESTER, qq(tcsh -c "gpath $dir/$tsttgt;  bin/rununittests >& "|);

I have also read the discussion on the perl FAQ but that was in relation to bash: http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q5.15.html


回答1:


I would suggest you do your capturing with the Capture::Tiny module from CPAN. It's small, simple, and well tested. It has an elegant API and if you totally can't have any dependencies, it can be embedded into your program easily.

Apart from that: If you have any control over the testing program being run, I would suggest you investigate the Test Anything Protocol. If you can make your test program output TAP, then your use case including a nice summary of the tests becomes as simple as:

perl -MTest::Harness -e 'runtests(@ARGV)' bin/rununittests

NB about the second paragraph: Potentially recent Test::Harness required. Also, doesn't quite do what you need regarding the shell invocation, but it should get you close enough.




回答2:


Try redirecting handle 2 (stderr) to handle 1 (stdout) as follows.

open UNIT_TESTER, qq(tcsh -c "gpath $dir/$tsttgt; bin/rununittests 2>&1 "|);




回答3:


Two options (at least) spring to mind:

open UNIT_TESTER, qq(tcsh -c "gpath $dir/$tsttgt >&;  bin/rununittests >& "|);

and

open UNIT_TESTER, qq(sh -c "{ gpath $dir/$tsttgt;  bin/rununittests; } 2>&1"|);

The second is a cheat; it uses Bourne/Korn/POSIX/Bash shell notation. Note that unless you are careful, you end up with the error output of only the second command and not of the first.




回答4:


Thanks to BillThor I stumbled upon a solution:

open UNIT_TESTER, qq(tcsh -c "gpath $dir/$tsttgt;  bin/rununittests |& cat "|);


来源:https://stackoverflow.com/questions/2995304/is-there-a-way-to-make-this-perl-code-capture-stderr-as-well-as-stdout-from-a-tc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!