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:

Followers