从 Unix Shell 脚本运行 Perl 脚本 [英] Run Perl Script From Unix Shell Script

查看:85
本文介绍了从 Unix Shell 脚本运行 Perl 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Unix Shell 脚本调用 Food.sh ;我有一个 Perl 脚本调用 Track.pl.有没有一种方法可以将 Track.pl 的代码放入 Food.sh 代码中并且仍然有效?Track.pl 需要一个参数来标记文件夹的名称.

Hi I have a Unix Shell Script call Food.sh ; I have a Perl Script call 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 requires one arugement to label a name of a folder.

基本上它会像这样运行.

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.

推荐答案

您有几个选择.

  1. 您可以使用 -e/-E 将代码传递给 Perl.

  1. You can pass the code to Perl using -e/-E.

 ...
 perl -e'

    print "Hello, World!\n";

 '
 ...

缺点:逃避可能会很痛苦.[1]

Con: Escaping can be a pain.[1]

您可以通过 STDIN 传递代码.

You can pass the code via STDIN.

 ...
 perl <<'END_OF_PERL'

    print "Hello, World!\n";

 END_OF_PERL
 ...

缺点:脚本不能使用标准输入.

Con: The script can't use STDIN.

您可以创建虚拟文件.

 ...
 perl <(
 cat <<'END_OF_PERL'

    print "Hello, World!\n";

 END_OF_PERL
 )
 ...

缺点:罗嗦.

您可以利用 perl-x 选项.

You can take advantage of perl's -x option.

 ...
 perl -x -- "$0"
 ...
 exit

 #!perl
 print "Hello, World!\n";

缺点:只能有一个片段.

Con: Can only have one snippet.

$0 是正在执行的 shell 脚本的路径.它作为要运行的程序传递给 perl.-x 告诉 Perl 在 #!perl 行而不是第一行开始执行.

$0 is the path to the shell script being executed. It's passed to perl as the program to run. The -x tells Perl to start executing at the #!perl line rather than the first line.

参考:perlrun

  1. 程序中'的实例需要使用'\''进行转义.

  1. Instances of ' in the program needs to escaped using '\''.

您也可以重写程序以避免使用'.例如,您可以使用双引号字符串文字而不是单引号字符串文字.或者替换单引号字符串文字的分隔符(例如 q{...} 而不是 '...').至于双引号和正则表达式文本中的单引号,可以用 \x27 替换,你可能会发现它比 '\'' 更好.

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 (e.g. q{...} instead of '...'). As for single-quoted inside of double-quoted and regex literals, these can be replaced with \x27, which you might find nicer than '\''.

这篇关于从 Unix Shell 脚本运行 Perl 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆