Running command line commands within Ruby script

前端 未结 5 1495
情歌与酒
情歌与酒 2020-12-04 06:14

Is there a way to run command line commands through Ruby? I\'m trying to create a small little Ruby program that would dial out and receive/send through command line program

5条回答
  •  醉梦人生
    2020-12-04 06:44

    The Most Used method is Using Open3 here is my code edited version of the above code with some corrections:

    require 'open3'
    puts"Enter the command for execution"
    some_command=gets
    stdout,stderr,status = Open3.capture3(some_command)
    STDERR.puts stderr
    if status.success?
      puts stdout
    else
      STDERR.puts "ERRRR"
    end
    

提交回复
热议问题