[완료] 펄로 만든 스크립트 실행 결과를 redirection 으로 파일에 출력할때, 다시 화면에 찍기

jufoot의 이미지

예를 들어 셸에서..

$ perl spit.pl > output.txt 를 수행한다고 할때

spit.pl 이 만들어내는 출력의 일부분을 다시 화면에 찍고 싶습니다.

아래 같이 해도 output.txt 에 들어가게 되네요

print STDOUT "twe twe twe\n";

다른 방법이 없을까요?

ktd2004의 이미지

tee 명령어를 보시면 될 것같네요.

perl spit.pl > tee output.txt
jufoot의 이미지

tee 에 대해서 찾아봤습니다.

tee 로 접근하게 되면 화면 or 파일로 선택적으로 출력할 수 없을거 같네요.

정확하게 제가 원하는건 perl 에서 어느 file descriptor 로 보낼지를 결정할 수 있게 하는 것입니다.

제 생각엔.. perl 상에서 해야 할거 같은데 말이죠.. (perl 문서를 뒤적거리고 있습니다..)

jg의 이미지

쉘에서 리디렉트 시키지 마시고 펄 상에서 해보십시오.

open( $OutFh, "> output.txt" ) or die $!;
my $some_result = "This will be shown on both terminal and `output.txt'"
print $_ $some_result for( STDOUT, $OutFh );

만일 리디렉트를 피할 수 없는 상황이면
터미널을 파일핸들러로 여셔야 합니다. 그러면 포터블한 코드는 아니지만
unix계열에서는 /dev/tty를 여시면 되고
좀 전과는 반대로 STDOUT으로 출력한 내용이 파일로 가고
새롭게 연 파일핸들이 터미널과 연결이 됩니다.

open( $TTYFh, "> /dev/tty" );
my $some_result = "This will be shown on both terminal and `output.txt'"
print $_ $some_result for( STDOUT, $TTYFh );

후자의 경우에는 윈도우에서는 지금 테스트 할 수 없는데 혹시 다른 방법이 있을 듯 싶네요.

또한 굳이 STDERR를 다른 용도로 사용하지 않으신다면 그 기본 파일 핸들을 이용하는 것도 괜찮지 않을까요?

$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}

jufoot의 이미지

STDERR 로 깔끔(?) 하게 되긴 하네요.

단지 느낌상 로그가 아닌 에러라는 기분이 들긴 하지만.. 터미널을 여는 방법보다는 깔끔하네요.

생각해보니.. 그냥 redirect 안시키고 파일이름을 argument 로 주는 방법이 더 낫겠군요.

어쨋든 답변 감사합니다~ :-D

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.