Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I provide input to a C program from bash? [duplicate]

Tags:

I have been reading a ton about bash scripts and program testing but I am still unable to make this code work.

Basically it is a simple program that asks the user for either of north east south or west. I start the program then it immediately asks for input. I just can not get the bash script to give it any input. I tried using echo and expect.

Any help is appreciated.

Here is the function used to get the players input:

int process_input(Map *game) {     printf("\n> ");      char ch = getchar();     char a = getchar(); //eat enter      int damage = rand() % 4;      switch(ch) {          case -1:             printf("you suck\n");             return 0;             break;         case 'n':             game->proto.move(game, NORTH);             break;         case 's':             game->_(move)(game, SOUTH);             break;         case 'e':             game->_(move)(game, EAST);             break;         case 'w':             game->_(move)(game, WEST);             break;         case 'a':             game->_(attack)(game, damage);             break;         case 'l':                     printf("You can go:\n");                     if(game->location->north) printf("NORTH\n");                     if(game->location->south) printf("SOUTH\n");                     if(game->location->east) printf("EAST\n");                 if(game->location->west) printf("WEST\n");                     break;         default:             printf("Whats next?", ch);         }         return 1; } 

And here is the attempt at a bash script:

   #!/bin/bash     /Desktop/c     ./ex17 echo 'w' 
like image 333
blankwall Avatar asked May 12 '13 15:05

blankwall


People also ask

How do I give input to a program that I am calling inside bash script?

You can do either a C code or a Bash script, not a mixed one. Use scanf() for reading from keyboard (it stops reading when you hit ENTER) and complete this code in C language.

How do I control C in a bash script?

You can use the trap builtin to handle a user pressing ctrl-c during the execution of a Bash script. e.g. if you need to perform some cleanup functions.

What is &2 in bash script?

and >&2 means send the output to STDERR, So it will print the message as an error on the console. You can understand more about shell redirecting from those references: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirections.


2 Answers

You can feed input into a program from bash using any of the following mechanisms.

For a single line of input, you can use a here-string:

./ex17 <<<'w' 

For multiple lines, you can use a here-document:

./ex17 <<'EOF' w second line of input more input EOF 

Or you can move those lines out of the script and into a separate file:

./ex17 <filename     

More generally, you can run a command that generates as its output the desired input to your program, and connect them together with a pipe. For instance, the above could also be written:

cat filename | ./ex17 

or the original example as

echo w | ./ex17 

That's more general because you can replace cat and echo here with any sort of program, which can do all sorts of computation to determine what it outputs instead of just dumping the contents of a static string or file.

But what you can't easily do from bash is drive input, read output, and make decisions about what to send as the next input. For that, you should look at expect. An expect script would look something like this:

#!/usr/bin/env expect spawn ./ex17 expect ">" send "w\n" expect "Whats next?" send "next line here\n" # turn it back over to interactive user interact 
like image 121
Mark Reed Avatar answered Oct 04 '22 21:10

Mark Reed


Try this: first:

 echo w | ./ex17  

This will send w to the example and output the move. This is called piping; and it essentially connects the stdout of echo to the stdin of ex17

like image 30
Ahmed Masud Avatar answered Oct 04 '22 20:10

Ahmed Masud