Register
It is currently Fri Sep 19, 2014 9:50 am

Rediecting or piping text


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Mon Mar 22, 2010 7:23 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
I have this small bash script and it works..When I execute it, it will prompt the user for some text when the user enters some text via the keyboard and hits return the script echoes it back..Simple. Now the question. Is it possible to redirect or pipe text into this script and have it echo the text back..I tried redirection, I tried piping, I tried sending the data to the terminal device directly...it all failed...Is it possible?

Code:
#! /bin/sh

mytty=$(tty)

echo -n "enter a string of text->"
if read mystr<$mytty
then
   echo "text read->'$mystr'"
   exit 0
fi

echo "failed to read->'$mystr'"
exit 1


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 1:20 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
simply
Code:
#!/bin/bash

while read line; do echo "$line"; done

then
Code:
echo "foo
bar
baz" | ./your_script
or
Code:
./your_script < <(echo "foo
bar
baz")


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 3:54 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
If you need more flexibility than standard pipes will give then try expect;
http://www.manpagez.com/man/1/expect/


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 5:12 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
Watael I think you missed the point. The redirection or piping has to work with the original script that I posted.


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 5:15 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
DarthWavy wrote:
If you need more flexibility than standard pipes will give then try expect;
http://www.manpagez.com/man/1/expect/


I'll look into it..Thanks


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 8:34 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Quote:
Watael I think you missed the point. The redirection or piping has to work with the original script that I posted.


The tty stuff? I'm not sure what you are asking. If you just use the first script that Watael provided, it will handle anything coming from stdin, whether it's from a redirect, pipe, or user on a terminal. What exactly is missing here?

- thobbs


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 9:31 pm   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
thobbs wrote:
Quote:
Watael I think you missed the point. The redirection or piping has to work with the original script that I posted.


The tty stuff? I'm not sure what you are asking. If you just use the first script that Watael provided, it will handle anything coming from stdin, whether it's from a redirect, pipe, or user on a terminal. What exactly is missing here?

- thobbs


All I want to know is, is redirection or piping possible with the script I posted? If its possible then how?


Top
 Profile  
 PostPosted: Tue Mar 23, 2010 11:30 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
No, I don't believe so. If you echo $mytty when you try to redirect or pipe in, you'll get "not a tty," as there is no terminal connected to stdin.

- thobbs


Top
 Profile  
 PostPosted: Wed Mar 24, 2010 5:58 am   

Joined: Tue Mar 02, 2010 9:55 pm
Posts: 24
Location: Canada
From the things that I tried I would have to agree with you thobbs...Everyone thanks for the replys


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP