BASH tricks

I like using the console, I’m kinda not a gui person. This is funny as I’m not all that great at using it yet. But I’m learning and trying. So I thought I’d share any odd tips and tricks I might learn in case they are of use to others. BTW, some of these rely on having a personal ~/bin directory, if you create one, you can keep scripts and stuff there. Some distributions have this included in the default PATH settings, but CentOS doesn’t seem to, so I add this line to my ~/.bashrc (which you should make if you don’t have one).

# add my personal script dir
export PATH=$PATH:~/bin

Highlighting build errors

This will highlight anything that comes through stderr rather than stdout. I found it kinda helpful as a visual queue that there was an error or warning in a build. I wrote it months ago so I’m a bit fuzzy on the first line there. I’m pretty sure it simply does a shuffle of the streams and redirects them round till the stdout just dumps to console like stderr would normally and stderr is redirected back to act as stdout and is then piped into a little subshell that adds colour coding. Anyway, here it is:

($* 3>&1 1>&2 2>&3 3>&-) |
read input
while [ “$input” ]
echo -e “\E[31m$input\E[0m”
read input

Emacs/Vi in the shell

I discovered that due to the input processing library used by the shell interpreters in Linux, they support some basic input commands from both emacs and vi. By default most of the Linux based shells are in emacs mode and you can use emacs movement commands to move back and forth through the line and the history, edit a command (like deleting words etc) and even to search through history and stuff. To switch to vi mode (I tend to use vi mostly) you can type in set -o vi and it changes the shell into vi mode. So with vi you get a modal shell. This is kinda cool and kinda confusing. By default you are in normal shell mode, but if you hit or -C you enter vi mode and can now use vi movement and editing commands. To switch a shell back to emacs mode, you can use set -o emacs. There are also environment variables you can set in your .bashrc, or you can put the set lines there.

Bulk renaming

So I changed the name of a module, it was originally going to be split into two modules and bundled into a container to hide the other modules. During building, I decided not to do this and merged it all into one again. But I had already named the module with an eye to the container, meaning that I needed to rename all the references to the old module name and replace them with the new ones. Plus all the file names needed to ideally be replaced as well.

Long story short, the first task I accomplished with this line:

perl -e "s/ADC_HM/ADC/g;" $(grep -ilr ADC_HM * | sed -e '/.svn/d')

This command saves the original file with a .save extension in case you need it later, this can be skipped by omitting the .save after the -pi. The second part I did with this:

find . -name ADC_HM* | sed -e '/.svn/d' | sed 's/\(.*\)ADC_HM\(.*\)/svn move \1ADC_HM\2 \1ADC\2/' | sh

This was an svn checked out source so I used the svn move command to rename the files. Note that although svn is called for each file being renamed it doesn’t check each change in individually until you do a svn ci afterwards, that way you can check that it worked properly before committing it. You could add an svn ci at the end of each line by adding it to the sed command, but that would just clog up the svn log and boost your commit numbers … (hmmmmm).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s