Linux.com

C take input from Bash Script

Link to this post 30 Nov 10

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

Link to this post 03 Dec 10

./test << EOF
$ch
$num
EOF

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board