hisham hm

Fun hack to redirect stdout and stderr in order

Prologue

This is anecdote about roundabout ways to get stuff done. Pierre mentioned in the comments below that a proper way to solve this is to use unbuffer (though it does _not_ produce the exact same order as the terminal!). But if you want to read the improper way to do this, read on! :)

The story

Due to buffering, the terminal messes with the order of stdout and stderr of a program when redirecting to a file or another program. It prints the outputs of both descriptors in correct order relative to each other when printing straight to the terminal:

] ./my_program
stdout line 1
stdout line 2
stderr line 1
stdout line 3
stderr line 2
stderr line 3

This doesn’t change the order:

] ./my_program 2>&1
stdout line 1
stdout line 2
stderr line 1
stdout line 3
stderr line 2
stderr line 3

but it changes the order when saving to a file or redirecting to any program:

] ./my_program 2>&1 | cat
stderr line 1
stderr line 2
stderr line 3
stdout line 1
stdout line 2
stdout line 3

This behavior is the same in three shells I tested (bash, zsh, dash).

A weird “solution”

I wanted to save the log while preserving the order of events. So I ended up with this evil hack:

] strace -ewrite -o trace.txt -s 2048 ./my_program; sed 's,^[^"]*"\(.*\)"[^"]*$,\1,g;s,\\n,,g;' trace.txt > mytrace.txt
] cat mytrace.txt
stdout line 1
stdout line 2
stderr line 1
stdout line 3
stderr line 2
stderr line 3
+++ exited with 0 +++

It turns out that strace does log each write in the correct order, so I’m catching the write syscall.

Note the limitations: it truncates lines to 2048 characters (good enough for my logs) and I was simply cutting off n and not cleaning up any other escape characters. But it worked well enough so I could read my ordered logs in a text editor!


On the word “latino”

One of my least-favorite American English words is “latino”, for two reasons:

First, a linguistic reason: because it’s not inflected when used. When you’re used to the fact that in Spanish and Portuguese “latino” refers only to men and “latina” only to women, hearing “latino woman” sounds really weird (weirder than, say, “handsome woman”). Even weirder “latino women”, mixing a Spanish/Portuguese word and English grammar. “Bonito girls”? :)

Second, a sociological reason: because using a foreign loanword reinforces the otherness. Nobody calls the Italian community in America “italiano”, although that’s their name in Italian. The alternative “Hispanic” is not ideal because it doesn’t really make sense when including Brazil, which was never a Spanish colony (plus, the colonial past is something most countries want to leave behind).

I can’t change the language by myself, so I just avoid the term and use more specific ones whenever possible (Colombians, Argentines, Brazilians, South Americans, Latin Americans when referring to people from the area in general, etc.)

After writing the above, I checked Wikipedia and it seems the communites in the US agree with me:

« In a recent study, most Spanish-speakers of Spanish or Hispanic American descent do not prefer the term “Hispanic” or “Latino” when it comes to describing their identity. Instead, they prefer to be identified by their country of origin. When asked if they have a preference for either being identified as “Hispanic” or “Latino,” the Pew study finds that “half (51%) say they have no preference for either term.”[43] A majority (51%) say they most often identify themselves by their family’s country of origin, while 24% say they prefer a pan-ethnic label such as Hispanic or Latino. Among those 24% who have a preference for a pan-ethnic label, “‘Hispanic’ is preferred over ‘Latino’ by more than a two-to-one margin—33% versus 14%.” Twenty-one percent prefer to be referred to simply as “Americans.” »

I think the awkwardness in the grammar from point one actually reinforces point two, because it strikes me as something that no Spanish or Portuguese native speaker would come up with by themselves. So it sounds tacked upon.

Don’t get me wrong, I fully identify as a Brazilian, a South American and a Latin American — travellling abroad helps a lot to widen your cultural identity! — and I have no problem when people wear the term “latino” proudly, but I always pay close attention to the power of language and how it represents and propagates ideas.


Quarta-feira, 31 de agosto

Andando pela Marquês de Abrantes, eu paro porque vejo uma multidão, a uma boa distância, olhando para um ponto. Algo aconteceu.

Assalto? Briga? Melhor ficar longe. Eu vou perguntar pro jornaleiro e reparo que ele está rindo da cena.

“Um cara aí desandou a gritar, aí atravessou a rua e foi se estranhar com outro que gritou de volta, contra a Dilma, a favor da Dilma, uma confusão!”

Eu tento identificar a confusão de longe pra ver se está tranquilo passar, mas já parece dispersa.

O jornaleiro diz: “ó, la vem ele!”

Um homem branco de terno e gravata, de uns 40 anos, com um sorriso de ponta a ponta, levando de mãos dadas a esposa, quieta. Ele segue balançando a outra mão pro alto e gritando pela rua “Quem votou em Dilma, votou em Temer!”, repetindo sem parar.


Automatizando a espera na fila do ingresso do GnR

Entrei no site da venda de ingressos pro Guns n’ Roses às 10 da noite (22:02 talvez?).

“Sua posição na fila é 25504”.

Fui até uma meia-noite e pouco olhando o número cair lentamente, até que o Rakan me lembrou que eu sei programar, aí eu escrevi esse script abaixo pra “tocar um alarme” quando a fila chegasse perto no final. Botei o computador do lado da cama e fui dormir. 7 da manhã do dia seguinte, acordo ao som de Nightrain ao vivo. Acompanhei os momentos finais da fila e, enfim, consegui comprar o meu ingresso!


Uma descoberta interessante sobre Veja X14 Tira Limo

Hoje eu me senti espertinho no supermercado (ou, segundo os ensinamentos do Livro de Detsch, eu senti que fui burro da vez passada).

Fui comprar Veja X14 Tira Limo no supermercado. Tava tipo 14 reais, frasco de 500ml. Aí resolvi ler no verso a composição pra ver se tinha algum outro produto equivalente.

“Ingrediente ativo: hipoclorito de sódio”

Também conhecido como: água sanitária. Comprei uma garrafa de 2 litros por 4 reais, voltei pra casa, e enchi o tubinho de X14 que eu tinha, com o conveniente bico aplicador. Funcionou tal-e-qual!