00 - introduction

来自「O Reilly Ruby Cookbook source code」· 代码 · 共 45 行

TXT
45
字号
def run(command, input='')  IO.popen(command, 'r+') do |io|    io.puts input    io.close_write    return io.read  endendrun 'wc -w', 'How many words are in this string?'         # => "7\n"#---print 'Enter your password for sudo: 'sudo_password = gets.chomprun('sudo apachectl graceful', user_password)#---require 'expect'require 'pty'print 'Old password:'old_pwd = gets.chompprint "\nNew password:"new_pwd = gets.chompPTY.spawn('passwd') do |read,write,pid|  write.sync = true  $expect_verbose = false    # If 30 seconds pass and the expected text is not found, the  # response object will be nil.  read.expect("(current) UNIX password:", 30) do |response|    write.print old_pwd + "\n" if response  end    # You can use regular expressions instead of strings. The code block  # will give you the regex matches.  read.expect(/UNIX password: /, 2) do |response, *matches|    write.print new_pwd + "\n" if response  end  # The default value for the timeout is 9999999 seconds  read.expect("Retype new UNIX password:") do |response|    write.puts new_pwd + "\n" if response  endend#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?