The Go Command

Alexey Komissarouk requested a modification to the cl command that opens the argument in the default text editor if it’s a file. The go command will open the file in the editor defined by the EDITOR environment variable.
Place this in your .bashrc, .bash_profile or analogous file:

go()
{
if [ -f "$1" ]
then
    $EDITOR "$1"
else
    cd "$1" && ls
fi
}

If you have not defined the default editor, perhaps you’d like to try emacs, my chosen editor. Add this to the .bashrc or .bash_profile as well:

export EDITOR=emacs
Advertisements
Posted in Shell Scripting | 11 Comments

Combining cd and ls Into One Command

Andrew Braunstein and I were discussing how one might add a shell command to change a directory and list it using one command. I’m sure anyone who uses the shell is familiar with cd followed immediately by ls.

A search found this discussion on the Unix.com forums. The suggestion was to add the following shell function to the .bashrc or .bash_profile:

cl()
{
if [ -d "$1" ]; then
cd "$1"
ls
fi
}

This works reasonably well, but I had a couple of complaints:

  1. Because the function does nothing if the argument is not a directory, when we try to cl aDirectoryThatDoesNotExist, there is no indication of the error. The shell just prints a new prompt.
  2. Also due to the check that the argument is a directory, we cannot use the default functionality of cd with no argument that returns us to the home directory.

My proposed solution is this:

cl()
{
cd $1 && ls
}

Here we take advantage of the exit code produced by the cd command to ensure that the directory change was successful before we ls. If cd exits with an error, the boolean “and” is already false, and the ls is not executed. All we see is the error message, and we remain in the same directory. This behavior seems ideal. Additionally, calling cl with no arguments changes to, and lists the home directory as expected.

cl in action

cl in action

Posted in Shell Scripting | Tagged , , | 2 Comments

Automatic SOCKS Proxy for a Domestic IP Address

Update 2013-03-31: In Mac OS 10.8 (and maybe 10.7), the Airport name has been changed to Wi-Fi. The script should look like:

trap "{ networksetup -setsocksfirewallproxystate Wi-Fi off; }" INT
networksetup -setsocksfirewallproxy Wi-Fi localhost 3333
ssh -ND 3333 USERNAME@HOSTNAME

While enjoying a relaxing spring break in Jamaica, I was attempting to read through the monster that is Gödel, Escher, Bach: The Eternal Golden Braid. Hofstadter inspired me to check out the music of Bach, so I logged into my Rhapsody account. All good. But when I attempted to play the songs I received the message, “This content not available in your country.” That’s interesting, since I’m a US based subscriber to Rhapsody. The problem was that the request was coming from a Jamaican IP Address.

Of course, there is a way around this. SSH into a machine in the US and create a SOCKS proxy so that the web requests are coming from the US-based IP. But this was inconvenient to do every time I wanted to listen, so I made a shell script to automatically create the SSH connection, change my Mac’s system proxy to this, and undo all the changes when quit.

Running this script will require an SSH public key pair be created so that the remote machine can be connected to without a password. Edit the script to include the correct user and host names. This will only work on a Mac. It also assumes that you are getting internet through the Airport. If you are getting internet through ethernet change Airport on line 2 to Ethernet. Any suggestions on how to find which device is providing internet in the script to do this automatically?

trap "{ networksetup -setsocksfirewallproxystate Airport off; }" INT
networksetup -setsocksfirewallproxy Airport localhost 3333
ssh -ND 3333 USERNAME@HOSTNAME

This can be run directly from the command line, but I enclosed it in an Automator workflow so that I could launch it through LaunchBar as though it were an application. When the script is closed it should turn off the proxy and restore the previous IP.

I suspect this method should work for any site that restricts content by IP address. Netflix, Hulu and Youtube users will likely find this useful.

Posted in Shell Scripting | Tagged , , , , , , , , | 1 Comment