如何使用system()命令传递用户输入? [英] How do I use the system() command to pass in user input?

查看:65
本文介绍了如何使用system()命令传递用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 system(在此处插入shell命令")并传入脚本.但是,此脚本需要用户输入.我也想传递用户输入.我该怎么做?

I want to use system("insert shell command here") and pass in a script. This script however, requires user input. I want to pass in the user input as well. How do I do this?

我尝试过:

system('./script')
system('input1')
system('input2')

但是,Ruby等待直到完成第一个系统调用,然后才继续进行下一个系统调用.

However, Ruby waits till the first system call is done and then only proceeds to go to the next one.

是否可以这样做?我已经尝试过 system('./script','input1'),但这也不起作用. exec 也没有运气.

Is it possible to do this? I've tried system('./script', 'input1') but that didn't work either. No luck with exec either.

推荐答案

您应该查看

You should look into the Open3 library from the Ruby stdlib. It gives you more control over "shelling out".

output, status = Open3.capture2("./script", :stdin_data => "I am STDIN")

这篇关于如何使用system()命令传递用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆