- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试执行命令,将数据提供给其标准输入,并从其标准输出中读取数据。我尝试过使用 Ruby 的 Open3#popen3 以及通过 MacRuby 公开的 NSTask。我正在编写的程序的源代码可以访问 here 。我在 Xcode 和 MacRuby 中执行此操作。
这里是一些选择代码:
入口点,只是让我可以轻松地在两种方法之间切换。
def do_gpg_cmd cmd
do_gpg_cmd_nstask cmd
end
Ruby 方式,使用 Open3#popen3。
def do_gpg_cmd_ruby cmd
gpg = "#{@gpg_path} --no-tty "
cmd_output = ''
logg "executing [#{cmd}]"
Dispatch::Queue.concurrent.async do
logg "new thread starting"
Open3.popen3(gpg + cmd) do |stdin, stdout, stderr|
stdin.write input_text
stdin.close
cmd_output = stdout.read
output_text cmd_output
stdout.close
logg stderr.read
stderr.close
end
end
return cmd_output
end
在这种方法中,应用程序会卡住(我通过单击应用程序中的“签名”按钮进行测试,该按钮运行 gpg --clearsign --local-user $key
)。
当我终止应用程序时,Xcode 在自动出现的线程诊断中显示此内容:
libsystem_kernel.dylib`__psynch_cvwait:
0x7fff84b390f0: movl $33554737, %eax
0x7fff84b390f5: movq %rcx, %r10
0x7fff84b390f8: syscall
0x7fff84b390fa: jae 0x7fff84b39101 ; __psynch_cvwait + 17 ; THIS LINE IS HIGHLIGHTED
0x7fff84b390fc: jmpq 0x7fff84b3a4d4 ; cerror_nocancel
0x7fff84b39101: ret
0x7fff84b39102: nop
0x7fff84b39103: nop
Cocoa 方式,使用 NSTask。
def do_gpg_cmd_nstask cmd
Dispatch::Queue.concurrent.async do
fcmd = "--no-tty " + cmd
task = NSTask.alloc.init
task.setLaunchPath(@gpg_path)
task.setArguments(fcmd.split(" ") << nil)
task.arguments.each {|a| puts "ARG: [#{a}]" }
inpipe = NSPipe.pipe
outpipe = NSPipe.pipe
errpipe = NSPipe.pipe
task.setStandardOutput(outpipe)
task.setStandardInput(inpipe)
task.setStandardError(errpipe)
output = outpipe.fileHandleForReading
errput = errpipe.fileHandleForReading
input = inpipe.fileHandleForWriting
task.launch
input.writeData input_text.dataUsingEncoding(NSUTF8StringEncoding)
input.closeFile
outdata = output.readDataToEndOfFile
errdata = errput.readDataToEndOfFile
output.closeFile
errput.closeFile
outstring = NSString.alloc.initWithData(outdata, encoding: NSUTF8StringEncoding)
errstring = NSString.alloc.initWithData(errdata, encoding: NSUTF8StringEncoding)
output_text outstring
logg errstring
end
end
当我运行此命令时,我在 Xcode 调试输出中收到此错误。显然,我自己将 ARG 部分输出为 super 愚蠢的日志记录。子进程没有执行。
ARG: [--no-tty]
ARG: [--clearsign]
ARG: [--local-user]
ARG: [0xC2808780]
ARG: []
2013-03-12 23:27:39.305 GPGBoard[84924:3503] -[NSNull fileSystemRepresentation]: unrecognized selector sent to instance 0x7fff75b05310
*** Dispatch block exited prematurely because of an uncaught exception:
/Users/colin/Library/Developer/Xcode/DerivedData/GPGBoard-bradukgmaegxvmbukhwehepzyxcv/Build/Products/Debug/GPGBoard.app/Contents/Resources/AppDelegate.rb:81:in `block': NSInvalidArgumentException: -[NSNull fileSystemRepresentation]: unrecognized selector sent to instance 0x7fff75b05310 (RuntimeError)
我怀疑这两种方法的问题是相互排斥的:Open3#popen3 问题可能与阻塞读取有关,而 NSTask 的问题与管道问题有关。
最佳答案
这段代码适用于我并打印出当前目录中的文件:
framework "Cocoa"
task = NSTask.new
task.launchPath = "/bin/ls"
task.arguments = ["-l", "-a"]
stdoutPipe = NSPipe.pipe
task.standardOutput = stdoutPipe
task.launch
data = stdoutPipe.fileHandleForReading.readDataToEndOfFile
puts NSString.alloc.initWithData data, :encoding => NSASCIIStringEncoding
现在如果我替换task.arguments = ["-l", "-a"]
与 task.arguments = "-l -a".split(" ") << nil
我收到以下错误:
macruby[86209:707] -[NSNull fileSystemRepresentation]: unrecognized selector sent to instance 0x7fff77d6f310
所以,我认为你的问题是task.setArguments(fcmd.split(" ") << nil)
。将其更改为 task.setArguments(fcmd.split(" "))
你不应该再得到NSNull
问题。
关于cocoa - 如何在 MacRuby 中执行命令、向其标准输入提供数据并从其标准输出读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376638/
我是一名优秀的程序员,十分优秀!