Redirect stderr - Linux

By Jacob
at 2012-07-22T06:47
at 2012-07-22T06:47
Table of Contents
※ 引述《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);
}
程式結束
--
: 可是我現在寫的會多一份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
Related Posts
升級 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
Redirect stderr

By Blanche
at 2012-07-22T00:19
at 2012-07-22T00:19
Ubuntu 套件更新版本問題

By Heather
at 2012-07-21T22:59
at 2012-07-21T22:59
Redirect stderr

By Ursula
at 2012-07-21T22:51
at 2012-07-21T22:51