简体   繁体   English

如何在perl中调用unix命令

[英]how to invoke unix command in perl

i am trying to execute following unix command but its not getting executed 我正在尝试执行以下UNIX命令,但未执行

$array_of_tables= `dbsmp $srv_name`;
print "$array_of_tables\n";

please help me to find out list of tables in a data base through perl scripting. 请帮助我通过perl脚本找出数据库中的表列表。

Also i am trying to copy a file from a path to different path by using following command:- 我也试图通过使用以下命令将文件从路径复制到其他路径:

copy(`cd /osp/slee/service/$srv_name/bin/exec/script.txt`,`cd /osp/local/home/linus/amit/scripts`);

but getting an error:- 但出现错误:

Usage: copy(FROM, TO [, BUFFERSIZE])

please provide some solution Thanks 请提供一些解决方案,谢谢

Use doublequotes instead of back ticks. 使用双引号而不是反引号。

copy("/osp/slee/service/$srv_name/bin/exec/script.txt","/osp/local/home/linus/amit/scripts");

and remove the cd 并取出cd

In Perl, the preferable way to capture the output of a system (shell) command is the qx() operator. 在Perl中,捕获系统(shell)命令输出的首选方法是qx()运算符。 See http://perldoc.perl.org/perlop.html#Quote-Like-Operators . 请参阅http://perldoc.perl.org/perlop.html#Quote-Like-Operators

$array_of_tables = qx(dbsmp $srv_name);
print("$array_of_tables\n");

Actually, backticks should also work, so the problem must lie with your dbsmp command. 实际上,反引号也应该起作用,因此问题必须出在dbsmp命令上。 I'm not sure what that command is; 我不确定该命令是什么。 you'll have to provide more information about the utility and what error you're seeing. 您将必须提供有关实用程序以及所见错误的更多信息。

For comparison, I can retrieve the list of tables in my local postgres database as a pipe-separated table using this shell command: 为了进行比较,我可以使用以下shell命令以管道分隔表的形式检索本地postgres数据库中的表列表:

> psql -tAXq main postgres <<<\\d;

And this can be run from Perl as follows: 可以从Perl运行,如下所示:

> perl -e 'print(qx(psql -tAXq main postgres <<<\\\\d;));'

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

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