bash

Check if an command was successful in bash

When scripting gigantic build scripts with bash, its important that if anything fails you halt the script.

Heres how:

function checkResult() {
	RESULT=$1
	if [ $RESULT -ne 0 ] ; then
		echo
		echo "!!!!!!!!!!!  Build Failed !!!!!!!!!!!!!!!!!"
		echo
		exit $RESULT
	fi
}

You call it like this:

./someCommandOrScript.sh
checkResult $?
  • Share/Bookmark

bash
code
linux commands
sysadmin

Comments (0)

Permalink

Edit GMail messages using Vim

Ok, why would you want to do this ?

Because you can.

1) Install this add-on which allows you to edit text areas in firefox using an external editor:

https://addons.mozilla.org/en-US/firefox/addon/4125

2) Install VIM

ftp://ftp.vim.org/pub/vim/pc/gvim72.exe

3) Setup the plugin to use vim (addOns->its all text addon->options)

4) make sure gmail is using “plain text” as editing – otherwise its not a text area, but some mad iframe.

6) Happy Days!

  • Share/Bookmark

bash
sysadmin

Comments (2)

Permalink

Read in a list of files and loop over them in bash

Needed to do this and after a bit of looking around (Bash scripting) came up with this:


#!/bin/bash

FILE_LIST_SOURCE=listoffiles.txt
DEFAULT_FILE="some.default.file"

[ -f $FILE_LIST_SOURCE ] && LIST_OF_FILES=$(cat $FILE_LIST_SOURCE)

LIST_OF_FILES=${LIST_OF_FILES:-$DEFAULT_FILE}

#echo -e "\\nLIST_OF_FILES:\\n$LIST_OF_FILES"

echo "List of files:"
for i in $LIST_OF_FILES; do
    echo $i
done


  • Share/Bookmark

bash
sysadmin

Comments (0)

Permalink

Python!

Wanted to find all files which contained a specific string and then extract a part of the line we were interested in.

First the GREP:

grep -R --include=*.vm  * > results.txt

then run this python script:

import re
for line in open('editablecomponents.txt'):
	match = re.search(r'#editableComponent\("(.*?)"', line)
	if match:
		print match.group(1) + "Display.vm"
  • Share/Bookmark

bash

Comments (0)

Permalink

Sweet Search And Highlight Command

Been doing quite a bit of manipulating LaTeX files on the command line recently, and have a very sweet little command for searching for text in multiple files:

grep -r Hoffman */*.tex | less +/Hoffman

Where “Hoffman” is the search term which can of course be any regular expression.

The “less” command has a very convenient argument “+” which allows you to pass a search term in and will automatically highlight it for you.

The -r in grep recurses directories and */*.tex looks for all tex files. the | pipes the result to less.

sweet.

  • Share/Bookmark

bash
latex
linux commands
os x
regex

Comments (0)

Permalink