Redirect stderr - Linux

By Carol
at 2012-07-22T13:30
at 2012-07-22T13:30
Table of Contents
你是想stdout&stderr -> screen 且 stderr -> file 嗎?
如果我沒搞錯的話應該可以用下面的script完成
str="$@"; ${str#$1} 3>&1 1>&2 2>&3 | tee $1 ;
然後把上面那一行存成xxx.sh 或 xxx
之後你只要呼叫 sh xxx.sh [你要輸出stderr的filename] [你的command] 就可以了
基本上就是swap stdout & stderr 然後用tee去存
嫌打sh太麻煩你就chmod一下即可
※ 引述《bitlife (BIT一生)》之銘言:
: ※ 引述《kdjf (我抓得到什麼呢?)》之銘言:
: : 可是我現在寫的會多一份PS1,還不知到為什麼
: : 有誰可以幫我補上XD?
: 我提供另一個作法,但缺點是會沒有 prompt,我還沒找到如何解決 XD
: 將下列程式存成 err2log.c 然後用 cc -o err2log err2log.c 編譯
: 以如下命令執行
: $ ./err2log 輸出檔案的路徑
: 程式開始:
: #include <stdio.h>
: #include <stdlib.h>
: #include <errno.h>
: #include <fcntl.h>
: #include <unistd.h>
: main(int argc, char *argv[]) {
: if (argc<2) {
: printf("Usage: err2log output-file\n");
: exit(0);
: }
: close(2);
: int fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR, 0644);
: if (fd<0) {
: printf("errno=%d\n", errno);
: perror("");
: exit(errno);
: }
: if (fd!=2) {
: printf("Error!\n");
: exit(0);
: }
: execl("/bin/bash", "/bin/bash", NULL);
: }
: 程式結束
--
如果我沒搞錯的話應該可以用下面的script完成
str="$@"; ${str#$1} 3>&1 1>&2 2>&3 | tee $1 ;
然後把上面那一行存成xxx.sh 或 xxx
之後你只要呼叫 sh xxx.sh [你要輸出stderr的filename] [你的command] 就可以了
基本上就是swap stdout & stderr 然後用tee去存
嫌打sh太麻煩你就chmod一下即可
※ 引述《bitlife (BIT一生)》之銘言:
: ※ 引述《kdjf (我抓得到什麼呢?)》之銘言:
: : 可是我現在寫的會多一份PS1,還不知到為什麼
: : 有誰可以幫我補上XD?
: 我提供另一個作法,但缺點是會沒有 prompt,我還沒找到如何解決 XD
: 將下列程式存成 err2log.c 然後用 cc -o err2log err2log.c 編譯
: 以如下命令執行
: $ ./err2log 輸出檔案的路徑
: 程式開始:
: #include <stdio.h>
: #include <stdlib.h>
: #include <errno.h>
: #include <fcntl.h>
: #include <unistd.h>
: main(int argc, char *argv[]) {
: if (argc<2) {
: printf("Usage: err2log output-file\n");
: exit(0);
: }
: close(2);
: int fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR, 0644);
: if (fd<0) {
: printf("errno=%d\n", errno);
: perror("");
: exit(errno);
: }
: if (fd!=2) {
: printf("Error!\n");
: exit(0);
: }
: execl("/bin/bash", "/bin/bash", NULL);
: }
: 程式結束
--
Tags:
Linux
All Comments

By Quanna
at 2012-07-26T17:32
at 2012-07-26T17:32
Related Posts
Redirect stderr

By Jacob
at 2012-07-22T06:47
at 2012-07-22T06:47
有些網站顯示找不到server

By Ophelia
at 2012-07-22T01:07
at 2012-07-22T01:07
pcmanx 中文顯示(英文環境)

By Harry
at 2012-07-22T00:55
at 2012-07-22T00:55
升級 Fedora linux 後 TUI 介面出現

By Thomas
at 2012-07-22T00:54
at 2012-07-22T00:54
新硬碟安裝Ubuntu 12.04無可用空間

By Kelly
at 2012-07-22T00:28
at 2012-07-22T00:28