Dark Launch

This is a Dark Launch.

Kill screen session using command line & bash

To kill an active screen, use the following command:

Code
$ screen -S myscreen -X quit
 

To use this in a bash script, use the following:

Code
# Kill screen if running
sessionname="myscreen"
if [ $(screen -list | grep "$sessionname" | wc --lines) -ge 1 ]; then
    echo "screen session $sessionname is running"
    screen -S "$sessionname" -X "quit"
fi

Enable HTTP Basic Authentication / Authorization with Django & mod_wsgi

To enable Basic Authentication using Django and mod_wsgi, do the following:

Add WSGIPassAuthorization to apache configuration file ( /etc/apache2/sites-available/mysite.com )

Configure this WSGIPassAuthorization On so that mod_wsgi will pass the HTTP Authentication headers to your Django application so your app will be able to authenticate the requests.

Code
<VirtualHost *:80>
    WSGIPassAuthorization On
    WSGIScriptAlias / /home/www/mysite.com/wsgi.py
</VirtualHost>
 

Restart apache and the now the HTTP_AUTHORIZATION environment variable will be available in the django request.

Bash Increment Counter Integer Variable

To increment a counter in bash, use any of the following:

Code
#!/bin/bash
 
set -x
 
x=1
echo $x
 
(( x++ ))
echo $x
 
(( x += 1 ))
echo $x
 
let x++
echo $x
Code
+ x=1
+ echo 1
1
+ ((  x++  ))
+ echo 2
2
+ ((  x += 1  ))
+ echo 3
3
+ let x++
+ echo 4
4
 

Python touch file

To touch a file in python, do the following:

Python
import os
 
def touch(fname, times=None):
    """
    touch
    """
    with file(fname, 'wa'):
        os.utime(fname, times)
 
if __name__ == '__main__':
    for i in range(0, 10):
        filename = 'somefile{i}.txt'.format(i=i)
        print filename
        touch(filename)

Vim Replace 2 spaces indent with 4 spaces

To replace all 2 space indentations, use the following command:

Code
:%s/\n \{2\}\([^ ]\)/\r    \1/gc
 

Explained:

Code
: - command
%s - entire selection
/ - separator
\n \{2\}\([^ ]\) - search for a newline and 2 spaces followed by a character that is not a space
/ - separator
\r    \1 - replace with a newline and the matching group that is a not a space
/ - separator
g - global replace
c - confirm replacements (type y (yes) to confirm replacements)