Making a system call that returns the stdout output as a string

前端 未结 27 920
误落风尘
误落风尘 2020-11-27 05:13

Perl and PHP do this with backticks. For example,

$output = `ls`;

Returns a directory listing. A similar function, system(\"foo\")

相关标签:
27条回答
  • 2020-11-27 05:55

    Ruby: either backticks or the '%x' builtin syntax.

    puts `ls`;
    puts %x{ls};
    
    0 讨论(0)
  • 2020-11-27 05:55

    An alternative method in perl

    $output = qx/ls/;
    

    This had the advantage that you can choose your delimiters, making it possible to use ` in the command (though IMHO you should reconsider your design if you really need to do that). Another important advantage is that if you use single quotes as delimiter, variables will not be interpolated (a very useful)

    0 讨论(0)
  • 2020-11-27 05:56

    Lua:

        foo = io.popen("ls"):read("*a")
    
    0 讨论(0)
提交回复
热议问题