Android and Linux

Thursday, May 27, 2010

Copy/paste between Linux and N1

Using my post about copy/paste in the Android terminal, it becomes easy to share clipboard contents between your phone and Linux.

First, you need a way to access the Linux clipboard from a terminal. I use either xclip or xsel. Both are old programs which always seem to work differently for each application, so this example uses both.

You need to set up ssh keys to login to your home computer from the phone. The general layout for using keys in a script is something like: ssh USER@IP -i KEYFILE. You can also briefly login to a computer and run a command over ssh by using: ssh USER@IP -i KEYFILE 'command' and that's what we'll be doing.

In order to get the computers clipboard contents, we'll use the command "xsel -p." Since xclip and xsel are utilities meant to interact with the X display, you may also need to export the display then unset the display in order to get a clean logout with ssh. For simplicity, I made a script called paste2n1 which can be ran with the ssh command.

#! /bin/sh
export DISPLAY=:0.0
xsel -p
unset DISPLAY


Now I can run this command to display the computer's clipboard on my phone:
ssh USER@IP -i KEYFILE 'paste2n1'

By piping it to the copy script I posted previously and running this command from gscript, I can copy the computer's clipboard contents to the phone with one tap of an icon on the phone.

ssh USER@IP -i KEYFILE 'paste2n1' | copy

To paste the phone's clipboard contents to the computer, I'll use xclip with this script named n1_to_comp on the computer:
#! /bin/sh
in=$(cat /dev/stdin) # or "in=$(cat -)"
export DISPLAY=:0.0
echo "$in" | xclip
unset DISPLAY


ssh supports most commands through pipes so you can send any text to the computer with the command: ssh USER@IP -i KEYFILE 'n1_to_comp'

You could use that as a flexible all-around paste command/script to paste any output to the comptuer such as: cat textfile | ssh USER@IP -i KEYFILE 'n1_to_comp'

Or use my previous command I called "clip" for pasting the phone's clipboard in the terminal for a one touch clipboard-to-clipboard transfer using gscript: clip | ssh USER@IP -i KEYFILE 'n1_to_comp'

These are just examples and instead of piping to different scripts, you could write a simple one-liner for each operation. I may write a single script with options to handle both operations if I get bored.

Sunday, May 23, 2010

locate.sh

I polished and uploaded my locate script to L.E.S. Linux. It can be found in L.E.S. under the name "locate.sh" or here:
#! /system/bin/sh

if test -z "$1"; then exec echo "USAGE: locate.sh [filename|-u|--help]"; fi

case "$1" in
--help)
echo 'locate.sh by fubaya (a-more-common-hades.blogspot.com)
This is a cheap imitation of the UNIX locate command. It stores
a list of all the files on your system to /sdcard/.locatedb (about 2MB)
then greps that file to find whatever you are looking for.

USAGE: locate.sh [filename|-u|--help]

[filename] case insensitive search for any file of any type.

-u update the "database." This must be ran periodically if you want to
find new files. I have it set to run at boot on phone.

--help display this help message

EXAMPLE: $ cd /; touch /data/testfile
$ locate.sh testfile
$
$ locate.sh -u
database updated
$
$ locate.sh testfile
/data/testfile
$
'
;;
-u)
find / -print > /sdcard/.locatedb && echo "database updated"
;;
*)
grep -i "$1" /sdcard/.locatedb
;;
esac


Copy locate.sh to your clipboard with this QR code:

Thursday, May 20, 2010

Whereis redux

I made my little whereis script a little better and uploaded it to the Android app L.E.S. Linux.

L.E.S. Linux is a cool little app which allows you to download Linux executables, scripts and libraries which have been compiled or made for Android. I will probably put any scripts which I think may be useful on L.E.S. The only other one I have in mind so far is the little "locate" script which I'll upload after I make it a little more presentable.

Here is the whereis script I uploaded. I named it whereis.sh to avoid confusion with the real (but missing on Android) whereis executable but, oops, I accidentally called it "whereis" instead of "whereis.sh" in the actual script. Oh well.

#! /system/bin/sh

if test -z "$1"
then
echo "whereis.sh by fubaya (a-more-common-hades.blogspot.com)
This is a cheap imitation of the UNIX whereis command.
It doesn't imitate all the whereis functions, it only looks in your
path for the binary (or other file) which you specify as an argument.

USAGE: whereis [arg]
EXAMPLE:
$ whereis date
/system/bin/date
/system/xbin/date
$"
else
echo $PATH | tr ':' '\n' | while read line; do ls "${line}/${1}" 2>/dev/null; done
fi


Copy whereis.sh to your clipboard with this QR code:

Monday, May 3, 2010

Paste in an Android terminal

I've always found it useful to be able to paste the clipboard contents in a terminal. I often find links in the Browser that I want to download with wget, but how to get the link into terminal without having to type it out?

My brief search for how clipboard content is stored yielded no results. It would be nice if it was stored in a file so we could grab the contents, but it's probably held in memory. At any rate, there is a workaround very similar to the workaround I used on the iPhone.

I went looking for some sort of clipboard helper app that does store the clipboard in a text file. The one I settled on is called Clipstore by benishouga and it stores the clipboard contents in a plain text file at /data/data/jp.benishouga.clipstore/files/clip.txt.

It is meant to keep a clipboard history of multiple items and it stores every entry on a single line. It replaces newlines with a "n" tag so even if you have 20 clipboard history items consisting of multiple lines, the most recent item will always be on top and it will all be on a single line. So the paste script can simply be:
#! /system/bin/sh
# Requires the app "Clipstore" by benishouga and app must be opened or allowed to
# run in the background in order to transfer system clipboard to app clipboard
sed 's/<n>/\n/g;q' /data/data/jp.benishouga.clipstore/files/clip.txt
I suggest not naming it "paste" to avoid any conflicts with the unix command of the same name. I named it "clip".

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


I may try to figure out a way to do a copy command soon, but it isn't usually as handy.

Update: Copying text is actually pretty easy using Clipstore. You just have to stick the text in the file at /data/data/jp.benishouga.clipstore/files/clip.txt, but to use it, you have to open Clipstore and select the text. This makes Clipstore put it in the system's clipboard and it can then be pasted in any app. Neither the copy or paste are perfect, but I don't know of a way to access the system clipboard directly. Neither command is needed very often, although they're lifesavers when they are, so having an extra step isn't really a problem.

Here's the copy script:
#! /system/bin/sh
# Requires the app "Clipstore" by benishouga and text must be
# selected in Clipstore to transfer it to the system clipboard
in=$(cat -)
lines=$(echo "$in" | wc -l)
if [ "$lines" = 1 ]
then sed -i "1i\\
$in" /data/data/jp.benishouga.clipstore/files/clip.txt
else
inn=$(echo "$in" | sed ':a;N;s/\n//g')
sed -i "1i\\
$inn" /data/data/jp.benishouga.clipstore/files/clip.txt
fi

Copy the "copy" script to your clipboard with this QR code:

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