简体   繁体   English

将变量从perl脚本发送到命令行

[英]send a variable from a perl script into a command line

I have a perl script that always returns yesterday's date and the date is stored on a variable called $yesterday 我有一个Perl脚本,该脚本始终返回昨天的日期,并且该日期存储在名为$yesterday的变量中

Now, I am running that perl script using cmd prompt. 现在,我使用cmd提示符运行该Perl脚本。 After it figures out what yesterday's date was I would like to pass $yesterday somehow into the cmd line so that it can open up a "$yesterday.txt" file. 在确定了昨天的日期之后,我想以某种方式将$ yesterday传递到cmd行中,以便它可以打开“ $ yesterday.txt”文件。 The txt files are already there and match the date format in the filename. txt文件已经存在,并且与文件名中的日期格式匹配。

Are you asking how to open a file? 您在问如何open文件吗?

my $file = "$yesterday.txt";
open my $fh, "<", $file or die "Cannot open $file for reading: $!";

See the documentation for more information on the open command. 有关open命令的更多信息,请参见文档

You can simply include the $variable in the string of the command or use string concatenation to put the static and variable parts together: 您可以简单地在命令的字符串中包含$ variable或使用字符串串联将静态和可变部分放在一起:

system("echo yesterday >$yesterday.txt");

or 要么

system("echo yesterday >" . $yesterday . ".txt");

The above calls a commandline program or command from perl and the command includes the value of your $yesterday variable. 上面的代码从perl调用命令行程序或命令,并且该命令包含$ yesterday变量的值。

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

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