Regular Expression to minimum(non-greedy) matching

If we would like to match multiple duplicate content in the same line, we would like to use minimum match by using RE.

for example:

filename: text

pillar[‘abc’]
pillar[‘abc’]
pillar[‘abc’]}pillar[‘wx’]||pillar[‘yz’]
pillar[‘abc’]

1. matched expression python

import re

f=open('./text','r').read()
# non-greddy result:

print(re.findall('(pillar\[.*?\])',f))

# ourput: ["pillar['abc']", 
"pillar['abc']", 
"pillar['abc']", 
"pillar['wx']", 
"pillar['yz']", 
"pillar['abc']"]

# greedy match result:

print(re.findall('(pillar\[.*\])',f))

#output: ["pillar['abc']", 
"pillar['abc']", 
"pillar['abc']}pillar['wx']||pillar['yz']", 
"pillar['abc']"]

 

2. matched expression using shell

grep -o -P "pillar\['.*?'\]" text

# output: 
pillar['abc']
pillar['abc']
pillar['abc']
pillar['wx']
pillar['yz']
pillar['abc']

Nginx auxiliary server shcadule[shell scripts]

 

Recently, we are planning to use two nginx server for load balancing’s hot backup. Here is the scripts I wrote that used to switch virtual IP automatically for servers when either one of the two servers got crash.

#!/bin/bash                                                                                

# This scripts is used to check nginx status and startup automatically.
# Written by Libo.ma @ Oct 20, 2011 17:16

Ver=0.9 Continue reading "Nginx auxiliary server shcadule[shell scripts]" 

LVM Using snapot to backup data–from google searching..

This is a Daily script

#!/bin/bash
DIR=/root/backup
BACKUP_CMD=backup-lv.sh

VG=/dev/vg1 #whichever folder your xen files located at)
LV[0]=xen-1 # (name of diff, xen machines/guests)
LV[1]=xen-2
LV[2]=xen2-data #xen2 is a web server with large amount of data
LV[3]=xen-3

cd $DIR
COUNT=0
CAT_STR=""
while [ $COUNT -lt ${#LV[@]} ]
do
./$BACKUP_CMD $VG/${LV[$COUNT]} &> last-backup-${LV[$COUNT]}
CAT_STR="${CAT_STR} last-backup-${LV[$COUNT]}"
MOO=$((COUNT++))
done

ls -lh /mnt/system_backups/ &> last-backup-ls
cat $CAT_STR last-backup-ls > last-backup.txt
echo "daily backup report attached" | mutt -s "$HOSTNAME daily backup report" -a last-backup.txt MyEmail@MyCompany.com

Continue reading “LVM Using snapot to backup data–from google searching..”

Passing an indirect reference to a function.

 #!/bin/bash
 # ind-func.sh: Passing an indirect reference to a function.

 echo_var ()
 {
 echo "$1"
 }
message=Hello
Hello=Goodbye

echo_var "$message"        # Hello
# Now, let's pass an indirect reference to the function.
echo_var "${!message}"     # Goodbye

echo "-------------"

# What happens if we change the contents of "hello" variable?
Hello="Hello, again!"
echo_var "$message"        # Hello
echo_var "${!message}"     # Hello, again!
 exit 0

This function can able to use for dictionary (key-value)creating.

for instance:
Continue reading “Passing an indirect reference to a function.”