Posts Tagged Programming

Quick tip: Counting lines of code

I wanted to count the lines of code on a project I am working on. From the base directory of the project, I wanted to line count all files ending with .php. I also needed to ignore the directories with test, model/om, cache and plugins. Using grep, find and wc you can do this:


#!/bin/bash
grep -v '^ *$' `find . -iname "*.php" | grep -v model\/om | grep -v \/test | grep -v \/cache | grep -v \/plugins | grep -v \/web | grep -v lib\/OFC` | wc -l

I am at 15645 lines of code, which includes comments. What are you at?

Tags: , , ,

Make your bash shell cool again

I originally wrote a spec in 2006, but it never actually made it in.  You can see it at https://wiki.ubuntu.com/Spec/EnhancedBash .  The good news is, it is just as relevant today. Hopefully these tips will make your life easier, I use them on all of my systems.

Use the page up key to complete the command
What this does is, if I type ‘ssh ‘ then hit the page-up key, it will complete the line to the last time in my history file that I typed ssh. Hitting page up again will go to the 2nd to last time I typed it. Incredibly handy if you ever type the same commands more than once. This actually has been the default of SUSE based distributions for quite some time.


echo "\"\e[5~\": history-search-backward" >> ~/.inputrc
echo "\"\e[6~\": history-search-forward" >> ~/.inputrc

Increase history size
Now that you have the pageup / pagedown bindings to auto-complete your history, you are going to want to store more of it. You can store the previous 1000 commands using this.


echo "export HISTSIZE=1000" >> ~/.bashrc
echo "export HISTFILESIZE=1000" >> ~/.bashrc

Color grep
Using the following bash alias, you can make grep highlight the word it is matching in color. I grep about 10000 times a day, so this makes it a bit easier on my eyes to focus where I need to:


echo "export GREP_OPTIONS='--color=auto'" >> ~/.bashrc

Command matching
There was also someone who contributed more sane command matching (ie what happens when you hit the tab key a bunch). I use these also, which tell it not to do stupid things like try to match hidden files and to display both files if they are ambiguous:


echo "set match-hidden-files off" >> ~/.inputrc
echo "set page-completions off" >> ~/.inputrc
echo "set completion-query-items 350" >> ~/.inputrc
echo "set show-all-if-ambiguous on" >> ~/.inputrc

Tags: , , , ,

Python Commands Module

Python logoPython Commands Module

The Python module ‘commands’ allows commands to be ran on the system. I frequently use this in combination with Django to launch processes.

Get a directory listing:

#!/usr/bin/python
import commands
status, output = commands.getstatusoutput('ls')
print "Status: " + str(status)
print "Output: " + str(output)

This program will print out the directory contents of where you ran it from. An important thing to note about programs in Linux in general is that on success they return 0, and anything not 0 is probably an error. I commonly check the status of the returned code:

Check if status is not 0:

if status != 0:
    print "An error occurred!"

This allows us to do some really fancy things with this module. For instance, I created an application that is a django app that provides the user a menu to launch programs. This runs on their Firefox browser, but will launch the programs on their system. Each system runs it’s own Cherokee webserver locally, which is extremely lightweight and fast. To install Cherokee to use django I followed this guide: http://www.cherokee-project.com/doc/cookbook_django.html

The users run as the user ‘autologin’, however the webserver runs under the user ‘www’. This is probably a bit hackish, but it works. What I do is copy autologin’s .Xauthority file to the www user’s home directory, then launch the program as autologin using sudo.

Launch a local X program from a django program:

def launchAutologinXProgram(commands):
    status, output = commands.getstatusoutput('sudo cp /home/autologin/.Xauthority /home/www/')
    os.spawnl(os.P_WAIT, "/usr/bin/sudo", "sudo", "-u", "autologin", "-H", "/bin/bash", "-c", 'export DISPLAY=":0.0"; . /etc/profile; nohup ' + str(commands) + ' &')

Obviously you want to check the status variable and make sure it completed. To do this you need to update your sudoers file to make the www user be able to run commands as autologin. This is for a kiosk type setup, so www and autologin don’t have sensitive data, I am sure there is a more secure way to do it. But it sure is fun.

Tags: , , ,

Finding the difference between two files

Ever need to find what you changed between two files or directories? The universal way to find these differences is to use the ‘diff’ command. The diff command is used by most open source projects, and people use it to communicate their changes / bug fixes etc.

Console using diff

Console using diff

Sometimes I find the diff command to be boring. Much like my terminal prompt, I like consoles to have color. The color helps me figure out what is going on a bit quicker. Do get diffs in color, you can ‘sudo apt-get install colordiff’. The resulting output will look like this instead:

Console with color diff

Console with color diff

But there are some people out there who just don’t like consoles / terminals for one reason or another. There is also a GUI based diff tool called meld. You can install meld by running ‘sudo apt-get install meld’. Your diff will now look like this:

Screenshot of meld

Screenshot of meld

That is a brief overview of the tools I have found useful, using screenshots.

Tags: , , , , ,

Self Explanatory Perl Code

For some reason I had to ask on IRC what this meant, because it doesn’t google well:


$| = 1;

I don’t know why I didn’t understand that intuitively :) At any rate, the answer is:

LeoNerd: No, it forces the currently selected output filehandle not to buffer its output

Tags: , ,