3

I wanted to log my program's (the python program boots up a headless embedded device connected to a serial port) output to a text file console_log.txt using a shell script, for which, I wrote the following script:

#!/bin/bash
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
gnome-terminal -x bash -c "cd ~/Scripts;pwd;script -c \"python test.py $1\" ~/Output/console_log_$current_time.txt;exec $SHELL"

The above writes unreadable (non ASCII) characters to the file along the lines of:

捓楲瑰猠慴瑲摥漠桔牵摳祡〠‶灁楲〲㜱〠㨳㔲㈺‸䵐䤠呓ഊ上⹯漠⁦慰慲敭整獲瀠潲楶敤⁤祢琠敨甠敳⁲›ㄠ਍਍敔瑳漠瑰潩湥整敲⁤›〠਍獕湩⁧敤慦汵⁴潣普杩牵瑡潩楦敬⸮⸮਍਍⨪⨪⨪⨪⨪桔⁥潣普杩牵瑡潩楦敬琠慨❴⁳潧湩⁧潴戠⁥獵摥椠⁳›㰠潮敮ാ猊慴瑲湩⁧慭湩猠牣灩൴⼊潨敭愯瑵浯瑡潩㑮愯瑵浯瑡潩⽮噄㥆弹畁潴慭楴湯਍栯浯⽥畡潴慭楴湯⼴畡潴慭楴湯䐯䙖㤹䅟瑵浯瑡潩⽮湉異⽴਍❛灳捥牴浵睟形档物⹰慷❶‬洧污彥欸眮癡Ⱗ✠灳捥牴浵湟形楤捳㍟〶⸰慷❶‬猧数瑣畲彭扮损楨灲眮癡Ⱗ✠灳捥牴浵睟形楤捳㕟〸⸰慷❶‬琧獥⹴慷❶‬洧污彥㘱⹫慷❶‬昧汭扟灵慟瑴潟杲眮癡崧㠠਍栯浯⽥畡潴慭楴湯⼴畡潴慭楴湯䐯䙖㤹䅟瑵浯瑡潩⽮湉異⽴潣普杩琮瑸਍††††††††††唠䕓⁒†††倠䑉䄠䍃卅⁓佃䵍乁ൄ⼊敤⽶瑴啹䉓㨸††††畡潴慭楴湯‴㈠㘲㔷䘠⸮⸮氠杯湩楟楮൴⼊敤⽶瑴啹䉓㨹††††畡潴慭楴湯‴㈠㘲㔶䘠⸮⸮氠杯湩楟楮൴匊牥慩潐瑲⁳楬瑳㨠†❛✹‬㠧崧਍敓楲污瀠牯獴㨠†❛搯癥琯祴单㥂Ⱗ✠搯癥琯祴单㡂崧਍桃灩敳⁴‱›ㄠ㄰਍桃灩敳⁴′›ㄠ㄰਍潂牡⁤‱敓楲污倠牯⁴›⼠敤⽶瑴啹䉓ഹ䈊慯摲㈠匠牥慩潐瑲㨠†搯癥琯祴单㡂਍潂牡ㅤ䤠⁐›ㄠ㈷㈮⸸⸴㈳਍潂牡㉤䤠⁐›ㄠ㈷㈮⸸⸴㘳਍潂牡㍤䤠⁐›ㄠ㈷㈮⸸⸴㔲਍潂牡㑤䤠⁐›ㄠ㈷㈮⸸⸴㜲਍潂牡㕤䤠⁐›ㄠ㈷㈮⸸⸴㠵਍汯⁤敲畳瑬搠汥猠捵散獳⹳⸮‮慰獳਍潔‽❛牳敥慫瑮⹨䁳獤杰挮浯崧਍灓楬⁴潶灩‽ര⨊⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪਍⨪⨪⨪⨪⨪⨪⨪潓湵⁤慃摲䌠湯捥楴湯⩳⨪⨪⨪⨪⨪⨪പ⨊⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪਍佂剁ㅄⴠ㸭戠弱畯彴潰瑲漽瑵档ⰱ戠弱湩灟牯㵴湩档റ䈊䅏䑒′ⴭ‾㉢潟瑵灟牯㵴畯捴㉨‬㉢楟彮潰瑲椽据㉨਍⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪പഊ嬊⼧敤⽶瑴啹䉓✹‬⼧敤⽶瑴啹䉓✸‬⼧敤⽶瑴啹䉓✳‬⼧敤⽶瑴啹䉓✲‬⼧敤⽶瑴啹䉓✱‬⼧敤⽶瑴啹䉓✰‬⼧敤⽶瑴卹✰൝嬊⼧敤⽶瑴啹䉓✹‬⼧敤⽶瑴啹䉓✸‬⼧敤⽶瑴啹䉓✳‬⼧敤⽶瑴啹䉓✲‬⼧敤⽶瑴啹䉓✱‬⼧敤⽶瑴啹䉓✰‬⼧敤⽶瑴卹✰൝䈊慯摲†椠⁰†‽㜱⸲㠲㐮㈮㤰਍慇整慷⁹灩†㴠ㄠ㈷㈮⸸⸴㔲ഴ䌊浯瀠牯

Or gedit displays messages like:

Could not open the file “/home/user4/con…g_2017.04.06-16.05.48.txt”.
Unexpected error: Invalid byte sequence in conversion input

when I try to open the file.

When I run some thing simpler like:

script -c ifconfig ~/user4/Scripts/test.txt

it runs just fine, i.e, I get the expected output at the log file. How to avoid such unreadable characters?

  • 1
    what is the output of python -c "import sys;print(sys.getdefaultencoding())"' and of export | grep LANG ? Make sure both is set to UTF-8 – Michael D. Apr 6 '17 at 11:19
  • I doubt this is the problem, but why are you using gnome-terminal? Why not just bash -c . . . ? – terdon Apr 6 '17 at 11:20
  • 1
    Do you get the same issue if you just direct the output of the python program to a file, or only with script? i.e. with python test.py > outfile instead of script -c "..." outfile ? – ilkkachu Apr 6 '17 at 11:31
  • 1
    Setting all your locales to en_IN.UTF-8. Your python file should be encoded to utf-8. add this line to the top of your python file # -*- coding: utf-8 -*- That should generate an utf-8 encoded textfile which will open. – Michael D. Apr 6 '17 at 12:46
  • 1
    @skrowten_hermit, I thought so, I don't think there's any reason for script to mangle the output, so it's probably not an issue with that. You might want to try to find out what part of the python program produces the output with broken encoding (and if it is actually broken) – ilkkachu Apr 6 '17 at 13:08
2

In case if you are running python script in Linux environment, follow the below steps before running the script:

$ script console_log.txt
$ run your python script
$ exit  (After the script completes execution)

Entire console logs will be captured in "console_log.txt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.