A
A
Alexander Kurakin2016-03-03 12:00:39
linux
Alexander Kurakin, 2016-03-03 12:00:39

Why is the output truncated when redirecting to a file?

Linux Mint. The Ruby script outputs some information to stdout, the standard puts. But I cannot guarantee that there are no tricky characters there.
When I just run it, all the text is output to the console. When I do a redirect to a file ( > file), part of the text is cut off, not all the text is in the file, only a part (initial).
The script completes successfully. In the case of using a pipeline ( | wc), the same is true - there are as many lines as in file. Hooking stderr ( > file 2>&1) does not help.
At the same time, if you write directly to the file ( fputs) in the script, then everything is in order.

# process_direct.rb использует fputs
# process.rb использует puts
# других отличий нет

$ bundle exec ruby process_direct.rb && wc f_direct 
 3461  5858 87579 f_direct

$ bundle exec ruby process.rb 
# визуально текст весь - конец этого вывода совпадает с концом предыдущего

$ bundle exec ruby process.rb > f_1 && wc f_1
 3249  5531 82131 f_1

$ bundle exec ruby process.rb | wc
 3249  5531 82131

$ bundle exec ruby process.rb > f_12 2>&1 && wc f_12
 3249  5531 82131 f_12

The disk is not full. Tried both Bash and Sh.
How can this even be? Thank you.
PS : Investigation revealed the following minimal code that reproduces the problem:
gem install happymapper
require 'happymapper'

class IpMetaData
  include HappyMapper
end

resp = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<ip-answer>\n<ip value=\"5.61.17\"><inetnum>4.16.0 - 5.255.255</inetnum><country>RU</country><city>\xCC\xEE\xF1\xEA\xE2\xE0</city><region>\xCC\xEE\xF1\xEA\xE2\xE0</region><district>\xD6\xE5\xED\xF2\xF0\xE0\xEB\xFC\xED\xFB\xE9 \xF4\xE5\xE4\xE5\xF0\xE0\xEB\xFC\xED\xFB\xE9 \xEE\xEA\xF0\xF3\xE3</district><lat>55.755787</lat><lng>37.617634</lng></ip>\n</ip-answer>"
IpMetaData.parse(resp)

puts "SUCCESS"

I don't know what's in Happymapper...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Burov, 2016-03-03
@BuriK666

Maybe you have something going to stderr?
> file 2>&1

O
Oleg, 2016-03-03
@hobo-mts

Didn't ^D, which means the end of the file, accidentally get stuck in the output?

A
Alexey Shumkin, 2016-03-03
@ashumkin

Send output to hexdump -Cor sed -nel
Also try not puts, butp

A
Andrey, 2016-03-03
@VladimirAndreev

try >>file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question