bc

So, I just discovered the Unix tool bc.  Given that I’m a Mac guy as much for the Unix as for anything else, this got me interested.

bc is a mathematical programming language and interactive calculator.  The latter is useful, sometimes I need a calculator, and for what I’m doing that instant, a terminal app is most appropriate.  bc fills that role well.

bc also is a C like programming language for mathematical operations.  I haven’t gotten deeply into it, but here’s a factorial function, with a simple recursive implementation.  

/*Factorial function in bc*/

define fact(x)

{

    if( x <= 1){

        return 1

    }else{

        return x * fact(x-1)

    }

}

Advertisements

M3u is brilliant in its simplicity

I was all like, “I need to write a perl script to make these M3u playlists”… then reality hit me and was all “Dumbass.  OS X is Unix.  Use IO redirection and pipes”.  This shell command creates a playlist from files in the current directory.

    ls | grep -v *.m3u | cat - > playlist.m3u

It’s not particularly clever, the only filtering prevents a recursive playlist which I’ve found to blow up VLC.  At least on OS X.  I don’t know if there is defined behavior with such an event, but VLC doesn’t like it and I’m doing this to feed playlists to VLC, so I have to make sure I don’t do it.

I’m not sure why the cat – is necessary.  The grep’d ls should keep out the m3u file, but it doesn’t unless I feed it to cat first.  This is probably a flaw in my understanding of how pipes work rather than anything else.  

I should make this into a BASH script and do some more extensive filtering.  I don’t think this needs Perl or another ‘proper’ programming language, though.

M3u being just a text file containing a list of filenames, with relative paths assumed when an absolute is not given, is dead simple and lets you do stuff like this.

Of course, if you want more complicated tricks you’d need something like XML.  But for basic cases, m3u is plenty, and it’s easy to work with.