LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   invoking perl inside a ksh script (https://www.linuxquestions.org/questions/linux-newbie-8/invoking-perl-inside-a-ksh-script-861430/)

cheltz 02-08-2011 10:31 AM

invoking perl inside a ksh script
 
Can anyone help with this?
Trying to invoke perl inside ksh script.

#!/usr/bin/ksh
tm=72392

/bin/perl -e
$epochseconds="$tm";
($second, $minute, $hour) = gmtime($epochseconds);
print sprintf("%02d:%02d:%02d\n", $hour, $minute, $second);


Thanks in advance for your help ..

RockDoctor 02-09-2011 06:20 PM

On my system:
Code:

~$ which perl
/usr/bin/perl

perhaps you too need to be looking in /usr/bin rather than in /bin

Tinkster 02-09-2011 06:39 PM

Quote:

Originally Posted by cheltz (Post 4252058)
Can anyone help with this?
Trying to invoke perl inside ksh script.

#!/usr/bin/ksh
tm=72392

/bin/perl -e
$epochseconds="$tm";
($second, $minute, $hour) = gmtime($epochseconds);
print sprintf("%02d:%02d:%02d\n", $hour, $minute, $second);


Thanks in advance for your help ..


You're not saying in which way this fails, but one problem
is that the '' that normally wrap a '-e' command are missing.

Code:

#!/usr/bin/ksh
tm=72392

/bin/perl -e
'$epochseconds="$tm";
($second, $minute, $hour) = gmtime($epochseconds);
print sprintf("%02d:%02d:%02d\n", $hour, $minute, $second);'

Note: I never tried a perl command in that way within a script.

Cheers,
Tink

makyo 02-09-2011 10:29 PM

Hi.

Similarly:
Code:

#!/usr/bin/env bash

# @(#) s3        Demonstrate perl script embedded in shell script.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

perl -e '
print " Hello, world from perl in a continued line.\n";
'

perl  <<'EOF'
print " Hello, world from perl in a here document.\n";
EOF

# Be careful placing additional shell code here.

exit 0

producing:
Code:

./s3
 Hello, world from perl in a continued line.
 Hello, world from perl in a here document.

Best wishes ... cheers, makyo

A.Thyssen 02-09-2011 10:54 PM

This is for awk (nawk) but equally valid for perl, sed, etc...

The normal way is to use Single Quotes....

nawk '# output to mailx commands!
/^[^ ]/ { recipent=$0; }
/^ / { print $0 > "|mailx -s \"This is the test\" " recipent; }
/^$/ { close( "|mailx -s \"This is the test\" " recipent );
' list.txt

Note the whole script is inside single quotes on the nawk command line!

ASIDE: old versions of awk must have something on the first line thus the
addition of the # comment to keep it happy! Perl needs no such comment but
does require a -e option to execute a command line argument.

To insert a external shell variable into the script you need to close
the single quotes, output variable and re-open the single quotes. Also
the variable sould be in double quotes so as to prevent any insertion of
space characters, and depending on situation double quotes inside the
awk script too.

Example inserting a $prefix shell variable into a awk string.
' ...
{ print "'"$prefix"'" $0; }
... '

Also to insert a single quote into the script you have to also exit the
wrapping single quotes and supply it outside those quotes
' ...
{ print "I just can'\''t do that!"; }
... '

CAUTION: Watch for single quotes inside any COMMENTS which is in the script!
Comments are within the single quotes so are also scanned for those quotes.


I have used that technique to wrapper very large perl scripts inside even larger shell scripts.


All times are GMT -5. The time now is 05:55 AM.