LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2004, 05:01 PM   #1
artur
Member
 
Registered: Apr 2002
Location: Illinois, US
Distribution: Red Hat, Fedora, Yellow Dog, Debian, FreeBSD, Embedix
Posts: 106

Rep: Reputation: 15
redirecting stdin in bash script


this is a silly question, but somehow I can't get this working:
in my /etc/aliases I have a line like this:
fax: "|/test/myscript"

That's supposed to pipe mail sent to account fax to a bash script for processing.

In /test/myscript I have :
Code:
#!/bin/bash

cd /tmp
myresult="/tmp/`/test/unpack.py`"
echo $myresult
The line beginning with "myresult" is supposed to set this variable to contain "/tmp/" plus whatever is being output by a python script in /test/unpack.py
That unpack.py is supposed to receive incoming message on it's stdin, process it, sotre some data in a file in /tmp and give that file's name on it's stdout to be put in "myresult"

I can't figure out how to feed my e-mail through the bash script into the python script. I've checked my python script by cat myemaildump | unpack.py, works fine.

I checked silly stuff like paths, permissions, disabled sendmail's smrsh functionality to allow insecure scripts ( the machine sits on private network, not reachable from outside). /var/log/maillog indicates that the message was sent sucessfully to "|/test/myscript", but python script never sees the data.

What am I doing wrong?
 
Old 01-27-2004, 05:53 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Since you are piping into the bash script you will need to use "read" to get the data from stdin then echo that into a pipe to your paython script.
 
Old 12-08-2011, 02:31 PM   #3
superjer
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 1
You can pipe your script's stdin to the command's stdin with <&0. Like this:
Code:
myresult="/tmp/`/test/unpack.py <&0`"
I know this is 8 years later but this page ranks highly for "redirect stdin script", etc.
 
1 members found this post helpful.
Old 12-09-2011, 06:07 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by superjer View Post
You can pipe your script's stdin to the command's stdin with <&0. Like this:
Code:
myresult="/tmp/`/test/unpack.py <&0`"
I know this is 8 years later but this page ranks highly for "redirect stdin script", etc.
The problem is, that after this long time it’s hard to tell what the real cause of the problem was and whether it was solved. Did the shell’s behavior changed over time? For bash version 3 it’s neither necessary to use read for the stdin to feed it to another command or sub-shell, nor to specify <&0 inside a command substitution. It’s mostly used to restore the original stdin after you changed it to something else.
Code:
$ cat demo.sh 
#!/bin/sh
RESULT=$(cat)
echo $RESULT
$ echo hello | ./demo.sh 
hello
No read here, no <&0. I use this in some wrappers to add some lines to an email like:
Code:
#!/bin/sh
(echo "*** BEGIN OF ERROR MESSAGE ***"; cat; echo "*** END OF ERROR MESSAGE ***") | mail reuti
 
  


Reply



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
redirecting /dev/ttyS0 -> stdin goestin Linux - Software 2 10-26-2011 11:34 AM
Redirecting output of background processes after they've been started (BASH) DaneM Programming 18 08-08-2011 09:03 PM
How to create a shell script that reads something from stdin? ricky_ds Programming 2 05-17-2005 05:06 AM
after redirecting stdin still need to read data from keyboard vrikers Programming 4 10-31-2004 06:11 AM
redirecting stdin and stdout with pipes: convincing apps they're for a terminal? prell Programming 1 09-02-2004 06:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 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