简体   繁体   English

如何在perl中存储模块可用命令的输出?

[英]How to store output of module avail command in perl?

#!/depot/local/bin/perl5.8.0
my @data = `module avail icwbev_plus `;
print "Data Array : @data \n " ;

my $data1 = `module avail icwbev_plus `;
print "Data $data1 \n" ;

my $data2 = system (" module avail icwbev_plus ");
print "S Data $data2 " 

Output : 输出:

Data Array :  
 Data  
S Data -1

I am not getting why it is not storing output to a variable. 我不明白为什么它不将输出存储到变量。 Please help me to solve this. 请帮我解决这个问题。 Thanks in advance. 提前致谢。

To quote from the documentation for system (Emphasis added): 引用system文档 (已添加强调):

The return value is the exit status of the program as returned by the wait call. 返回值是等待调用返回的程序的退出状态。 To get the actual exit value, shift right by eight (see below). 要获得实际的退出值,请向右移动八位(请参见下文)。 See also exec. 另请参阅exec。 This is not what you want to use to capture the output from a command ; 这不是您要用来捕获命令输出的内容 for that you should use merely backticks or qx//, as described in "`STRING`" in perlop. 为此,您应该只使用反引号或qx //,如perlop中的“ STRING”所述。 Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason). 返回值-1表示无法启动程序或系统调用wait(2)出错(原因是检查$!)。

That combined with the blank output of the other attempts suggests that this module command isn't present in your path when you try to execute it. 加上其他尝试的空白输出,表明在尝试执行该module命令时,该命令不存在于您的路径中。 (I suspect that if you followed best practices and included use warnings; you'd get one about using an undefined value when you try to print $data1 ) (我怀疑如果您遵循最佳实践并包含use warnings;当您尝试打印$data1use warnings;您会得到关于使用未定义值的信息)


Anyways, if this module command is present on the computer you're running your perl code on, try using the absolute path to it ( my $data1 = qx!/foo/bar/module avail icwbev_plus! ), or put the directory it's in in your path before running the script. 无论如何,如果正在运行perl代码的计算机上存在此module命令,请尝试使用其绝对路径( my $data1 = qx!/foo/bar/module avail icwbev_plus! ),或将目录放在在运行脚本之前先进入路径。

module outputs to stderr, not stdout, which is not captured by qx/backticks. module输出到stderr,而不是stdout,这不是qx / backticks捕获的。 You can try: 你可以试试:

`LMOD_REDIRECT=yes module avail ...`

See https://lmod.readthedocs.io/en/latest/040_FAQ.html 参见https://lmod.readthedocs.io/en/latest/040_FAQ.html

The module command is a shell alias or a function. module命令是外壳程序别名或函数。 Thus it cannot be called directly via a `` or a system call. 因此无法通过``或system调用直接调用它。

To get the output of an avail sub-command, you should call the modulecmd command which is called by the module shell alias/function. 要获得可用子命令的输出,应该调用由module外壳别名/函数调用的modulecmd命令。

To get the location of modulecmd on your system, type in a regular shell session type module which exposes the command called by the module shell alias/function. 要获取modulecmd在系统上的位置,请键入常规的shell会话type module ,该type module将公开由module shell别名/函数调用的命令。

The fully qualified path to the modulecmd command can then be used through a back-tick or a system call to get the result of an avail sub-command: 然后,可以通过反modulecmdsystem调用来使用modulecmd命令的标准路径,以获取avail子命令的结果:

To get the output of a module avail command (in terse format to simplify parsing): 要获取module avail命令的输出(简洁格式以简化解析):

#!/depot/local/bin/perl5.8.0
my $data1 = `/usr/share/Modules/libexec/modulecmd.tcl perl avail --terse icwbev_plus 2>&1`; 
print "Data $data1 \n"

Note the --terse format used to simplify result parsing. 请注意--terse格式,用于简化结果解析。 Also stderr is redirected to stdout to catch the actual output of the command (as modulecmd primarily uses stdout to output environment change commands). 同样,stderr也被重定向到stdout以捕获命令的实际输出(因为modulecmd主要使用stdout来输出环境更改命令)。

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

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