Category

shell

  • 01.09.12
    Greping from inside Vim shell | n0str0m0 | (0)
    Hi there, First things first: Happy New Year! :) One task I usually perform when I am writing code is grepping the code for a certain string. In order to do that, I used to exit Vim (either with :q or suspending the process), grep the files and then go back to my Vim session. This approximation has one main drawback: I usually don't remember all the matches reported by grep. Vim provides a mechanism for invoking grep from a Vim session. The command is: :grep string files This way, one can invoke his local grep command and the results will be integrated into the ...
  • 11.12.11
    Unicode sorting in perl shell | pfortuny | (0)
    Just Normalize. Assume file.txt is a list of unicode words, then cat file.txt | perl -e 'use Unicode::Normalize; my @w ;\ while () {chomp; push @w, $_;} ; @w = sort {NFD($a) cmp NFD($b) } @w ;\ print(join("\n", @w))' will output the sorted list (well, sorted according to the NFD normalization, which for Spanish is enough).
  • 10.15.11
    Creating shell archives shell | n0str0m0 | (0)
    shar(1) is a FreeBSD base system utility for creating shell archives that is, a kind of self-extracting archive of a hierarchy of files and directories. The archive will recreate the file hierarchy specified at creation time. It is fast and the result is easy to handle as the shell archive is a text file (good to email a file hierarchy). To create a shell archive: $ shar `find my_directory` > my_hierarchy.shar To recreate the hierarchy: $ sh my_hierarchy.shar That is, shar is a simpler version of ar and tar. Enjoy!
  • 09.22.11
    How to know if a CPU is 32 or 64-bit shell | rafacas | (0)
    Nowadays almost all the computers have a 64-bit CPU, but sometimes we are logged in on a remote server and do not know what kind of CPU it has and we need to know it to install a package or... out of mere curiosity. In those cases we can run the following command: $ grep --color lm /proc/cpuinfo flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up ida nonstop_tsc arat pni ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm If the lm flag ...
  • 09.13.11
    Audio settings on FreeBSD shell | n0str0m0 | (0)
    mixer(8) is a FreeBSD base system utility for setting and displaying sound card mixer levels. It resembles the Linux's command line utility alsamixer. Invoked with no parameters it shows the current values of the mixer devices: $ mixer Mixer vol is currently set to 85:85 Mixer pcm is currently set to 75:75 Mixer speaker is currently set to 75:75 Mixer mic is currently set to 0:0 Mixer mix is currently set to 0:0 Mixer rec ...
  • 09.01.11
    Adding formatting to an XML document shell | rafacas | (0)
    Sometimes, when I have to program a web service client I have to deal with unformatted XML files. For example, the next one: <users><user><email>pfortuny@commandliners.com</email><passwd>a0f901492d89fe2ba88cc96bf9d 2475e</passwd></user><user><email>n0str0m0@commandliners.com</email><passwd>7e1b6dbfa824d 5d114e96981cededd00</passwd></user><user><email>rafacas@commandliners.com</email><passwd> 70c1db56f301c9e337b0099bd4174b28</passwd></user></users> This is not a bad thing, because it is sent that way to save traffic, but I'd rather see it in a human readable format. So I use the xmllint command, that reformat and reindent the input. $ xmllint --format test.xml <?xml version="1.0"?> <users>   <user>     <email>pfortuny@commandliners.com</email>     <passwd>a0f901492d89fe2ba88cc96bf9d2475e</passwd>   </user>   <user>     <email>n0str0m0@commandliners.com</email>     <passwd>7e1b6dbfa824d5d114e96981cededd00</passwd>   </user>   <user>     <email>rafacas@commandliners.com</email>     <passwd>70c1db56f301c9e337b0099bd4174b28</passwd>   </user> </users> The indentation can be controlled by the environment variable XMLLINT_INDENT. The default value is two spaces. pfortuny, n0str0m0, do not worry guys, those are not your ...
  • 08.18.11
    Determining if an XML document is well-formed shell | rafacas | (0)
    After creating an XML document from scratch I always check if it is well-formed. This means it must adhere to a number of rules, including the following: Every start-tag must have a matching end-tag. Elements may nest, but may not overlap. There must be exactly one root element. Attribute values must be quoted. An element may not have two attributes with the same name. This is not an exhaustive list and I do not mean to explain all the rules. There are many, many ways a document can be malformed. But if you need to determine if and XML document is well-formed there is a linux ...
  • 07.21.11
    Printing sequential numbers in BSD shell | rafacas | (0)
    In Linux, the seq command is pretty useful in some scripts, because it prints a sequence of numbers: $ seq 1 5 1 2 3 4 5 It is usually used in for loops: for i in `seq 1 5`; do ... done But this command is not included in BSD-like OSes. It is contained in the sh-utils, so one option is downloading and compiling it. But I prefer using the commands that come by default with the OS, for portability. In the BSD case, I found the jot command that prints sequential or random data. The following example shows the seq behaviour with jot. $ jot 5 1 2 3 4 5 Other example ...
  • 07.08.11
    Script for making an ordered list of files shell | pfortuny | (0)
    You've got a bunch of files you want to reorder (that is rename so that they appear in a specific order) but do not want to really rename them (their original names are relevant). You may want to create a set of links to each of them with the appropriate '001', '002' term before. Here is a script for doing exactly this. Use as follows (if named 'mapnames.sh'): $ mapnames.sh digits file1 [file2 file3 ...] will use digits digits for the leading numeral (adding '0' before) and will link the files in the given order. Here is the code: #!/bin/sh length=$1 shift count=1 for name in $@ do ...
  • 04.08.11
    Screenshots from the command line (FreeBSD) shell | n0str0m0 | (0)
    vidcontrol is a nice utility that among other things allows one to capture the state of a video buffer and dump it to a file. The format of this file is specified in the man page as it is not a regular image file. For this reason one needs a specific tool to convert it to something one can play with. These commands are scr2txt (/usr/ports/textproc/scr2txt) and scr2png (/usr/ports/graphics/scr2png). They can then be combined using one single line: $ vidcontrol -p < /dev/ttyv0 | scr2txt > /tmp/screenshot.txt The -p flag instructs vidcontrol to get the current content of the corresponding video buffer. ...