Pass string arg from shell to perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass string arg from shell to perl
# 1  
Old 04-12-2011
Pass string arg from shell to perl

My shell script generates a bunch of lines of text and passes this text as an argument to a perl script.

I'm able to do this, but for some reason newlines don't get recognized in the perl script and so the script just prints actual '\n' instead of carriage returning, otherwise everything gets passed just fine.

Not even sure what's causing this, but I'll settle for a solution.
# 2  
Old 04-13-2011
Show us how are you passing that text to the Perl script.
# 3  
Old 04-13-2011
Code:
if [ -n $notify ]
then
        ~/notify "$results"
fi

$results contains a bunch of lines delimited by newline characters

The perl script accesses it through $ARGV[0]

Basically the shell script accumulates a bunch of results, ships them off to the "mailman" perl script which in turn e-mails the results to a user.

Last edited by Franklin52; 04-13-2011 at 03:18 PM.. Reason: Please use code tags
# 4  
Old 04-13-2011
Putting it in quotes causes it to pass it as one single argument. It's not perl that has to understand the newlines, it's the shell.

Keeping everything as one giant string like that is subject to limits, you know. On many systems you're not guaranteed anything more than 4 kilobytes of data in commandline arguments! If your data will ever be more than a page or so, you should be feeding the data into stdin instead of into arguments, as well as not storing it all in one giant variable inbetween, to prevent it being truncated when the data gets large. If you post your shell code we can show you how to adapt it, and on the perl side, you'd do something like:

Code:
while($line=<STDIN>)
{
        chomp($line);
        # do stuff with $line
}

and on the shell side, something like
Code:
while condition
do
       echo something
done | ~/notify

On some shells, you can kludge your existing solution like

Code:
OLDIFS="${IFS}"
IFS=$'\n'
~/notify $results
IFS="${OLDIFS}"

which will cause arguments to temporarily be split on newlines but not on other whitespace, splitting $results into arguments the way you want.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass a variable string in To_Date Oracle function in shell script

Hello, I am trying to execute an SQL query from shell script. A part of script is something like this: fromDate=`echo $(date +"%F%T") | sed "s/-//g" | sed "s/://g"` $ORACLE_HOME/sqlplus -s /nolog <<EOD1 connect $COSDBUID/$COSDBPWD@$COSDBSID spool... (4 Replies)
Discussion started by: sanketpatel.86
4 Replies

2. Shell Programming and Scripting

Pass command line arg to sql file

Hi all, How to pass the command line argument to a sql file Script: #!/bin/ksh if ] ; then test.sql fi My Sql Informix DB: echo "select * from table where col1 = 2234 and col2 = '$3'"|dbaccess ddname But im getting `:' unexpected error (5 Replies)
Discussion started by: Roozo
5 Replies

3. Shell Programming and Scripting

Not able to pass string with spaces in shell

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' >... (15 Replies)
Discussion started by: patilrakesh1984
15 Replies

4. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

5. Shell Programming and Scripting

Pass a string with spaces to a shell script

I'm a beginner and wasn't able to google my problem... I would like to pass a string with spaces to a shell script. my test_shell: #!/bin/sh -vx ####################################################### # generate_documentation (c) Ch.Affolter Nov. 2009 Version 1.0 #... (3 Replies)
Discussion started by: vivelafete
3 Replies

6. Shell Programming and Scripting

pass perl variables to shell script

I have a perl script that opens a text file containing numbers on each line: for example: 755993 755994 755995 755996 755997 755998 The perl script takes these numbers and store them as an array @raw_data, where I can access individual numbers by using $raw_data for the value 755993.... (2 Replies)
Discussion started by: xchen89x
2 Replies

7. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

8. Programming

warning: int format,pid_t arg (arg 2)

I try following code under Solaris10,like follows: int glob = 6; int main(void) { int var; pid_t pid; var = 88; printf("before vfork\n"); if ((pid = vfork()) < 0) { err_sys("vfork error"); } else if (pid == 0) { glob++; var++; _exit(0); } ... (1 Reply)
Discussion started by: konvalo
1 Replies

9. Shell Programming and Scripting

How to pass an array as arg to a script..

Hi, Please guide to pass an array as a arg to a script... for example, I have a script small.sh to find the small no of given arg as below... #! /bin/sh # this script is for finding the small number set -A arr_no_updates small=$1 i=1 for arr in $@ do if (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

Pass string from shell script to java

Hi, I,m writing a program in shell script and currently this script is calling a java program. I have a problem to pass string variable from my shell script to the java program. I don't know on how to pass it and how the java program can call what I have pass from the shell script. This is... (3 Replies)
Discussion started by: badbunny9316
3 Replies
Login or Register to Ask a Question