Ruby 交互式 shell 命令 [英] Ruby interactive shell commands

查看:55
本文介绍了Ruby 交互式 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个从 shell 执行命令的 ruby​​ shell 脚本.当 shell 命令需要用户的回答时会发生什么?

I want to make a ruby shell script that executes command from shell. What happens when the shell command needs the answer from a user?

例如,创建一个新的播放框架应用程序:

For example, creating a new play framework application:

play new calimero

~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.7.2, http://www.playframework.org
~

~ The new application will be created in /home/anquegi/src/11paths/buildsdeb/toni_build/devops-tools/packaging/packwithruby/calimero
~ What is the application name? [calimero] ~   <=== here asks to the user

使用 ruby​​ shell 脚本,我想向用户提示并获取它,ruby 脚本等待它,我按 Enter,它就可以工作了.

With ruby shell script, I want to prompt this to the user and get it, the ruby script waits for that, I press enter, and it works.

value = %x(play new calimero)

等到用户点击介绍.我应该如何管理这个?我应该在提出问题之前打印问题吗?

waits until the user clicks intro. How I should manage this? Should I print the question before it is made?

推荐答案

原文:https://stackoverflow.com/a/6488335/2724079

这也可以通过 IO.expect 来实现

This can also be accomplished with IO.expect

require 'pty'
require 'expect'

PTY.spawn("play new calimero") do |reader, writer|
  reader.expect(/What is the application name/)
  writer.puts("\n")
end

这会等待生成的进程显示应用程序名称是什么",并且当它看到打印定义的字符串(新行)时.

This waits for the spawned process to display "What is the application name" and when it sees that prints a defined string (new line).

这篇关于Ruby 交互式 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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