简体   繁体   English

如何获取ruby`print`来呼应bash脚本?

[英]How to I get ruby `print` to echo to calling bash script?

I'm calling ruby from a bash shell script, like 我从bash shell脚本中调用ruby,例如

#!/bin/bash

# lots of stuff

ruby script.rb

# more stuff

I'd like the things I print in the ruby script to show up in stdout for the shell script, but they don't. 我希望将在ruby脚本中print的内容显示在shell脚本的stdout中,但事实并非如此。 Strangely, whatever I use p on does show up. 奇怪的是,无论我用p没有显示出来。 How can I get this to work for print ? 我怎样才能将其用于print


Quick answer: use puts . 快速答案:使用puts

My guess is this has to do with STDOUT buffering. 我的猜测是这与STDOUT缓冲有关。 p flushes the buffer immediately, while print does not. p立即刷新缓冲区,而print不刷新。 Use puts instead, which also flushes, or you can set STDOUT to always flush globally with: 请改用puts ,它也会刷新,也可以将STDOUT设置为始终使用以下命令全局刷新:

$stdout.sync = true

To flush on a case-by-case basis, you can always call flush yourself: 要根据情况进行flush ,您始终可以自己调用flush

print ...
print ...
print ...
$stdout.flush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM