November 30, 2010

C take input from Bash Script

Hi,

I make a simple c file which takes input from user and then display it. I am trying to make a bash script in which i predefined those input and then run the c program inside bash and the c program takes the input which i have defined (i.e. user don't need to enter the input)

Here is the code.

#include<stdio.h>
void main()
{
char ch;
int num;
printf("Enter a single character:\n");
scanf("%c", &ch);
printf("Enter any number:\n");
scanf("%d", &num);

printf("You type charachter %c\n", ch);
printf("You type number %d\n", num);
}

And here is the bash file

#!/bin/bash

ch=s

num=2

./test < $ch $num

When i run the bash script it don't take the input values of ch and num. then i find on Google to use | so i tried ./test | $ch $num but didn't work

Please help me.

Thank you