for i in $(seq 1 10); do echo "BASH" | say; done;
That’s right. This week’s post is about Bash, and for once started with a project I was actually working on outside of my little endeavor here and thought might make a good post.
Where did I put my hammer?
You all know Bash, or at least you should), so I’ll keep my introduction to the language short and sweet.
There’s only one thing you really need to know about bash (or any other shell language, for that matter), and that is the concept of pipes. Pipes (the ‘|’ character) take the output from one command and “pipe” it as input to the next command. For instance,
echo "hello" would normally print “hello” to STDOUT, but you can take that output and pipe it anywhere you want. You can pipe it to something silly like
say (as I did at the start of this article) or something a little more practical, like
bc (a decimal calculator).
To take full advantage of the UNIX environment, you need to understand how you can take a series of small commands and piece them together to get what you want. For instance, when I start a create post with
rake new_post['Title'], octopress generates TIMESTAMP-Title.markdown. To open the new post up in Sublime Text, I can run
ls -1 | tail -1 | xargs subl -n, which is 3 commands (technically 4 because of xargs) joined together with the pipe.
ls -1prints the contents of the current directory (the
lscommand) and formats the output to one file per line (the
tail -1takes the last
1line of input and outputs it by itself. If I didn’t include this, Sublime would open every file and directory provided by
xargsis a special command that takes another command as its first argument and passes that command the input it receives from the pipeline. It’s generally used as a wrapper for commands that don’t handle pipes so well, but it has a whole range of more powerful formatting capabilities.
subl -nopens the file in Sublime in a new window (the
That’s the gist of it, and if you want to learn more, well here you go).
tl;dr Use | and people will marvel at your command line ninja skills.
Here’s this week’s program. I was inspired by this guy’s termal setup, especially the battery level indicator, but the python script he had started crying about encodings the minute I fired it up, so I decided to roll my own in pure bash, with pretty decent results. For reference, my current setup is iTerm2 with the LiquidCarbonTransparent color scheme, Inconsolata font, Bashstrap, and this hack, and it looks a little something like this:
And here’s how (also on GitHub):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
And here’s an example ~/.bash_profile to go with it (without full bashstrap integration, which is left as an exercise for the reader :D).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Normally I’d love nothing more than to spend several minutes explaining some dense bash code to you, but it’s late, so I’ll just tell you that the secret sauce comes from
ioreg -n AppleSmartBattery -r, which prints out a whole ton of “useful” information about the battery (and presumably other subsystems, hmmmm…). The code is relatively well commented, so I’ll let it speak for itself.
TL;DR To gain ninja-like bash skills, use | and google the fiddly bits in between.