2

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

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Typically you use expect for testing these types of applications.

1 Comment

The previously mentioned solutions appear to work in some cases, but yours seems to be the best solution. Thanks for the help byron :)
2

You can save your input in a text file - input.txt and execute your program this way: ./program < input.txt

1 Comment

I call this method the "Poor Man's automation", and I use it way more often than I'd like to admit. Zero error handling, but it usually gets the job done.
1

I do this:

#! /bin/bash

printf "1\n2\nq\n" | ./start kernel

Comments

0

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]

2 Comments

Forgive me if I have misunderstood; upon my original reading I thought you wanted to test these functionalities one at a time (what this code does), but it seems unclear now that I have read the other answers provided.
@whoever_voted_down... Since I'm new here could you please give me some tips on why you did that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.