LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-15-2012, 02:43 AM   #1
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 453

Rep: Reputation: 46
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?
Code:
#!/bin/sh
#dove-deliver.sh
FROOM=$0
TOO=$1  
base=$( /usr/lib/dovecot/deliver -f $FROOM -d $TOO < /some/path/to/email )
echo " $? "
The command looks like ./dove-deliver.sh some-guy@linuxquestions.org duder@mydomain.net
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 12:18 PM.
 
Old 01-15-2012, 04: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: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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, 10:56 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
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, 12:22 PM   #4
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 453

Original Poster
Rep: Reputation: 46
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 12:24 PM.
 
  


Reply

Tags
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 05:11 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 10:59 AM
Printing "s in Perl resetreset Programming 2 12-05-2008 03:19 PM
perl printing pantera Programming 1 06-02-2004 03:07 PM


All times are GMT -5. The time now is 08:39 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration