Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can I embed Perl script inside Bash script?

1,059 views
Skip to first unread message

T

unread,
Jul 18, 2015, 7:14:51 PM7/18/15
to
Hi All,

Inside Bash script, I know I can do Perl one lines
and I call Perl scrips, but can I embed Perl code
inside a Bash script?

The idea is to eventually take over and replace the
Bash script with Perl script.

Many thanks,
-T

Rainer Weikusat

unread,
Jul 19, 2015, 6:34:06 AM7/19/15
to
T <T...@invalid.invalid> writes:
> Inside Bash script, I know I can do Perl one lines
> and I call Perl scrips, but can I embed Perl code
> inside a Bash script?

That's a bash question and the answer (also supported by Perl) is "Yes,
with here documents",

-----------
#!/bin/sh

PATH=/usr/local/bin:/bin:/usr/bin
export PATH

perl <<'EOF'
print("Gotta get out to this shell somehow!\n");
EOF

Henry Law

unread,
Jul 19, 2015, 6:36:52 AM7/19/15
to
On 19/07/15 00:14, T wrote:
> but can I embed Perl code
> inside a Bash script?

I played around with this and I think it's a bash question, not a Perl
one. You could put the whole of your Perl program into a here document,
but all the dollar signs and backslashes would need to be escaped; when
I tried this my brain went numb just working out how many escapes I
needed and where.

Why not simply write a Perl program, called "myprog.pl" for example, and
then a Bash wrapper called "myprog.sh", or even "myprog" which contained
simply the line "perl myprog.pl"? You could generate these wrappers
automatically, in fact. I do that in Windows occasionally, where
foo.cmd (or, these days, foo.bas maybe) calls foo.pl.

--

Henry Law Manchester, England

Jens Thoms Toerring

unread,
Jul 19, 2015, 10:01:06 AM7/19/15
to
Perhaps the question is more about using some features from
Perl from within a bash script, perhaps similar to something
like this:

#!/bin/bash
a="xyz"
a="`perl -e '($r=$ARGV[0])=~s/$ARGV[1]/$ARGV[2]/g; print $r' $a '[a-xz]' AA`"
echo $a

Butt-ugly, I agree;-) But could be used if one's really
desperate.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Rainer Weikusat

unread,
Jul 20, 2015, 5:11:24 AM7/20/15
to
Henry Law <ne...@lawshouse.org> writes:
> On 19/07/15 00:14, T wrote:
>> but can I embed Perl code
>> inside a Bash script?
>
> I played around with this and I think it's a bash question, not a Perl
> one. You could put the whole of your Perl program into a here
> document, but all the dollar signs and backslashes would need to be
> escaped;

The here-document syntax for Perl and the shell is essentially the same
and in both cases, the here-document delimiter can be quoted to suppress
any interpretation by the host language, eg, this code

-------
#!/bin/sh

PATH=/usr/local/bin:/bin:/usr/bin
export PATH

perl - "$@" <<'EOF'
print("no argument\n"), exit(0) unless @ARGV;

print("arguments\n", '-' x length('arguments'), "\n");
print(map { "\t$_.\t=>\t$ARGV[$_]\n" } 0 .. $#ARGV);
EOF
-------

is a Bourne-shell (not necessarily bash) script which invokes perl with
an embedded Perl-script to print a numbered list of the arguments passed
to the outer script.

T

unread,
Jul 22, 2015, 4:18:47 PM7/22/15
to
Hi Rainer,

Cool. I use to do this with "ftp" before I discovered
wget and curl.

Thank you!

-T

T

unread,
Jul 22, 2015, 4:21:37 PM7/22/15
to
This is what I call a one liner. I like using perl's substitute
command over "sed" as Perl allows for substitute pairs.

s{.*/}{} is much more intuititve than s/.\*\///

T

unread,
Jul 22, 2015, 4:39:46 PM7/22/15
to
On 07/19/2015 07:00 AM, Jens Thoms Toerring wrote:
Hi Jens,

I am thinking of creating a Perl script full of exported
libraries. The idea is to take over so many of the subroutines
and functions that were used in the bash script that eventually
I could just drop the bash script altogether.

But, my main sticking point is can I use "use" in a one
liner?

#!/bin/bash
$a="$(perl -e 'use MySubs::revisions; print "blah blah")

Can I get away with this?

-T





Jens Thoms Toerring

unread,
Jul 22, 2015, 4:44:30 PM7/22/15
to
> This is what I call a one liner. I like using perl's substitute
> command over "sed" as Perl allows for substitute pairs.

> s{.*/}{} is much more intuititve than s/.\*\///

Sorry, but when you use 'sed' in your bash script then you're
doing 'sed' one-liners - sed is a program of it's own and not
any part pf bash, and you only can invoke it from bash. So
there's no difference to a Perl one-liner, just the syntax of
what you pass to sed as the script is a bit different from
what you pass to Perl.

If the question is: can I somehow just have Perl's regex
machinery seemlessly become a part of bash, then, unfor-
tunately, the answer is no. All you can do is run Perl
scripts (of one or lots of lines) from within bash.

T

unread,
Jul 22, 2015, 4:52:35 PM7/22/15
to
On 07/22/2015 01:44 PM, Jens Thoms Toerring wrote:
> Sorry, but when you use 'sed' in your bash script

bash uses everyone else to do the heavy lifting.
It is easy to forget that you are making constant
external calls to other programs.

This is one of the reasons why I want to migrate to perl.

shar...@hotmail.com

unread,
Jul 23, 2015, 2:02:13 AM7/23/15
to
On Thursday, 23 July 2015 02:09:46 UTC+5:30, T wrote:
[snip]


> But, my main sticking point is can I use "use" in a one
> liner?

But of course. The '-M' option is just what the doctor ordered.


> #!/bin/bash
> $a="$(perl -e 'use MySubs::revisions; print "blah blah")
>
> Can I get away with this?
>
> -T

You bet. Apart from a single-quote closing that you forgot to apply above.


#!/bin/bash -u
a=$(perl -MMySubs::Revisions -le 'print "bash perl"')

And in case you wanted to access the symbols (functions or vars) from the module
that are not exported by default, meaning this piece of line

use File::Basename qw( basename dirname );

will become

perl -MFile::Basename="basename,dirname" -le 'print "perl bash"'

Note: no spaces around the , or = accepted. IOW, you aren't allowed to do this

perl -MFile::Basename=" basename, dirname "


& in case you are a stickler for spaces, perl allows you to be that as well:

perl '-MFile::Basename qw/ basename dirname /'

You don't have to take my word for it ;-)

another -M command to actually verifes what
perl is doing under it's hood, so to say:

perl -MO=Deparse '-MFile::Basename qw/ basename dirname /' -e 'print dirname $ARGV[0]' /tmp/foo/perl_in_bash.sh

will return:
use File::Basename ('basename', 'dirname');
print dirname($ARGV[0]);
-e syntax OK


Do a "perldoc perlrun" on your xterm, to whet your appetite; it gives you
all the nooks & crannies of executing a perl via the command line, & that too from the horse's mouth.

T

unread,
Jul 23, 2015, 3:00:27 AM7/23/15
to
Hi Sharma,

Wow! Thank was an extremely in depth answer. I am writing
this down. Thank you!

The game plane it the etch away at the bash script until
all that is left is the "contract" portion (what drives
the subs). Then the "contract" portion will migrate
into the Perl script.

-T

T

unread,
Jul 23, 2015, 3:40:13 AM7/23/15
to
I am thinking of keeping the calling routines (the "contract")
separate (two files) from the Subs and just doing imports. That
way I can pound the subs with one liners until I get the
result I like. Much easier to troubleshoot.
0 new messages