Bash - Upgrading lots of git repos.

16. August 2013 19:33

 

Here is a nice example of how to execute a git command on multiple git repositories one after another. Assuming that they are all in the same directory.

 

#!/bin/bash

for i in * ; do
	if [ -d $i ] ; then
		if [ -d $i/.git ] ; then
			echo Updating $i ....
			(cd $i ; git pull)
			echo
		fi
	fi
done

E-mail Kick it! DZone it! del.icio.us Permalink


Linux - Killing all processes for a specific user

13. July 2012 08:00

 

Here is a few methods for killing tasks for a specific user in linux which may be required during account deletion or because somebody has managed to be an idiot and locked himself out with a fork bomb or some such.

 

The simple method is to use the utility called 'slay' which for debian / unbuntu and most other distrobutions is avilable if its not install you can install it using 'apt-get install slay'

 

It is very simple to use. Just running the command slay <username> and it will kill all of that users processes.

 

 

The other method to use when slay is not avilable is a combination of ps and kill. You can use the following command

 

 

kill -TERM `ps h --User nobody -o pid`

 

 

Understanding the above can other advantages as well because you can switch out the --User for --Group and kill processes by group id as well as for a specific user.

E-mail Kick it! DZone it! del.icio.us Permalink


Doing for loops in bash

9. May 2012 23:21

 

It can be commonly known that you can do for loops in bash by doing something like for i in * ; do echo $i ; done or some such. This will loop for every file in the current directory. Though typical for loops for a specific number range can be a little more difficult since you need to form the data to be able to execute the loop.

 

To get a simple for loop to work we can copy python's for i in range(x, y): type of loop since the bash for loop is exactly the same as this. Both bash / python perform a for each loop around a list of data items rather than the traditional for loop with a counter. This can be used to our advantage since all you need to do is create a small program that generates this data lists.

 

The following c program does this.

 

#include <stdio.h>
#include <stdlib.h>

void print_usage(FILE *fp, char *app) {
    fprintf(fp, "Usage: %s <start number> <end number>\n", app);
    fprintf(fp, "\n");
}

int main(int argc, char **argv) {
    int a = 0, b = 0;
    int i;

    if (argc < 3) {
        print_usage(stderr, argv[0]);
        exit(EXIT_FAILURE);
    }

    a = atoi(argv[1]);
    b = atoi(argv[2]);
    
    if (a >= b) {
		int tmp = a;
		b = a;
		a = tmp;
    }

    for(i=a;i<=b;i++) {
        printf("%d\n", i);
    }

    return 0;
}

All you need to do is put the program above into a c file and compile it with gcc (gcc -Wall range.c -o range) and place the executable on the path (eg in $HOME/bin). Then you can do for loops in the bash shell the following way.

 

for i in `range 0 20` ; do echo $i ; done

The above will of course produce the output of 0 to 20 on the terminal when run.

 

E-mail Kick it! DZone it! del.icio.us Permalink


Color Coding the bash prompt

16. January 2012 07:00

 

Here is a little tip so that you can quickly tell which linux machine that you are currently logged into. The easy way to do this is to colour code the shell prompt.

 

In bash the PS1 enviroment variable controls how the prompt is formatted. On a debian squeeze install it would be set to something like this by default

 

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

 

That of course does look somewhat confusing mostly because of the escaping. The letters with a '\' at the front are telling bash to also display certain things but I will cover that in another post. Lets add some colour.

 

To add some colour we need to add a control sequence to the shell prompt this tells the terminal to enable a specific colour then at the end of the prompt turn that colour back to the default for the terminal.

 

to set the colour we need to apply this to the beginning of the enviroment variable (this is using the color red 31m)

 

\[\033[31m\]

 

Then we also need to append the close sequence to the end of the enviroment variable.

 

\[\033[0m\]

 

So the complete enviroment variable should now look like this.

 

export PS1="\[\033[31m\]\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$\[\033[0m\]"

 

Our prompt will be red. To change the colour you only need to change the starting sequence and choose from one of the following colours.

 

Black 0;30     

Blue 0;34     

Green 0;32     

Cyan 0;36     

Red 0;31     

Purple 0;35     

Brown 0;33     

Light Gray 0;37     

 

Dark Gray 1;30

Light Blue 1;34

Light Green 1;32

Light Cyan 1;36

Light Red 1;31

Light Purple 1;35

Yellow 1;33

White 1;37

 

E-mail Kick it! DZone it! del.icio.us Permalink