Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-15-2012, 01:43 AM   #1
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 538

Rep: Reputation: 71
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.

Last edited by mpapet; 01-15-2012 at 11:18 AM.
Old 01-15-2012, 03:28 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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".
Old 01-15-2012, 09:56 AM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Originally Posted by mpapet View Post
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.
Old 01-15-2012, 11:22 AM   #4
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 538

Original Poster
Rep: Reputation: 71
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.

Last edited by mpapet; 01-15-2012 at 11:24 AM.


bash, bash scripting, perl

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
Printing "s in Perl resetreset Programming 2 12-05-2008 02:19 PM
perl printing pantera Programming 1 06-02-2004 02:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 PM.

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