Android and Linux

Wednesday, April 28, 2010

Dump a webpage in terminal

This doesn't work as well an lynx -dump, but is a simple alternative that does work pretty well for some pages. It uses wget to dump the raw html to stdout, then sed to clean out the javascript and html tags. I also have an optional grep tacked on the end to get rid of lines of text from Google Adsense which can add up to quite a bit on pages which use it. That was just something that was present on the pages I tested it with and I figured I might as well leave it.

The first line is a test to check for proper formatting of the link. The wget present on Android phones is a stripped down Busybox version which requires the http before the address and simply won't work for "google.com". The script checks for the http and adds it if it's not present, so if you type "dump google.com", it should still work.

Unfortunately, it doesn't work well at all on pages containing css.

#! /system/bin/sh
echo $1 | grep -q http || pre="http://"
wget -q ${pre}${1} -O - | sed -e '/<script type="text\/javascript"/,/<\/script>/d' -e 's#<[^>]*>##g' | grep -v googleAdd

Copy "dump" to your clipboard with this QR code:

Friday, April 16, 2010

Two more missing Linux commands imitated

The "man" and "whatis" commands are two more useful command line utilities that are missing from Android. Here are two more scripts to imitate their function.

These require a network connection because they download information from unixhelp.ed.ac.uk. For this reason, there is no guarantee the information will be accurate for your system. This information is normally installed when the program is installed, but it's not on Android so the best I can do is grab general information off the web.

Please note, you may already have a man command if you have Busybox installed. My Nexus One with Cyanogenmod has man located at /system/xbin/bb/man but it is just a link to Busybox and it didn't work so I just deleted it.

You can use my previous whereis command to see if you have a man command installed:

# whereis man
/system/xbin/bb/man
#

Then run "ls -l /system/xbin/bb/man". If it is a link to busybox, the output will contain this: "/system/xbin/bb/man -> /system/xbin/busybox" and you are safe to delete it. To restore it later, just create a link to busybox named "man".

Anyway, here are the scripts:

man:
#! /system/bin/sh
wget -q -O - "http://unixhelp.ed.ac.uk/CGI/man-cgi?${1}" | grep -A 1000 NAME | sed -e 's/<[^>]*>//g' -e 's/^[ \t]*//'

Copy "man" to your clipboard with this QR code:


whatis:
#! /system/bin/sh
wget -q -O - "http://unixhelp.ed.ac.uk/CGI/man-cgi?${1}" | grep "\- " | head -1 | sed 's/^[ \t]*//'

Copy "whatis" to your clipboard with this QR code:

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.

whereis:
#! /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.

Examples:
# whereis grep
/system/xbin/grep
# whereis sh
/system/bin/sh
/system/xbin/sh
#

locate:
#! /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.

Examples:
# locate ncurses
/system/lib/libncurses.so
# locate livewallpaper
/cache/dalvik-cache/system@app@LiveWallpapers.apk@classes.dex
/cache/dalvik-cache/system@app@LiveWallpapersPicker.apk@classes.dex
/system/app/LiveWallpapersPicker.apk
/system/app/LiveWallpapers.apk
/data/data/com.estrongs.android.pop/app_.apps/s.system.app.LiveWallpapers.apk
/data/data/com.estrongs.android.pop/app_.apps/s.system.app.LiveWallpapersPicker.apk
#

locate-update:
#! /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.

Followers