Shell script to run program that asks for input - c

I have a C program in which two scanf() are done.
I need to write a shellscript that will run the program and give it the arguments too.
The problem is that all I could come up with is how to pipeline an argument into the program as a command line arguments which is not what I need.
Any help appreciated.

This should work
echo "some input" | yourprog
e.g. echo "1 1 + p" | dc

Related

Giving inputs to an executed program automatically

I have a C program that I want to run without having to manually type commands into. I have 4 commands (5 if you count the one to exit the program) that I want given to the program and I don't know where to start. I have seen some stuff like
./a.out <<<'name'
to pass in a single string but that doesn't quite work for me.
Other issues I have that make this more difficult are that one of the commands will give an output and that output needs to be a part of a later command. If I had access to the source code I could just brute force in some loops and counters so I am trying to get a hold of it but for now I am stuck working without it. I was thinking there was a way to do this with bash scripts but I don't know what that would be.
In simple cases, bash script is a possibility: run the executable in coproc (requires version 4). A short example:
#!/bin/bash
coproc ./parrot
echo aaa >&${COPROC[1]}
read result <&${COPROC[0]}
echo $result
echo exit >&${COPROC[1]}
with parrot (a test executable):
#!/bin/bash
while [ true ]; do
read var
if [ "$var" = "exit" ]; then exit 0; fi
echo $var
done
For a more serious scenarios, use expect.

Need a shell script to automatically input 1-10000 into a program

Pretty simple, I need to test a C program by inputting a large number of integers into it. To do this, for this particularly case, I need to run the program, then when it waits for user input (scanf("%d, integer)), I need to give it the integer from the loop I'm currently at.
right now I have
for i in {1..5};
do
(echo -n "$i" && ./a2 $i)
done
but it still just waits for user input, then after user input is given, prints the integer of the loop I'm on. I've tried a few examples from similar problems I've found on stack exchange and elsewhere but so far no luck. I haven't ever really needed to mess about with shell scripting before so it's probably something simple I'm doing backasswordsly but no one else has done wrong before.
Try this!
for i in `seq 1 1000`; do echo $i | ./a2; done
Your solution
echo -n "$i" && ./a2 $i
would pass $i as argument for a2 not input.
I think what you need is not usually done using shell script.
You can write a c code to generate your input, which in this case is numbers from 1-10000. Let that file be testGenerator.c. Then, run this on your terminal:
gcc testGenerator.c
./a.out >input
This will create a file, named input which will have numbers from 1 to 10000, which is of course the o/p of testGenerator.c.
Then, if your program, in which you want input is compiled into a2, as I can see, you can run it as:
./a2 <input >output
and you will get the required output in the file output. If you don't write >output here, you will see o/p on terminal.
Also, when you are running this script, you are actually running the a2 10000 times and giving a number as command line argument, which is very different from taking input from stdin which of course, scanf does.
for i in {1..1000}; do echo $i | ./a2; done

Pass parameter from shell script to c program

gcc (GCC) 4.7.2
GNU bash, version 4.2.37
Hello,
I have the following bash script that I want to pass string parameter to my c program. I tried using pipes but failed.
The c program will need to get an input from the shell script. I am not sure to to read in an input from a shell script.
My bash script is below.
#!/usr/bash
# About on any errors
set -e
RUN_WITH_VALGRIND=""
if [ "$1" == "valgrind" ]; then
RUN_WITH_VALGRIND="valgrind"
echo "START TESTING WITH VALGRIND"
fi
$RUN_WITH_VALGRIND ./c_program
echo "Hello" | ./c_program
And my sample c program is here:
char str_input[16];
printf("Get input: ");
scanf("%s", str_input);
printf("Input [ %s ]\n", str_input);
I am trying to get the scanf to read the input from the shell script.
Many thanks for any advice,
echo "Hello" | $RUN_WITH_VALGRIND ./c_program, it's that simple.
But in your script, c_program will run twice as you re-call it after the run_with_valgrind call (I don't know if its intend or not)
I like to use 'here docs' for that:
$RUN_WITH_VALGRIND ./c_program <<EOF
first
second
EOF
cf. http://tldp.org/LDP/abs/html/here-docs.html

How to extract the system command output into a variable inside an awk program?

The output of the system command that I invoke from my AWK script is being displayed on the terminal. I do not want the shell to display the output of the system command but rather want this output to be read into a variable of the script (the output of the system command is a one-word string either "yes" or "no"). This variable then needs to be compared with a string in the script (if its "yes" or "no") and the output from the command finally needs to be saved into a new file. How do I do this in AWK?
"command" | getline var
That will execute command and put the output, one line at a time, into the variable var. e.g.
$ awk 'BEGIN{while("seq 3" | getline x) print "output: " x}'
output: 1
output: 2
output: 3
And it will persist between calls too:
$ seq 3 | awk '{"seq 4 7" | getline x; print x}'
4
5
6
Note the 7 is not printed because there isn't a fourth line of input.
The post here shows using |&, which doesn't appear in the man page, and I'm not sure what the difference is. I thought it might capture stderr too, but it's not doing that in my tests.

Shell script to test command line program

I have a program written in C that operates similar to the below output.
WELCOME TO PROGRAM.
Hit 1 to do task 1.
Hit 2 to do task 2.
Hit q to quit.
What i need is a bash shell script that start the program, then enters 1, 2 and q into the program so i can test all the functionality in one command.
I would assume it to look similar to the following
#!/bin/bash
./start kernel
1
2
q
You can use a "here document" . The syntax looks like this:
./start kernel <<EOF
1
2
q
EOF
"EOF" can be whatever unique word you want, as long as it isn't something you'll actually need in the input.
Typically you use expect for testing these types of applications.
You can save your input in a text file - input.txt and execute your program this way: ./program < input.txt
I do this:
#! /bin/bash
printf "1\n2\nq\n" | ./start kernel
You can think of shell scripts as what they are... just each line being executed in an (albeit new) shell.
A simple way to do this sort of input is, assuming [your program] accepts stdin, is:
#!/bin/bash
echo "1" | [your program] > [logfile1]
echo "2" | [your program] > [logfile2]
echo "q" | [your program] > [logfileq]

Resources