-1

This is my C program

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;
}

This is my bash shell script

./a.out << 'EOF'
1
4
2
b
-1
EOF

This will execute the code but doesn't save the output

./a.out > outputfile

The above code will save the output, including "Enter n".

I want to execute the code and save only true/false part (i.e excluding all the other printf's). How to store the output of a file along with giving inputs to it?

Sudo Pehpeh
  • 71
  • 1
  • 8
  • 1
    If you know the inputs, avoid unnecessary printf's and you can achieve what you want. – Abhijit Nathwani May 19 '20 at 06:04
  • @SudoPehpeh : Your program writes to stdout, so `outputfile` will contain all the `printf` results. If you don't want to have everything in stdout, write the unneeded part to stderr instead, i.e. `fprintf( stderr, "everything you do not want to capture")` – user1934428 May 19 '20 at 06:44
  • @AbhijitNathwani Normally the file is supposed to be run by the user, so the print statements are necessary to tell the user what to input. shell scripting code is to just check for a given input whether the code is giving the correct input. – Sudo Pehpeh May 19 '20 at 11:42

2 Answers2

1

I made an alternative for a.out, that I can use for testing. is_odd.sh looks for odd umbers:

#!/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

Using this verbose script gives a lot garbage

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

testset

How can you have the same output and only false/true in a file? Use teeto send output to the screen and to some process that will do the filtering:

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

I think above command fits best with your question, I would prefer to see the input strings:

testset | tee >(egrep "Input|false|true" > output)
Walter A
  • 19,067
  • 2
  • 23
  • 43
0
./a.out < input.txt > output.txt
Kioni
  • 37
  • 1
  • 1
  • 7