简体   繁体   中英

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

I'm calling ruby from a bash shell script, like

#!/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. Strangely, whatever I use p on does show up. How can I get this to work for print ?


Quick answer: use puts .

My guess is this has to do with STDOUT buffering. p flushes the buffer immediately, while print does not. Use puts instead, which also flushes, or you can set STDOUT to always flush globally with:

$stdout.sync = true

To flush on a case-by-case basis, you can always call flush yourself:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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