如何使用bashshell脚本存储C程序的输出

如何使用bashshell脚本存储C程序的输出,c,bash,shell,output,sh,C,Bash,Shell,Output,Sh,这是我的C程序 int main(){ int n; while(1){ printf("Enter n:\n"); scanf("%d",&n); switch(n){ case 1: int t; scanf("%d",&t); if(t<10) printf("true"); else printf("false");

这是我的C程序

int main(){

int n;
while(1){
    printf("Enter n:\n");
    scanf("%d",&n);
    switch(n){
        case 1: int t; 
            scanf("%d",&t);
            if(t<10)
            printf("true");
            else printf("false");
            break;
        case 2: char c;
            scanf("%c",c);
            if(c=='a') printf("true");
            else printf("false");
            break;
        case -1: break;
    }
        if (n==-1) break;   
}

return 0;
}

上述代码将保存输出,包括“输入n”

我希望执行代码并只保存真/假部分(即排除所有其他printf)。如何存储文件的输出并向其提供输入?

/code>/a.outoutput.txt
./a.out < input.txt > output.txt

我为a.out做了一个替代方案,可以用于测试<代码>为奇数。sh查找奇数:

#!/bin/bash

exitfalse() {
   echo $*
   echo false
   exit 1
}

exittrue()
{
   echo $*
   echo true
   exit 0
}

[ $# -eq 0 ] && exitfalse I wanted a number
[[ $1 =~ [0-9]+ ]] || exitfalse Only numbers please
(( $1 % 2  == 0 )) && exitfalse Even number
exittrue Odd number
使用这个冗长的脚本会产生大量垃圾

#!/bin/bash
testset() {
   for input in john doe 1 2 3 4; do
      echo "Input ${input}"
      ./is_odd.sh "${input}"
   done
}

testset
如何在一个文件中有相同的输出而只有false/true? 使用
tee
将输出发送到屏幕和执行过滤的某个进程:

testset | tee >(egrep "false|true" > output)
我认为上述命令最适合您的问题,我更希望看到输入字符串:

testset | tee >(egrep "Input|false|true" > output)

可以但是,只保存开关盒的输出怎么样?如果您知道输入,请避免不必要的printf,这样您就可以实现您想要的。@SudoPehpeh:您的程序写入stdout,因此
outputfile
将包含所有
printf
结果。如果您不想将所有内容都保存在stdout中,请将不需要的部分写入stderr,即
fprintf(stderr,“您不想捕获的所有内容”)
@AbhijitNathwani通常情况下,文件应该由用户运行,因此需要print语句来告诉用户输入什么。shell脚本代码只是检查给定的输入是否正确。
testset | tee >(egrep "false|true" > output)
testset | tee >(egrep "Input|false|true" > output)