Android and Linux

Saturday, April 10, 2010

Replacing missing utilities

These two scripts were rewritten and improved. Here are the improved versions of whereis and locate.

The Nexus One, and Android in general, is missing a few basic unix utilities, but we can imitate them with short shell scripts.

#! /system/bin/sh
echo $PATH | tr ':' '\n' | while read line; do ls "${line}/${1}" 2>/dev/null; done
The whereis command is used to locate executables. This script will go through every directory in your path and try to list the executable you specify as an argument. If it doesn't find it, the errors go to /dev/null so you only see output if it is found.

# whereis grep
# whereis sh

#! /system/bin/sh
grep -i "$1" /sdcard/stuff/files
The locate command is a bit like the find command, except faster. When it is ran in update mode, it searches through the system and compiles a database of all files, so when asked to locate a certain string, it is able to do so almost instantly by searching it's database. This uses the find command to list all files and put them in my fake locate database at /sdcard/stuff/files, but it could use any file or location you want.

# locate ncurses
# locate livewallpaper

#! /system/bin/sh
find / -print > /sdcard/stuff/files
This updates the locate database. Like the normal locate command, it has to be ran once before the locate script will work or else the database will be empty and it won't locate anything.