You are not logged in.
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
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
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
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
It reminds me of a pipe http://www.linuxjournal.com/content/usi … fifos-bash
Offline
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