簡體   English   中英

linux 中的“tee”命令可以打印 C 程序的輸入和 output 嗎?

[英]Can “tee” command in linux print both the input and the output of a C program?

我有一個簡單的 C 程序,它會要求從用戶那里獲取 integer,然后它會打印出 integer。

#include <stdio.h>
int main() {   
    int number;
    printf("Enter an integer: ");  
    scanf("%d", &number);
    printf("You entered: %d", number);  
    return 0;}

當我使用這個命令時:

gcc program.c -o test
./test | tee text.txt

在終端上運行的程序不會打印輸入 integer 行,而是等待輸入,當我提供該輸入時,它會打印它並打印到 text.txt 文件夾中。 我想按原樣運行程序並將終端上運行的所有內容存儲到 text.txt 文件夾中,包括輸入和 output。 有什么可能的方法嗎?

tee命令使用一個輸入,但您想要捕獲兩個。 稍加注意,您可以使用兩個單獨的tee命令,將輸入和 output 復制到同一個文件,但最好使用專為您的目的設計的實用程序,例如script

對於基於 Debian 的 Linux,運行apt install devscripts ,然后嘗試使用annotate-output工具。 例如,使用進程替換和根本不存在的文件運行cat

annotate-output cat <(echo hello) /bin/nosuchfile

...顯示否則將輸入的內容,output 和標准錯誤 output,全部發送到標准 output,然后可以通過管道傳輸到文件:

13:01:03 I: Started cat /dev/fd/63 /bin/nosuchfile
13:01:03 O: hello
13:01:03 E: cat: /bin/nosuchfile: No such file or directory
13:01:03 I: Finished with exitcode 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM