Tag: Languages

bash debug – log all executed commands

Screenshot of a Bash 3.1 session demonstrating...
Screenshot of a Bash 3.1 session demonstrating its particularities. Shows exporting a variable, alias, type, Bash’s kill, environment variables PS1, BASH_VERSION and SHELLOPTS, redirecting standard output and standard error and history expansion. A POSIX session is launched from a normal session. Finally, the POSIX session kills itself (since just “exit” would be too boring). (Photo credit: Wikipedia)

Whenever I am writing a script in perl or bash, I always wish that there
was some way to have all the commands logged or output to screen. I know
there is “set -x” option to have debugging enabled, but sometimes that
seems to be too much information and I dont really need all that. So, here
is something I found recently for bash to log all the executed commands.

trap "echo $BASH_COMMAND" DEBUG

This will echo/print all the commands on the stdout.

 

Enhanced by Zemanta

Fix typescript files generated with script command

Generally quite a lot of us would have used the script command. This generates the logs for the session. But the problem with the logs is that it contains a lot of un-readable characters. These characters are mostly from the color codes, and as such can be removed very easily with a single command:

cat typescript | 
 perl -pe 's/e([^[]]|[.*.*?[a-zA-Z]|].*?a)//g' | col -b > typescript-processed

This assumes the input log file is named as typescript and the output is kept as typescript-processed. You can change the names as required.

 

 

Enhanced by Zemanta

create text tables from delimited files.

To create simple text tables to paste in emails or to use in any other document where you want to show a table, here is something that you can use. There is a perl module which provides “tablify“. And here is how to use it:

sudo yum install perl-Text-RecordParser

This will install a command “tablify” that you can use in number of ways. Here is a simple example to use it. You can read the man pages to see how you can use it.

: tmp ; cat < b.tab 
1	2001
2	3001
3	5001
4	1001
EOF

: tmp ; tablify --no-headers b.tab 
+--------+--------+
| Field1 | Field2 |
+--------+--------+
| 1      | 2001   |
| 2      | 3001   |
| 3      | 5001   |
| 4      | 1001   |
+--------+--------+
4 records returned

Enhanced by Zemanta