LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-27-2009, 04:19 PM   #1
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
Bash script: Dealing with program input from a pipe


I have a script that would get run like this:

echo "This is a test" | ./testScript

I would like the script to take "This is a script" and output it to a file without screwing up standard input.

I've tried to "cat - > /tmp/somefile" but that screws up 'read' statements later on in the program.

Thoughts?

Thanks.
 
Old 04-27-2009, 05:42 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

I take it you need to do some interactive stuff later on in the script that gets 'read'? Unfortunately, you only get one std in stream to work with - anything else and you'll have to use files or FIFOs to shunt data about.

Can you elaborate a little on how the script works and what sort of data you're dealing with? e.g. if the 'This is a test' data is likely to be less than a few thousand characters, you could specify it as an argument to the script, rather than using std in, which would let you 'read' later on. If it's bigger, you could set up a FIFO and read from that rather than std in.

Tell us more, do.

Dave
 
Old 04-28-2009, 01:07 PM   #3
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks for your reply.

You're correct about it being an interactive script.
Here is some more info on the script:

It will be a script that gets run every time a user commits to our CVS repo (a cvs hook). The script will get the CVS log message on standard in. This is how CVS hooks work so the log message must come from std in.

The point of the script is to determine the number of words in the CVS log message (again, it gets passed on std in). If the log message has more than 500 words the user will be presented with a menu to basically verify if they actually want to commit with such a large log message.

It's a long story, but I have users that accidentally enter a log message and then manage to repeat it 10,000 times because they aren't that great with vi.

So, my plan is to take the log message from std in and store it to a /tmp/file, so I can run wc on it as well as have the option of displaying it again to the user.


More thoughts?

Thanks!
 
Old 04-28-2009, 01:31 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Cool. I didn't actually think this was do-able, but then I had a vague memory of a script someone wrote long ago with a weird exec statement in it and...
Code:
#!/bin/bash

cat - > /tmp/somefile
exec 0< /dev/tty
<interactive stuff>
This will only work if there's a controlling terminal (i.e. not from cron or at), but since it's interactive, I don't think that'll be an issue for you.

Dave

Last edited by ilikejam; 04-28-2009 at 01:34 PM. Reason: Use cat instead of read - echo in script
 
Old 04-28-2009, 03:28 PM   #5
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Neat. That appears to do what I want.

I'm just curious, will /dev/tty always work on a very busy server with multiple people logged into it? I guess I don't understand tty devices that well. I'm concerned if multiple people are logged in and doing commits or whatever, will /dev/tty work for all of them at the same time?

Thanks. Your idea is very helpful.
 
Old 04-28-2009, 03:41 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
/dev/tty isn't a normal device. Any process that does an open() on the device to read or write is actually diverted to the controlling tty of the process doing the open(), if that makes any sense. A bit of kernel (or libc maybe?) voodoo so that you don't have to run 'tty' to find out what the controlling tty is.

Open a couple of shells and do 'echo hello > /dev/tty' in each - the output will appear on the same terminal, but not anywhere else.

Basically, yes, it'll work fine, regardless of how many people are logged in and where.

Dave
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pipe as Bash function input eldorel Programming 4 03-05-2009 10:38 AM
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
Bash Scripting: Pipe input to script vs. $1 jhwilliams Linux - Software 3 12-21-2007 10:54 PM
Bash script and chroot with a pipe Luminoth Programming 6 09-19-2007 01:01 PM
Can't get user input when using a pipe to my C++ program ta0kira Programming 18 04-22-2006 12:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration