perl inside bash: How to call perl on a script saved in a string

后端 未结 1 1562
孤街浪徒
孤街浪徒 2021-01-22 06:52

I need to execute the same perl script, multiple times, on different files.

To ease the process, I am trying to save the perl script as a bash string, and call perl over

相关标签:
1条回答
  • 2021-01-22 07:36

    You simply have too many quotes in your string $S:

    #!/bin/sh
    
    # works
    perl -e 'print 1;'
    
    # also works
    S='print 1;'    
    perl -e "$S"
    

    I have also added some double quotes around "$S", which prevents problems with word splitting.

    Another option is to use the -x switch to Perl:

    #!/bin/sh
    
    perl -x "$0"
    
    echo <<EOF >/dev/null
    #!/usr/bin/env perl
    my $a=5;
    print "$a\n";
    __END__
    EOF
    
    echo 'something else'
    

    $0 is the name of the current script, so Perl looks for the first line starting with #! and containing perl and interprets everything up to __END__ as a Perl script. The echo >/dev/null prevents the Perl script from being interpreted by the shell.

    0 讨论(0)
提交回复
热议问题