Redirect stderr - Linux

Carol avatar
By Carol
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);
: }
: 程式結束

--
Tags: Linux

All Comments

Quanna avatar
By Quanna
at 2012-07-26T17:32
發現順序好像會怪怪的...

Redirect stderr

Jacob avatar
By Jacob
at 2012-07-22T06:47
※ 引述《kdjf (我抓得到什麼呢?)》之銘言: : 可是我現在寫的會多一份PS1,還不知到為什麼 : 有誰可以幫我補上XD? 我提供另一個作法,但缺點是會沒有 prompt,我還沒找到如何解決 XD 將下列程式存成 err2log.c 然後用 cc -o err2log err2log.c 編 ...

有些網站顯示找不到server

Ophelia avatar
By Ophelia
at 2012-07-22T01:07
我在上網的時候基本上正常,不過還蠻常遇到server找不到的問題 (甚至是一些大網站或google的第一項結果) 但是我換回windows去逛,結果又變成正常,所以不是網站沒開 不知道是不是網路設定哪裡出了錯 安裝的時候也是跟著軟體走,沒有動什麼設定 我是fedora新手,想請教一下 - ...

pcmanx 中文顯示(英文環境)

Harry avatar
By Harry
at 2012-07-22T00:55
在英文環境下我在pcmanx上面看到的中文字下面都被截掉部份,造成閱讀困難 可是換成中文環境的時候就會變的正常 請問有什麼解決辦法嗎 我用 fedora 17 - ...

升級 Fedora linux 後 TUI 介面出現

Thomas avatar
By Thomas
at 2012-07-22T00:54
※ 引述《鵟.騎士 andlt;nowayatfakemail.organdgt;, 看板: Linux》之銘言: : 於 西元2012年07月01日 22:20, 無 提到: : andgt; 從 Red Hat 9 陸陸續續升級到 Fedora 17 後,一些 TUI : andgt; ...

新硬碟安裝Ubuntu 12.04無可用空間

Kelly avatar
By Kelly
at 2012-07-22T00:28
用光碟安裝ubuntu時, 檢查硬碟空間跟網路連線的步驟, 也就是選完語系之後的第一步, 軟體檢查我的電腦沒有足夠的可用空間。 但是事實上我的第二顆硬碟是初始化為MBR而尚未未格式化的1TB HD 我要如何開始第一部呢...? 在ubuntu wiki跟鳥哥網站都沒有查到在安裝之前要做甚麼前置動作 ...