iPhone and Linux

Friday, January 29, 2010

Copy and paste in MobileTerminal

I've been using this "paste" script for a long time, but put off creating anything to copy as I didn't have much of a need. I finally got interested when it came up on a forum last night. Here is the first draft of working copy and paste.

Both scripts require PasteBaordStacker because, as I have mentioned before, it stores everything as plain text and I've never gotten around to deciphering Apple's PasteBoard.

Paste also requires awk. The awk line may be familiar, it has been so useful it's probably in half the scripts on this blog. Copy uses sed, but I think sed is already installed on a jailbroken iPhone.

Edit Feb 7 2010: added the sed lines to convert the characters <, >, and & which get automatically encoded.

#! /bin/sh
awk 'BEGIN{ RS="</string>"}{gsub(/.*<string>/,"");print;exit}' $file | sed '

#! /bin/sh
in=$(cat -)
text=$(echo '<string>'"$in"'</string>')
sed -i "7i\\
$text" $file

Paste can be used in a variety of ways, but there are two standard methods that are most useful. 1) You can type "paste" on the command line and it will output the last thing you copied on the system. 2) You can use it in a subshell to paste an argument to another program. Example, copy a link to a file in Safari then type wget $(paste) in Terminal. Since the subshell is executed first, the resulting command that is executed is wget www.example.com/file.txt

Copy is a little rough. Since I used PasteBoardStacker, that means text isn't copied directly into the Apple PasteBoard. You have to select the text in PasteBoardStacker first, which is done by double tapping the status bar, then tapping the text you want from the pop-up list. It takes an extra step but it's still a good way to copy text from Terminal, and other than directing the output to a file, opening it and copying, it's the only way I am aware of. Copy handles piped input so if you run this command: echo "Hello world" | copy, you can then select "Hello world" in PasteBoardStacker and paste into another app.

Copy also requires you to escape some special characters with a backslash. echo "Hello world!" | copy won't work because the shell will try to interpret the exclamation point. echo "Hello world\!" | copy is what you need to use. Edit: D'oh, this was a poor example because it's normal shell behavior and not related to copy, but the sed line in copy still can't handle some characters like "<"without escaping.

Blog Archive