简体   繁体   English

从 Unix Shell 脚本运行 Perl 脚本

[英]Run Perl Script From Unix Shell Script

Hi I have a Unix Shell Script call Food.sh ;嗨,我有一个 Unix Shell 脚本调用 Food.sh ; I have a Perl Script call Track.pl.我有一个 Perl 脚本调用 Track.pl。 Is there a way where I can put Track.pl's code in to Food.sh code and still have it work ?有没有一种方法可以将 Track.pl 的代码放入 Food.sh 代码中并且仍然有效? Track.pl requires one arugement to label a name of a folder. Track.pl 需要一个参数来标记文件夹的名称。

Basically it will run like this.基本上它会像这样运行。

Unix Shell Script codes RUN

step into 

Perl Script codes RUN
Put in name of folder for Perl script
Rest of script runs

exit out.

You have a few options.你有几个选择。

  1. You can pass the code to Perl using -e / -E .您可以使用-e / -E将代码传递给 Perl。

     ... perl -e' print "Hello, World!\\n"; ' ...

    Con: Escaping can be a pain.缺点:逃避可能是一种痛苦。 [1] [1]

  2. You can pass the code via STDIN.您可以通过 STDIN 传递代码。

     ... perl <<'END_OF_PERL' print "Hello, World!\\n"; END_OF_PERL ...

    Con: The script can't use STDIN.缺点:脚本不能使用 STDIN。

  3. You can create a virtual file.您可以创建一个虚拟文件。

     ... perl <( cat <<'END_OF_PERL' print "Hello, World!\\n"; END_OF_PERL ) ...

    Con: Wordy.缺点:罗嗦。

  4. You can take advantage of perl 's -x option.您可以利用perl-x选项。

     ... perl -x -- "$0" ... exit #!perl print "Hello, World!\\n";

    Con: Can only have one snippet.缺点:只能有一个片段。

    $0 is the path to the shell script being executed. $0是正在执行的 shell 脚本的路径。 It's passed to perl as the program to run.它作为要运行的程序传递给perl The -x tells Perl to start executing at the #!perl line rather than the first line. -x告诉 Perl 在#!perl行而不是第一行开始执行。

Ref: perlrun参考: perlrun


  1. Instances of ' in the program needs to escaped using '\\'' .程序中'实例需要使用'\\''进行转义。

    You could also rewrite the program to avoid using ' .您还可以重写程序以避免使用' For example, you could use double-quoted string literals instead of single-quoted string literals.例如,您可以使用双引号字符串文字而不是单引号字符串文字。 Or replace the delimiter of single-quoted string literals (eg q{...} instead of '...' ).或者替换单引号字符串文字的分隔符(例如q{...}而不是'...' )。 As for single-quoted inside of double-quoted and regex literals, these can be replaced with \\x27 , which you might find nicer than '\\'' .至于双引号和正则表达式文字中的单引号,这些可以替换为\\x27 ,您可能会发现它比'\\''更好。

(I'm assuming your goal is just to have all of the code in a single file so that you don't have multiple files to install) (我假设您的目标只是将所有代码都放在一个文件中,这样您就不必安装多个文件)

Sure, there's a way to do this, but it's cumbersome.当然,有一种方法可以做到这一点,但很麻烦。 You might want to consider converting the shell script entirely to Perl (or the Perl script entirely to shell).您可能需要考虑将 shell 脚本完全转换为 Perl(或将 Perl 脚本完全转换为 shell)。

So ... A way to do this might be:所以......这样做的一种方法可能是:

#!/bin/sh

echo "shell"

perl -E '
say "perl with arg=$ARGV[0]"
' fred

echo "shell again"

Of course, you'd have to be careful with your quotes within the Perl part of the program.当然,您必须小心程序的 Perl 部分中的引号。

You might also be able to use a heredoc for the Perl part to avoid quoting issues, but I'm not sure about that.您也可以对 Perl 部分使用 heredoc 以避免引用问题,但我不确定这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM