LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-08-2011, 10:31 AM   #1
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Rep: Reputation: 2
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 ..
 
Old 02-09-2011, 06:20 PM   #2
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu
Posts: 1,210

Rep: Reputation: 238Reputation: 238Reputation: 238
On my system:
Code:
~$ which perl
/usr/bin/perl
perhaps you too need to be looking in /usr/bin rather than in /bin
 
Old 02-09-2011, 06:39 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by cheltz View Post
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
 
Old 02-09-2011, 10:29 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 02-09-2011, 10:54 PM   #5
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Posts: 119

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


Reply


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
invoking windows command inside a shell script nano2 Programming 6 09-09-2009 03:46 AM
linux command inside perl script von_vea Programming 5 09-08-2009 01:54 PM
mv inside ksh script problem katkota Linux - Newbie 2 01-14-2009 08:01 AM
perl inside a shell script baidym Linux - Newbie 2 05-05-2008 06:42 PM
calling a c++ binary inside a perl script Blue_muppet Programming 3 08-28-2004 11:31 PM


All times are GMT -5. The time now is 07:02 PM.

Main Menu
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