-   Programming (
-   -   Newbie: Printing from Perl into Bash? (

mpapet 01-15-2012 02:43 AM

Newbie: Printing from Perl into Bash?
Question #1: I need to send a stream of text that includes a variety of end of line characters and probably include special characters that might trip up Bash when I least expect it. The string goes FROM Perl to Bash.

This request is further complicated by the scenario. The Perl (Qpsmtpd) runs as a very low privilege user and Qpsmtpd complains if I try to use system() or back ticks. Various Perl IO modules did not work either.

I understand passing a string from Perl to Bash is not optimal, but is it possible?

Question #2: Once the output passes to the bash script, is there an elegant way to pass options as well as the text stream?

base=$( /usr/lib/dovecot/deliver -f $FROOM -d $TOO < /some/path/to/email )
echo " $? "
The command looks like ./

Dovecot's deliver return code is returned with that script. But, I'm trying to avoid temporarily writing the email to file and then sending the path to the Bash script. I was hoping to deliver in one step from QpSmtpd.

There are probably obvious answers. It's just not clear to me.

David the H. 01-15-2012 04:28 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

The devil is in the details, and those are not clear to me. Can you explain exactly what you are trying to do, and give us some examples of exactly the kind of output we have to parse? What are the "special characters" that are involved, and what constitutes a line/field break in the stream? What kind of things does the bash script need to do with the input it gets?

Bash variables can store any character except null (so it can be used as a field delimiter, but is otherwise mostly impossible to deal with). Other than that, it's really just a trick of parsing the input. Assuming the output of the perl script is piped directly into the bash script, then it's just a matter of setting up the read command properly.

As for the second question, I'm not sure I understand your description there either, or what the posted script has to do with it. Are the "options" part of the same perl data stream? If so, then they must be parsed out of it as they are received. If not, then you may be able to just directly hand them to it on the command line. Or perhaps a separate file descriptor or fifo could be used.

BTW, be aware that "#!/bin/sh" runs scripts in posix-compliant mode, and most shell-specific features become either illegal or undefined. To get the full compliment of features, you have to specify the exact shell to use, "#!/bin/bash".

theNbomr 01-15-2012 10:56 AM


Originally Posted by mpapet (Post 4574891)
I understand passing a string from Perl to Bash is not optimal, but is it possible?

Passing data between processes can be done in a limited number of ways, and depends to some extent upon the relationship between the process. If one is the parent of the the other, then the parent may pass data as arguments to the child process. Also, the child inherits a copy of the parent's environment. This is exactly what happens when a process is launched on a shell commandline. In that scenario, the child process cannot return any data, except its exit status which the parent can read when the child terminates.
It the processes are fully independent, then there are Interprocess Communications (IPC) mechanisms to allow process to exchange data. A very good primer on this subject is Beej's Guide to IPC. It sounds like you are trying to use e-mail to exchange data between processes, and I guess that could work, although I think it would be considered a bit clumsy.

--- rod.

mpapet 01-15-2012 12:22 PM

Thanks for the feedback.

The string of text is an entire email, so there are line feeds and all manner of characters inside the email.

I think I'll just write the email to file and be done.

All times are GMT -5. The time now is 01:49 PM.