Controlling a program from shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Controlling a program from shell script
# 1  
Old 11-25-2018
Controlling a program from shell script

Hi all,

I am trying to write a shell script that starts a program, reads from its stdout and can write to its stdin.
I'm having trouble to get it to work, I tried using named pipes, and using exec and file descriptors.
Just couldn't get it to work, so I thought I'll post it here to see if someone is willing to help me.

The program is called primes, and it first asks for my name

Code:
>What's your name?
<test
>Hi, test
>Given 10 numbers, calculate sum of its prime factors!
>1 : 187
>Sum:
<answer1
>2 : 123
<answer2
...

Thanks!
# 2  
Old 11-25-2018
You should show your work and what scripts you have written, what you have tried and why.
# 3  
Old 11-25-2018
Sure, I can show, but I had many versions and just in general have no clue how to do this.

I'm currently trying to create a named pipe for the input, so that I can write to that in my script (> $pipe).
Then I'm running the primes program in the background, which gets the $pipe on stdin.
I did a tail -f there to keep it opened, otherwise the stream would close when I write to it with for instance echo "test name" > $pipe.
But, it doesn't work, and I'm also not even sure how to debug this.

Code:
#!/bin/bash

mkfifo /tmp/primes.input
tail -f /tmp/primes.input | /tmp/primes >&0 &

echo "test name" > /tmp/primes.input

while true
do
    if read line; then
        echo "read: $line"

        if [[ "$line" == *"1 : "* ]]; then
	    echo "Solve this one: $line"
	    echo "12345" > /tmp/primes.input
        fi
    fi
done

------ Post updated at 03:00 PM ------

Now using this version. I thought I should perhaps not background the program. But this also doesn't seem to work.
This version does not output anything when I start it ... (not seeing the "read: ..." and also not seeing any output of the program).

Code:
#!/bin/bash
PROGRAM=/tmp/primes

mkfifo $PROGRAM.stdin

tail -f $PROGRAM.stdin | $PROGRAM | while true
do
    if read line; then
        echo "read: $line"

	if [[ "$line" == *"What's your name?"* ]]; then
	    echo "test name" > $PROGRAM.stdin
        elif [[ "$line" == *"1 : "* ]]; then
	    echo "Solve this one: $line"
	    echo "12345" > $PROGRAM.stdin
        fi
    fi
done

# 4  
Old 11-25-2018
The first question i have is: how exactly is the program reading the input?

If this is done via the gets() the problem might be a missing EOL ("\n") or EOF character at the end of the input line. Some programs (i.e. passwd also clear their <stdin> to make sure that redirection does not take place. Your program might have a similar mechanism in place.

Other than that: you might want to explore the "coprocess-facility" of the Korn shell if you want to bidirectionally communicate with asynchronously running jobs. If i remember correctly some versions of bash have that too now, but i have no experience with this in bash. It seems to work slightly different there than in Korn shell.

I hope this helps.

bakunin

Last edited by RudiC; 11-25-2018 at 05:57 PM.. Reason: corrected ICODE tag
This User Gave Thanks to bakunin For This Post:
# 5  
Old 11-25-2018
Wow, I didn't know about coproc.
That seems to make my life a lot simpler indeed! Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing a program in shell script

I followed the directions here Free Twitter Source Code ? Twitter Database Server: Install and created a php script that enters twitter keyword searches into a MySQL DB. When you execute the files outlined in the above link, a script starts indefinitely. I've noticed that the scripts... (6 Replies)
Discussion started by: phpchick
6 Replies

2. Shell Programming and Scripting

shell script program

shell script in Unix/Linux to find the lines numbers of a text file are having word which is 5 to 10 characters long and having first letter as a capital letter. (3 Replies)
Discussion started by: usersnehal
3 Replies

3. Shell Programming and Scripting

C program to execute shell script

Hi, Can anyone pls give a sample to execute a shell script from C program Thanks (2 Replies)
Discussion started by: baigmd
2 Replies

4. Shell Programming and Scripting

program for multiplication in shell script

Hi, I wanted to write a schell program that fetches the values from a file and prints the output as its onerall multiplication. for example I have a file named abc. it has values 2, 3, 4 now my program should give me 2*3*4 ie 24. note:this file abc can have any numbers. so experts,... (9 Replies)
Discussion started by: sandeep.krish
9 Replies

5. UNIX for Dummies Questions & Answers

Controlling GUI from Shell Script

Hi Everybody, I'm trying to create a script that will launch firefox from a bash shell in ubuntu and perform certain tasks. Why does it have to be a script? Because this firefox must eventually run in a server with xvfb (no gnome, no kde), so I will not have access to it through the GUI. At... (2 Replies)
Discussion started by: rummage_bin
2 Replies

6. Shell Programming and Scripting

ksh script as a login shell return "no controlling terminal"

I have created a ksh shell script and used it as a login shell for a user. </etc/passwd> lramirev:x:111:200:Luis:/export/home/menush:/usr/local/menush/menush My shell script is like this: </usr/local/menush/menush> #!/bin/ksh # if ] then . $HOME/.profile fi ... (8 Replies)
Discussion started by: lramirev
8 Replies

7. Shell Programming and Scripting

Controlling GUI from Shell Script

Hi Everybody, I previously posted this under Unix for dummies and got no answers, so here goes again: I'm trying to create a script that will launch firefox from a bash shell in ubuntu and perform certain tasks. Why does it have to be a script? Because this firefox must eventually run in a... (0 Replies)
Discussion started by: rummage_bin
0 Replies

8. Shell Programming and Scripting

Need help on C-shell script program

#!/bin/csh # This program will add integers # # # add integer1 .. # # Check for argument if ($#argv == 0 ) then echo "usage: add integers" exit 1 else set input = $argv endif # set sum = 0 foreach var ( $input ) @sum = $sum + $input end # (1 Reply)
Discussion started by: haze21
1 Replies

9. Programming

How to include shell script in C program

hi I want to call a shell script in C program the script is : ssh -t user@remote sh /<remote>home/user/<file_name>.sh and other several commands C program : Call this script and the retrive the task that is been done in <file_name>.sh file can any one tell me how... (5 Replies)
Discussion started by: mridula
5 Replies

10. Filesystems, Disks and Memory

shell script program

shell script for sorting,searchingand insertion/deletion of elements in a list (1 Reply)
Discussion started by: jayaram_miryabb
1 Replies
Login or Register to Ask a Question