Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

2010-08-08

Handling SSH on non-standard ports for subversion and git

Working with subversion and git over SSH on a non-standard port (!=22) is troublesome as the host:port syntax does not work.

One easy way around this is to to define an entry as the following in $HOME/.ssh/config:
Host my.githost.com
Port 7822

This allows one to use the host normally through most SSH-using tools such as subversion and git!

2009-05-18

Indentation problem when pasting into vim and the nopaste option

When pasting content into vim, such as a code snippet, it may mess up the indentation in a nested kind of way, with indentation increasing the further down you go.

The vim paste option fixes this - just issue :set paste before pasting, and :set nopaste when done.

As noted by the vim documentation, the underlying problem is that a terminal program, in contrast to a GUI one, may not recognize the difference between pasted and typed text.

2008-08-04

Better shell scripts: set -u

Beginning shell scripts with "set -u" could save you serious time. From bash(1):
Treat unset variables as an error when performing parameter expansion. If expansion is attempted on an unset variable, the shell prints an error message, and, if not interactive, exits with a non-zero status.
Example:
$ cat hello.sh
#!/bin/sh

NAME=$1
echo Hello $NAME
$ sh hello.sh
Hello
vs
$ cat hellou.sh
#!/bin/sh

set -u

NAME=$1
echo Hello $NAME
$ sh hellou.sh
hellou.sh: line 5: $1: unbound variable

2008-07-23

Send IM messages from the command line with iChat, Adium or Pidgin



In my office there was a tradition of using the zwrite command line tool for writing IM to eachother. This is useful to send snippets such as
grep FATAL logfile | zwrite coworker
It's quick, avoids tedious copy and paste and allows sending messages from batch run scripts. However, zwrite started to show its age long ago, being difficult to setup on various systems and having poor desktop integration and power.

Using XMPP (mostly over the Google Talk servers) works nicely, having good native clients, a web client (gmail) and a great java library for programmatic access. Missing was a zwrite-like command line way of sending snippets. I wrote a small shell script working with a running instance of either Pidgin under *nix (using D-Bus python bindings) and iChat or Adium (using applescript) on OS X:
#!/bin/sh

test $# -eq 0 && echo "Usage: echo hi | $0 user [otherusers] " && exit 1

export MESSAGE="`cat`"

while test "$1"; do

BUDDY=$1

# aliases
test $BUDDY = friend1 && BUDDY=friend1.account
test $BUDDY = friend2 && BUDDY=friend2.account
# ..

# default to @gmail.com
test `echo $BUDDY | grep @` || BUDDY=$BUDDY@gmail.com

export BUDDY

OS=`uname`
if [ "$OS" = "Darwin" ]; then
# use applescript to call either ichat or adium
/usr/bin/osascript <<EOL
set stdinText to do shell script "echo \"\$MESSAGE\"" without altering line endings

tell application "System Events" to set iChatRunning to (name of processes) contains "iChat"
if iChatRunning then
tell application "iChat"
send stdinText to buddy "$BUDDY"
activate
end tell
else
tell application "System Events" to set adiumRunning to (name of processes) contains "Adium"
if adiumRunning then
tell application "Adium"
tell the first account of the service "GTalk" to ¬
set newChat to ¬
make new chat with contacts {contact "$BUDDY"} ¬
with new chat window
send newChat message stdinText
activate
end tell
else
display dialog "ERROR: You must run either iChat or Adium" buttons "OK" default button "OK"
end if
end if
EOL
else
# calling pidgin over dbus
python -t - <<EOL
import os, sys
try:
import dbus
except ImportError: sys.exit('ERROR: You need dbus with python bindings installed')

try:
bus = dbus.SessionBus()
service = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(service, "im.pidgin.purple.PurpleInterface")
except: sys.exit('ERROR: Could not connect - is pidgin up and running?')

xmppAccount = None
for account in purple.PurpleAccountsGetAllActive():
if 'XMPP' == purple.PurpleAccountGetProtocolName(account): xmppAccount = account
if xmppAccount == None: sys.exit('ERROR: Cannot find any xmpp account')

recipient = os.environ['BUDDY']
matches = purple.PurpleFindBuddies(xmppAccount, recipient)
if len(matches) == 0: sys.exit('ERROR: Buddy "%s" not found!' % recipient)

conversationType = 1 # http://developer.pidgin.im/doxygen/dev/html/conversation_8h-source.html#l00051
conversation = purple.PurpleConversationNew(conversationType, xmppAccount, recipient)
im = purple.PurpleConvIm(conversation)
message = os.environ['MESSAGE']
purple.PurpleConvImSend(im, message)
EOL
test $? -ne 0 && exit 1
fi

shift
done

OS X command line tools: open

The /usr/bin/open command is a command line utility deriving from NextStep, and opens a file (or a directory or URL), just as if you had double-clicked the file's icon. It is the equivalent of the freedesktop xdg-open command but has a few options making it a bit more useful:
  • -a specifies the application to use, so "open -a firefox http://osnews.com/" opens the site in firefox.
  • -e Causes the file to be opened with /Applications/TextEdit
  • -t Causes the file to be opened with the default text editor, as determined via LaunchServices
  • -f Reads input from standard input and opens the results in the default text editor. End input by sending EOF character (type Control-D). Also useful for piping output to open and having it open in the default text editor.
  • -n Open a new instance of the application(s) even if one is already running.
  • -W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable.
Note especially that you can launch applications bundles, so
open $HOME/Downloads/GLTron.app
allows you to try out http://www.gltron.org/ directly after unpacking.