Shell script to test command line program - c

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]

Related

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 run program that asks for input

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

How to get input file name from Unix terminal in C?

My program gets executed like:
$./sort 1 < test.txt
sort is the program name
1 is the argument (argv[1])
and test.txt is the file I am inputting from
Is it possible to extract the name file from this? if so how?
The problem is I already wrote my whole program as if I could extract the name from the input line, so I need to be able to pass it into arguments.
Any help is appreciated,
Thanks!
You can't. The shell opens (open(2)) that file and sets up the redirect (most likely using dup2).
The only possible way would be for the shell to explicitly export the information in an environment variable that you could read via getenv.
But it doesn't always make sense. For example, what file name would you expect from
$ echo "This is the end" | ./sort 1
Though this can't be done portably, it's possible on Linux by calling readlink on /proc/self/fd/0 (or /proc/some_pid/fd/0).
eg, running:
echo $(readlink /proc/self/fd/0 < /dev/null)
outputs:
/dev/null
No you can't: the shell sends the content of test.txt to the standard input of your program.
Look at this:
sort << _EOF
3
1
2
_EOF
The < > | operators are processed by the shell, they alter standard input,output,error of the programs in the cmd line.
If you happen to run Solaris, you could parse pfiles output to get the file associated, if any, with stdin.
$ /usr/bin/sleep 3600 < /tmp/foo &
[1] 8430
$ pfiles 8430
8430: /usr/bin/sleep 3600
Current rlimit: 65536 file descriptors
0: S_IFREG mode:0600 dev:299,2 ino:36867886 uid:12345 gid:67890 size=123
O_RDONLY|O_LARGEFILE
/tmp/foo
1: S_IFCHR mode:0600 dev:295,0 ino:12569206 uid:12345 gid:67890 rdev:24,2
...
On most Unix platforms, you will also get the same information from lsof -p if this freeware is installed.

Resources