简体   繁体   English

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

[英]How to get bash built in commands using Perl

I was wondering if there is a way to get Linux commands with a perl script.我想知道是否有办法使用 perl 脚本获取 Linux 命令。 I am talking about commands such as cd ls ll clear cp我说的是诸如cd ls ll clear cp之类的命令

You can execute system commands in a variety of ways, some better than others.您可以通过多种方式执行系统命令,其中一些方式比其他方式更好。

  • Using system();使用system(); , which prints the output of the command, but does not return the output to the Perl script. ,它会打印命令的 output,但不会将 output 返回到 Perl 脚本。
  • Using backticks (``), which don't print anything, but return the output to the Perl script.使用反引号 (``),它不打印任何内容,但将 output 返回到 Perl 脚本。 An alternative to using actual backticks is to use the qx();使用实际反引号的另一种方法是使用qx(); function, which is easier to read and accomplishes the same thing. function,它更容易阅读并完成同样的事情。
  • Using exec();使用exec(); , which does the same thing as system(); ,与system(); , but does not return to the Perl script at all, unless the command doesn't exist or fails. ,但根本不会返回到 Perl 脚本,除非该命令不存在或失败。
  • Using open();使用open(); , which allows you to either pipe input from your script to the command, or read the output of the command into your script. ,它允许您将 pipe 从脚本输入到命令,或者将命令的 output 读取到脚本中。

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.重要的是要提到您列出的系统命令,如cpls使用 Perl 本身的内置函数可以更好地完成。 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.本页更详细地解释了您可以进行系统调用的不同方式。

You can, as voithos says, using either system() or backticks.正如 voithos 所说,您可以使用system()或反引号。 However, take into account that this is not recommended, and that, for instance, cd won't work (won't actually change the directory).但是,请考虑到不建议这样做,例如, cd将不起作用(实际上不会更改目录)。 Note that those commands are executed in a new shell, and won't affect the running perl script.请注意,这些命令在的 shell 中执行,不会影响正在运行的 perl 脚本。

I would not rely on those commands and try to implement your script in Perl (if you're decided to use Perl, anyway).我不会依赖这些命令并尝试在 Perl 中实现您的脚本(如果您决定使用 Perl,无论如何)。 In fact, Perl was designed at first to be a powerful substitute for sh and other UNIX shells for sysadmins.事实上,Perl 最初被设计为系统管理员的 sh 和其他 UNIX shell 的强大替代品。

The problem is perl is trying to execute the bash builtin (ie source , ...) as if they were real files, but perl can't find them as they don't exist.问题是 perl 正在尝试执行 bash 内置(即source ,...),就好像它们是真实文件一样,但是 perl 找不到它们,因为它们不存在。 The answer is to tell perl what to execute explicitly.答案是告诉 perl 明确执行什么。 In the case of bash builtins like source , do the following and it works just fine.对于source之类的内置函数,请执行以下操作,它工作得很好。

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

of for the case of cd do something like the following.对于cd的情况,请执行以下操作。

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

you can surround the command in back ticks你可以用反引号包围命令
`command` `命令`

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

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