Error 

Tagged as ‘Sys Admin’

Suggested change to aliasing commands

Posted January 18, 2010 at 02:01am in Linux

When I wrote the code to automatically generate aliases for hosts in my SSH config I started thinking about how checks are never done to verify we are not overriding an existing alias. This is my solution for it. I almost think that alias should be an alias for register_alias so that all aliases get checked, but I’m sure there could be an instance where it would break something. Let me know what you think about assigning aliases this way.

# .bash_functions
function register_alias() {
  local alias=$(echo $* | cut -d'=' -f 1)
  local TEMP_CMD=$(which $alias)
  local TEMP_ALIAS=$(echo "`alias`" | sed 's/alias\ \(.*\)=.*/\1/' | grep ^$alias$)
  if [ ${#TEMP_CMD} -gt 0 ]; then
    echo "Alias $alias conflicts with command $TEMP_CMD"
  elif [ ${#TEMP_ALIAS} -gt 0 ]; then
    echo "Alias $alias conflicts with alias $alias"
  else
    alias "$*"
  fi
}
# .bashrc
if [ -f ~/.bash_functions ]; then
  . ~/.bash_functions
fi
# .bash_aliases

register_alias sls='screen -ls'
register_alias sdr='screen -d -r'

Update: I changed the TEMP_ALIAS line to use sed instead of cut/sed

Various aliases and short scripts

Posted January 18, 2010 at 12:01am in Linux

There are some aliases and small scripts I use on a normal basis.

I prefer to just type in the machine I want to ssh to instead of typing ssh in front of it. This chunk of code goes in my ~/.bashrc file and creates an alias for each “Host …” entry in ~/.ssh/config. It checks to see if there is an existing command that matches the Host entry, and alerts you if there is a conflict

# Generate SSH aliases
for host in $(grep ^Host .ssh/config | sed s/Host\ //g); do
  TEMP_CMD=$(which $host)
  TEMP_ALIASES=$(echo "`alias`" | sed 's/alias\ \(.*\)=.*/\1/' | grep ^$host$)
  if [ ${#TEMP_CMD} -gt 0 ]; then
    echo "Alias $host conflicts with command $TEMP_CMD"
  elif [ ${#TEMP_ALIASES} -gt 0 ]; then
    echo "Alias $host conflicts with alias $host"
  else
    alias $host="ssh $host"
  fi
done

When you open a terminal you will see something like this if you have conflicts.

Alias www conflicts with alias www
Alias hg conflicts with command /usr/bin/hg
Alias git conflicts with command /usr/bin/git
[20:05:21] manis@baron:~$

Read the rest of this entry »

Ubuntu User Directories

Posted August 2, 2009 at 08:08pm in Linux

I discovered that if you delete directories in your home directory like Templates and other system folders it will make an undesirable change to ~/.config/user-dirs.dirs changing the location of variables to $HOME/, which means if you want to add the directory back you have to edit the file.

This file defines the locations for Desktop, Music, Templates, etc so I went ahead and moved those files to Dropbox so I can maintain the files across multiple machines.

While I’m on the topic of these directories, I want to mention the awesomeness that is the Templates directory. If you add the file “Python Script.py” to that directory and put in the following text:

#!/usr/bin/python2.5
#
# Copyright 2009

import sys

# Enter text here

You can then right click in a directory, go to “Create Document” you will see “Python Script” listed and if you select it a .py file will be created with the previous text in it. This directory lets you create templates for anything you want and easily use it to create documents.