是否可以使用 tee 捕获标准输入?
Is it possible to capture standard input with tee?
当我将我的程序通过管道传输到 tee
函数时,是否可以捕获来自 scanf(...)
的输入?例如,我有这个程序:
int main() {
int d;
printf("Enter a number: ");
scanf("%d", &d);
printf("Your number is %d\n", d);
}
当我执行命令./a.out | tee out.txt
时,我看到
7
Enter a number: Your number is 7
而out.txt
是:
Enter a number: Your number is 7
如何让终端和输出文件同时显示:
Enter a number: 7
Your number is 7
如果要捕获终端会话,请使用 script
。例如:
$ cat a.c
#include <stdio.h>
int
main(void)
{
int d;
printf("Enter a number: ");
if( scanf("%d", &d) == 1 ){;
printf("Your number is %d\n", d);
}
return 0;
}
$ gcc a.c
$ rm -f foo
$ SHELL=/bin/sh script foo
Script started, output file is foo
sh-3.2$ ./a.out
Enter a number: 5
Your number is 5
sh-3.2$ exit
exit
Script done, output file is foo
$ cat foo
Script started on Wed Apr 7 16:48:03 2021
sh-3.2$ ./a.out
Enter a number: 5
Your number is 5
sh-3.2$ exit
exit
Script done on Wed Apr 7 16:48:13 2021
当我将我的程序通过管道传输到 tee
函数时,是否可以捕获来自 scanf(...)
的输入?例如,我有这个程序:
int main() {
int d;
printf("Enter a number: ");
scanf("%d", &d);
printf("Your number is %d\n", d);
}
当我执行命令./a.out | tee out.txt
时,我看到
7
Enter a number: Your number is 7
而out.txt
是:
Enter a number: Your number is 7
如何让终端和输出文件同时显示:
Enter a number: 7
Your number is 7
如果要捕获终端会话,请使用 script
。例如:
$ cat a.c
#include <stdio.h>
int
main(void)
{
int d;
printf("Enter a number: ");
if( scanf("%d", &d) == 1 ){;
printf("Your number is %d\n", d);
}
return 0;
}
$ gcc a.c
$ rm -f foo
$ SHELL=/bin/sh script foo
Script started, output file is foo
sh-3.2$ ./a.out
Enter a number: 5
Your number is 5
sh-3.2$ exit
exit
Script done, output file is foo
$ cat foo
Script started on Wed Apr 7 16:48:03 2021
sh-3.2$ ./a.out
Enter a number: 5
Your number is 5
sh-3.2$ exit
exit
Script done on Wed Apr 7 16:48:13 2021