You are not logged in.

#1 2011-06-18 21:14:04

cmays90
Member
Registered: 2009-12-18
Posts: 31

[Solved] Controlling I/O to a Bash Subsell

Ok.  This one has me stumped, and it is probably because I do not know what to search for on Google...

Anyways, I have a program that is similar in nature (a lot more complex in practice, but the basic concept is the same) to the script below:

#!/bin/bash

while true; do 
        echo -n "What's your name? "
        read var1
        echo "hello $var1"
done

Now if I want to, in one line add a name, that seems simple to me:

echo "Chris" | ./name.sh

But what if I didn't know the name I wanted to pipe to name.sh just yet, but I want to start up name.sh anyway, and wait for the names to be asked for?

So I would run a script like:

#!/bin/bash
./name.sh
while [! -f /tmp/foo.txt]; do
    sleep 0.1
done
cat /tmp/foo.txt | name.sh

Obviously that won't work, but is there a way that I could make it work?

Thanks for any help.

Last edited by cmays90 (2011-06-19 04:29:49)

Offline

#2 2011-06-18 21:43:09

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Controlling I/O to a Bash Subsell

With that 'while true; do' loop you end up flooding your terminal with

What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello 
What's your name? hello

if you do

echo "Chris" | ./name.sh

Offline

#3 2011-06-18 22:06:45

cmays90
Member
Registered: 2009-12-18
Posts: 31

Re: [Solved] Controlling I/O to a Bash Subsell

That's just a very simplified script.  The program I have is quite a bit more complex...  I am just looking for the basic interface.  Perhaps this would be more appropriate:

#!/bin/bash

while true; do 
        echo -n "What's your name? "
        read var1
        if [ -n $var1 ]
        then
             exit
        fi
        echo "hello $var1"
done

Last edited by cmays90 (2011-06-18 22:09:50)

Offline

#4 2011-06-18 22:27:09

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [Solved] Controlling I/O to a Bash Subsell

If the idea is that you're piping input to a running process, then you want to use bash 4's coproc keyword. Otherwise, if its acceptable to start a new process for each input, I'm not sure what the above script doesn't accomplish (other than not actually piping the read var to the script).

Offline

#5 2011-06-18 22:32:36

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [Solved] Controlling I/O to a Bash Subsell

Offline

#6 2011-06-18 22:46:47

cmays90
Member
Registered: 2009-12-18
Posts: 31

Re: [Solved] Controlling I/O to a Bash Subsell

Both coproc and named pipes look like they will both do what I want.  Thanks falcon and karol!

I forsee some research to see what I actually want to use.


EDIT: Did some research on both of them, and within 30 minutes of real work, I managed to get coproc to do exactly what I wanted.  Linux always seems to have an easy answer to whatever I want to do.  Amazing really.

Thanks again.

Last edited by cmays90 (2011-06-19 04:29:19)

Offline

Board footer

Powered by FluxBB