How to call shell commands from Ruby

前端 未结 20 2044
旧时难觅i
旧时难觅i 2020-11-22 01:10

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?

20条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 01:47

    This is not really an answer but maybe someone will find it useful:

    When using TK GUI on Windows, and you need to call shell commands from rubyw, you will always have an annoying CMD window popping up for less then a second.

    To avoid this you can use:

    WIN32OLE.new('Shell.Application').ShellExecute('ipconfig > log.txt','','','open',0)
    

    or

    WIN32OLE.new('WScript.Shell').Run('ipconfig > log.txt',0,0)
    

    Both will store the ipconfig output inside log.txt, but no windows will come up.

    You will need to require 'win32ole' inside your script.

    system(), exec() and spawn() will all pop up that annoying window when using TK and rubyw.

提交回复
热议问题