Controlling a program from shell script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Controlling a program from shell script
# 1  
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  
You should show your work and what scripts you have written, what you have tried and why.
# 3  
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  
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 06:57 PM.. Reason: corrected ICODE tag
This User Gave Thanks to bakunin For This Post:
# 5  
Wow, I didn't know about coproc.
That seems to make my life a lot simpler indeed! Thanks.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #88
Difficulty: Easy
PowerShell makes use of pipes.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Regarding Python Program with Shell Script

Hi All, I have written a shell script which is using the expect method, it is working fine in terminal window, and then I have executed via python script its also working fine in command prompt functioning properly, I used subprocess.Popen method to execute the shell script file, its working... (0 Replies)
Discussion started by: janaefx
0 Replies

2. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Call C Program From Shell Script

Hi, Could anybody please let me know how to call a C_Program from shell script. I know through command "system" we can call shell script from C program. Awaiting response. Thanks and regards, Chanakya M (4 Replies)
Discussion started by: Chanakya.m
4 Replies

6. 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

7. 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

8. 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

9. 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

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

Featured Tech Videos