使用 eval 在 shell 脚本内执行 perl 代码

发布于 2024-12-09 08:27:32 字数 439 浏览 1 评论 0原文

我遇到了下面的例子。我尝试用谷歌搜索但找不到太多所以我在这里发布这个问题。

  1. 这样执行perl脚本有什么好处呢?
  2. 执行完 perl 代码后,如何才能使 shell 脚本像“正常”shell 脚本一样工作?

这是代码:

#!/bin/ksh
#! -*- perl -*-
eval 'exec $PERLLOCATION/bin/perl -x $0 ${1+"$@"} ;'
if 0;

print "hello world\n";
# how can I make it behave like a "normal" shell script from this point onwards? What needs to be done?
# echo "hello world" ### this results in error

I came across the following example. I tried to google but could not find much so I'm posting this question here.

  1. What is the benefit of executing the perl script like this?
  2. How can we make the shell script work like a "normal" shell script once we are through executing the perl code?

Here's the code:

#!/bin/ksh
#! -*- perl -*-
eval 'exec $PERLLOCATION/bin/perl -x $0 ${1+"$@"} ;'
if 0;

print "hello world\n";
# how can I make it behave like a "normal" shell script from this point onwards? What needs to be done?
# echo "hello world" ### this results in error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薯片软お妹 2024-12-16 08:27:32

此习惯用法在 perlrun 文档中进行了描述。
-x 开关扫描整个文件并忽略以 #! 开头且还包含单词 perl 的第一行之前出现的任何内容。
这意味着无论您使用 Perl 还是 shell 命令(sh/bash/ksh/ 等)调用脚本,您的系统都将使用 Perl 解释器运行脚本。
也就是说,

$ perl this_script

$ sh this_script

都将使用 perl 运行脚本。

为了解决你的第二个问题,这个习惯用法与将 shell 脚本和 Perl 脚本组合在同一个文件中几乎没有任何关系。有几种不同的方法可以解决该问题,但也许最易读的方法是编写 shell 脚本,但使用 shell 的定界符表示法来调用 perl 代码。

#!/bin/bash
# this is a bash script, but there is some Perl in here too

echo this line is printed from the shell
echo now let\'s run some Perl

perl <<EOF
# this is now perl script until we get to the EOF
print "This line is printed from Perl\n";
EOF

echo now this is from the shell script again

This idiom is described in the perlrun documentation.
The -x switch scans the whole file and ignores anything that appears before the first line that begins with #! and also contains the word perl.
It means that your system will run the script with the Perl interpreter whether you invoke the script with perl or with a shell command (sh/bash/ksh/etc.)
That is,

$ perl this_script

and

$ sh this_script

will both run the script with perl.

To address your second question, this idiom has just about nothing to do with combining shell script and Perl script in the same file. There are a few different ways to approach that problem, but maybe the most readable way is to write in shell script, but use the shell's heredoc notation to invoke perl code.

#!/bin/bash
# this is a bash script, but there is some Perl in here too

echo this line is printed from the shell
echo now let\'s run some Perl

perl <<EOF
# this is now perl script until we get to the EOF
print "This line is printed from Perl\n";
EOF

echo now this is from the shell script again
鼻尖触碰 2024-12-16 08:27:32

1. 如果您以通常的方式启动 Perl 脚本:

#!/usr/bin/perl
print "hello world\n";

只有当 Perl 解释器实际安装在 /usr/bin< 下时,#! 行才会起作用。 /代码>。您显示的 perl/ksh 双语脚本是一个棘手的问题,即使 perl 安装在其他地方,也无法使脚本正常工作。有关更多信息,请参阅例如这个

2.你不能。当 shell 进程遇到 exec 命令时,它会终止并将控制权交给 perl。 (从技术上讲,它执行 perl 代替 shell,而不创建新进程。)在此之后运行更多 shell 命令的唯一方法是启动一个新的 shell。

1. If you start a Perl script in the usual way:

#!/usr/bin/perl
print "hello world\n";

the #! line will only work if the Perl interpreter is actually installed under /usr/bin. The perl/ksh bilingual script you show is a tricky kluge to make the script work even if perl is installed somewhere else. For more information, see e.g. this.

2. You can't. When the shell process encounters the exec command, it terminates and hands control over to perl. (Technically, it executes perl in place of the shell, without creating a new process.) The only way to run more shell commands after that would be to launch a new shell.

⊕婉儿 2024-12-16 08:27:32

它比已经发布的内容简单得多。

#!$PERLLOCATION/bin/perl

不起作用,因为 shebang (#!) 行是由内核(而不是 shell)解释的,并且内核不进行变量插值。

该代码调用 ksh 来扩展环境变量并启动指定的 Perl 安装。

It's way simpler than what's already been posted.

#!$PERLLOCATION/bin/perl

doesn't work because the shebang (#!) line is interpreted by the kernel (not the shell), and the kernel doesn't do variable interpolation.

The code invokes ksh to expand the environment variable and to launch the specified installation of Perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文