如何使用 Perl 获得 bash 内置命令

发布于 2024-11-09 10:48:38 字数 78 浏览 0 评论 0原文

我想知道是否有办法使用 perl 脚本获取 Linux 命令。我说的是诸如cd ls llclear cp之类的命令

I was wondering if there is a way to get Linux commands with a perl script. I am talking about commands such as cd ls ll clear cp

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

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

发布评论

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

评论(4

命比纸薄 2024-11-16 10:48:38

您可以通过多种方式执行系统命令,其中一些方式比其他方式更好。

  • 使用 system();,打印命令的输出,但不将输出返回到 Perl 脚本。
  • 使用反引号 (``),不会打印任何内容,而是将输出返回到 Perl 脚本。使用实际反引号的另一种方法是使用 qx(); 函数,该函数更易于阅读并完成相同的任务。
  • 使用exec();,它与system();做同样的事情,但根本不返回到Perl脚本,除非命令不存在或失败。
  • 使用 open();,它允许您将输入从脚本传输到命令,或者将命令的输出读入脚本。

值得一提的是,您列出的系统命令(如 cp 和 ls )使用 Perl 本身的内置函数可以更好地完成。任何系统调用都是一个缓慢的过程,因此当所需结果很简单(例如复制文件)时,请使用本机函数。

一些示例:

# Prints the output. Don't do this.
system("ls");

# Saves the output to a variable. Don't do this.
$lsResults = `ls`;

# Something like this is more useful.
system("imgcvt", "-f", "sgi", "-t", "tiff", "Image.sgi", "NewImage.tiff");

此页面更详细地解释了进行系统调用的不同方式。

You can execute system commands in a variety of ways, some better than others.

  • Using system();, which prints the output of the command, but does not return the output to the Perl script.
  • Using backticks (``), which don't print anything, but return the output to the Perl script. An alternative to using actual backticks is to use the qx(); function, which is easier to read and accomplishes the same thing.
  • Using exec();, which does the same thing as system();, but does not return to the Perl script at all, unless the command doesn't exist or fails.
  • Using open();, which allows you to either pipe input from your script to the command, or read the output of the command into your script.

It's important to mention that the system commands that you listed, like cp and ls are much better done using built-in functions in Perl itself. Any system call is a slow process, so use native functions when the desired result is something simple, like copying a file.

Some examples:

# Prints the output. Don't do this.
system("ls");

# Saves the output to a variable. Don't do this.
$lsResults = `ls`;

# Something like this is more useful.
system("imgcvt", "-f", "sgi", "-t", "tiff", "Image.sgi", "NewImage.tiff");

This page explains in a bit more detail the different ways that you can make system calls.

酒几许 2024-11-16 10:48:38

正如 voithos 所说,您可以使用 system() 或反引号。但是,请考虑到不建议这样做,并且例如,cd 不起作用(实际上不会更改目录)。请注意,这些命令是在新 shell 中执行的,不会影响正在运行的 perl 脚本。

我不会依赖这些命令并尝试在 Perl 中实现您的脚本(如果您决定使用 Perl,无论如何)。事实上,Perl 最初被设计为系统管理员的 sh 和其他 UNIX shell 的强大替代品。

You can, as voithos says, using either system() or backticks. However, take into account that this is not recommended, and that, for instance, cd won't work (won't actually change the directory). Note that those commands are executed in a new shell, and won't affect the running perl script.

I would not rely on those commands and try to implement your script in Perl (if you're decided to use Perl, anyway). In fact, Perl was designed at first to be a powerful substitute for sh and other UNIX shells for sysadmins.

箜明 2024-11-16 10:48:38

您可以将命令放在反引号中
`命令`

you can surround the command in back ticks
`command`

旧人 2024-11-16 10:48:38

问题是 perl 试图执行 bash 内置命令(即 source,...),就好像它们是真实文件一样,但 perl 无法找到它们,因为它们不存在。答案是告诉 perl 明确执行什么。对于像 source 这样的 bash 内置命令,执行以下操作即可正常工作。

my $XYZZY=`bash -c "source SOME-FILE; DO_SOMETHING_ELSE; ..."`;

对于 cd 的情况,执行如下操作。

my $LOCATION=`bash -c "cd /etc/init.d; pwd"`;

The problem is perl is trying to execute the bash builtin (i.e. source, ...) as if they were real files, but perl can't find them as they don't exist. The answer is to tell perl what to execute explicitly. In the case of bash builtins like source, do the following and it works just fine.

my $XYZZY=`bash -c "source SOME-FILE; DO_SOMETHING_ELSE; ..."`;

of for the case of cd do something like the following.

my $LOCATION=`bash -c "cd /etc/init.d; pwd"`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文