Older Newer
Sun, 06 Dec 2009 09:01:04 . . . . 216-239-45-4.google.com [Remove spam]


Changes by last author:

Added:
My DotBashRc has been collecting more and more stuff for a while now.. every once in a while I just throw something new in when I do something for like the 10th time and realise that really, I could write a little function or alias and be lazier from then on. And laziness is good, believe me!

A function in bash is like a function in most other programming language. It can take parameters (which you access as $1, $2, etc.) and can have a return value (which must be an integer; 0 is used to indicate success). An alias in bash is really just a substitution. When you alias x to y, then every time you type x in a command to bash, it will replace it with y. That's how you have to think about it.

So, let's get on with it.. here's most of my DotBashRc. I've removed certain things that aren't relevant.

<code>

# Whee! vi-mode all the way!

set -o vi

# Some "typical" aliases. In fact, if you use RH, you probably don't need 'll'

alias users="who |awk '{print \$1}' |sort |uniq"

alias ll='ls -l'

alias vi=vim

# Useful for finding stuff in source

gsrc()

{

grep -IE "$*" `find . -type f |grep -vE > '(\.lo$|\.TPo$|\.dep$|\.\d$|CVS|\.mk$|Makefile|ChangeLog?|cvsignore|\.in$|\.ac$|\.am$|\.#)'`

}

gsrci()

{

grep -iIE "$*" `find . -type f |grep -vE > '(\.lo$|\.TPo$|\.dep$|\.\d$|CVS|\.mk$|Makefile|ChangeLog?|cvsignore|\.in$|\.ac$|\.am$|\.#)'`

}

# For moving a bunch of files, replacing one regex with another

plmv()

{

local sub="s!$1!$2!"

for i in *; do mv "$i" "$(echo "$i" |perl -pe "$sub")"; done

}

# Same as above, but you can specify a prefix for the files

plmvp()

{

local sub="s!$2!$3!"

for i in $1*; do mv "$i" "$(echo "$i" |perl -pe "$sub")"; done

}

# Check what sedmv would do

plchk()

{

for i in *; do echo "$i" |perl -pe "s!$1!$2!"; done

}

# Check what sedmvp would do

plchkp()

{

for i in $1*; do echo "$i" |perl -pe "s!$2!$3!"; done

}

# So I can edit .bashrc and do . !$ without re-updating my $PATH

if [ "`echo $PATH |grep sbin`" = "" ]; then

export PATH=$PATH:/sbin:/usr/local/sbin:/usr/sbin

fi

</code>

Hmm.. so you look at that, and maybe it looks like a bunch of random garbage to you. But maybe after I explain what some of the neater things do, you'll be a bit more interested:

First, set -o vi puts the shell in vi mode. If you're not used to vi, or you're a beginner with Unix, you probably don't want this. If you've used vi for text editing before and you don't mind it, you might want to give it a shot.

Next, we have some aliases. I don't like the typical users command, so I alias my own to provide a nicer list. ll is set up out of the box on some distros, and just gives you a more detailed directory listing. Aliasing vi to vim ensures I always get vim, which has some niceties I prefer over plain vi.

The gsrc functions have definitely saved me lots of times. If you've ever worked on a fairly large development project, you know it can sometimes be a pain to find a certain function, or a certain peice of code you know you've seen, you just can't remember here. With gsrc and gsrci, this is considerably easier. These functions take a regular expression (egrep syntax) and search through all source files under the current directory, recursively. What's better about gsrc and gsrci as opposed to just doing something like grep -Ir regex * is that they attempt to cut out non-relevant files. I care about source, dammit! I don't want autoconf/automake/CVS/whatever files getting in my way, and these functions try to cut out as much of crud as possible. Oh, and the difference between gsrc and gsrci is that gsrci is case-insensitive.

The sedmv and sedchk functions are also pretty cool, IMO. Most likely, you've been in a situation where you've had a bunch of files, all having something in their name. And you want to move them in such a way that that one thing is replaced with something else. So maybe you have some files foo1, foo2, foo3 and you want to move them so that you get foo01, foo02, foo03. With sedmv and sedmvp you can! sedmv acts on all files in the current directory, and replaces the first regex (sed syntax) you give it with the second. So in the example I just gave, I could do sedmv foo foo0 and I'd get what I want. Pretty nice, eh? I think so, anyway. It's definitely saved me a bunch of time before. sedmvp works like sedmv, except that it takes 3 parameters. The first is a prefix of the files you want to work on. So if I had some other files in that directory, and I only cared about moving the foo files, I could do sedmv foo foo foo0. Then files like barfoo wouldn't be moved to barfoo0.

sedchk shows you what the results of a sedmv will be. This is nice, and allows you to (hopefully) avoid screwing up. These functions are really convenient and quite powerful, but they can be a bit dangerous, so it's a good idea to use sedchk or sedchkp first!

Finally, I add the various sbin directories to my path. This makes using sudo a whole lot easier.

Enjoy!