在Perl脚本中使用Bash环境变量? [英] Using Bash environment variables from within a Perl script?

查看:199
本文介绍了在Perl脚本中使用Bash环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Perl程序中运行Bash命令. 但是,Perl似乎使我的Bash $ PWD环境变量与Perl变量混淆了.

I am trying to run a Bash command from within my Perl program. However Perl seems to be confusing my Bash $PWD environment variable as a Perl variable.

我如何才能将其全部读取为字符串?

How can I make it just read it all as a string?

这就是我要运行的

my $path = /first/path;
`ln -s $path $PWD/second/path`

这些反引号在Bash中运行第二行.使用System()会产生相同的问题.

Those backticks runs the second line in Bash. Using System() produces the same problem.

有什么想法吗?

推荐答案

这里有两个查询,关于使用Bash变量和运行外部命令.

There are two queries here, on use of Bash variables and on running external commands.

Perl中有 %ENV哈希,其中包含环境变量

There is the %ENV hash in Perl, with environment variables

perl -wE'say $ENV{PWD}'

但是,通常最好在脚本中使用等效项,因为对于脚本而言,事物可能具有微妙的含义,或者随着脚本的运行而发生变化.

However, you are often better off getting the equivalent within the script, as things may have a subtly different meaning for the script or change as the script runs.

更重要的是,使用shell命令会使您面临引号,shell注入和解释的各种潜在问题.例如,您显示的命令很危险,如 Charles Duffy 注释中所述.原则上,最好使用Perl的丰富功能.参见示例

More importantly, using shell commands exposes you to all kinds of potential problems with quoting, shell injection, and interpretation. For instance, the command you show is dangerous, as outlined in Charles Duffy comment. It is in principle better to use Perl's rich functionality. See for example

在Perl而非内置 库/函数[重复]

Using system commands in Perl instead of built in libraries/functions [duplicate]

详尽而详尽地说明优点.

for a sober, and detailed, account of advantages.

如果确实需要运行外部命令,则最好完全避免使用Shell,例如,使用

In case you do need to run external commands, it is best to avoid the shell altogether, for example by using the multi-argument form of system. If you need the output of the command as well there are various modules in Perl that provide that. See links below.

如果您还需要使用Shell的功能,那么可以使用

If you also need to use the shell's capabilities, instead of quoting everything just right in order for the shell to receive what it needs better use a ready tool like String::ShellQuote.

一些例子:

  • Perl尊重<"作为常规字符而不是输出重定向

    带有多个参数输出到文件.

    请注意, qx运算符(反引号)使用/bin/sh,它可能降级为Bash,也可能不会降级为Bash.因此,如果您想要Bash,则需要system('/bin/bash', '-c', @cmd).请参阅带有示例的链接.

    Note that qx operator (backticks) uses /bin/sh, which may or may not get relegated to Bash. So if you want Bash you'll need system('/bin/bash', '-c', @cmd). See the links with examples.

    这里是与问题背后的目标有关的完整示例.

    Here is a full example related to the objective behind the question.

    根据程序的启动方式,程序的工作目录可能与预期的不同.首先,它在chdir之后更改.我不知道您使用PWD的确切意图,但是在Perl中有核心 Cwd :: cwd FindBin $RealBin,用于当前工作目录和脚本所在的目录(通常是不同的东西).

    Your program's working directory may be other than expected depending on how it's started. For one, it changes after chdir. I don't know your exact intent with PWD, but in Perl there are core Cwd::cwd and FindBin with $RealBin, for the current working directory and for the directory where the script resides (generally different things).

    要创建指向$path的符号链接,其相对路径应位于当前工作目录之后

    To create a symbolic link to $path, with the relative path following the current working directory

    use warnings;
    use strict;
    use Cwd qw(cwd);
    
    my $cwd = cwd;
    
    my $path = '/first/path';
    
    symlink($path, "$cwd/second/path") or die "Can't make a symlink: $!";
    

    如果该路径是脚本的位置,请使用FindBin中的$RealBin而不是cwd.

    If the path is meant to be the script's location use $RealBin from FindBin instead of cwd.

    请注意,使用符号链接,您不能传递目录而不是链接名称.请参阅此页面.

    Note that with symlink you cannot pass a directory instead of a link name. See this page.

    这篇关于在Perl脚本中使用Bash环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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