Author: admin

  • Convert svn repository to git

    mkdir ~/Sites/repo_tmp
    cd ~/Sites/repo_tmp
    git-svn init https://svn.example.com/repo/trunk/ --no-metadata
    git config svn.authorsfile ~/Sites/git-users.txt
    git-svn fetch
    git-svn show-ignore > .gitignore
    git add .gitignore
    git commit -m "Convert svn ignored files to .gitignore"
    cd ~/Sites
    git clone repo_tmp repo

  • use grep for either of two strings

    Options to look for several reg exp with grep. -i for case insensitive and -E for reg exp.

    grep -iE "(jpg|gif)" your-file-with-text.txt

  • Bash menu in bash with getopts

    A little more sophisticated script than the former bash menu example to get some arguments from command line.

    #!/bin/bash

    while getopts "u:p:" opt; do

    case $opt in
    u)
    echo "-u was triggered, Parameter: $OPTARG"
    dbuser="$OPTARG"
    ;;
    p)
    echo "-p was triggered, Parameter: $OPTARG"
    dbpass="$OPTARG"
    ;;
    \?)
    echo "Invalid option: -$OPTARG"
    exit 1
    ;;
    :)
    echo "Option -$OPTARG requires an argument."
    exit 1
    ;;
    esac

    done

    # Clear all options and reset the command line
    shift $(( OPTIND -1 ))

    # First parameter
    if [ -z "$1" ]; then
    echo "usage: $0 [-u name] [-p password] file"
    exit
    fi

  • Shell script for domain searching binge

    This is an easy script to find out if a domain is available. It works with generic tld.

    #!/bin/bash
    while read i; do
    echo -n "$i is ";
    whois $i | grep -F "$(echo -e 'No match\nNOT FOUND\n AVAIL')" -q && echo 'AVAILABLE! :D' || echo 'taken :(';
    done

  • How to rename a mysql database or table

    Here is a dirty script to rename a mysql database or table from command line. Dont for get to stop mysql service!

    cd /var/lib/mysql/
    /etc/init.d/mysql stop
    # rename database...
    mv olddbname newdbname
    # ...or table
    cd database/
    mv oldname.frm newname.frm
    mv oldname.MYD newname.MYD
    mv oldname.MYI newname.MYI
    /etc/init.d/mysql start

  • Find latest file in a directory methods

    Sometimes there are so many files in a directory that it’s hard to find the last file in the directory. We show you here 2 ways for finding it out.

    With awk we print the last argument of the last line:

    $ ls -lrt | awk '{ f=$NF }; END{ print f }'

    Reverse ordering with ls and then getting the first line:

    $ ls -t1 | head -n1

  • how to batch file rename in bash shell

    This is a quick way of renaming a batch of files matching a reg exp in bash.

    ls foo*.jpg | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2' | /bin/sh

  • Vars expansion

    Some examples of how to work with variables in bash.

    ### Vars expansion with ${} ###

    ## Default value
    # If a variable is not set a default value will be used. If it's set,
    # its value is used
    echo ${NAME:-Pepe}
    # -> Pepe
    NAME="Juanje"
    echo ${NAME:-Pepe}
    # -> Juanje

    ## Assign a default value
    # Assign a value to a var, only if it already has one
    NAME=""
    echo $NAME
    # ->
    echo ${NAME:=Pepe}
    # -> Pepe
    echo $NAME
    # -> Pepe
    echo ${NAME:=Juanje}
    # -> Pepe

    ## Show an error if variable does not exist (some vars exists but are empty)
    # you can set a customized message
    echo ${X?}
    # -> -bash: X: parameter null or not set
    echo ${X?La variable X no exite}
    # -> -bash: X: La variable X no exite
    X=""
    echo ${X?La variable X no exite}
    # ->
    X="Algo"
    echo ${X?La variable X no exite}
    # -> Algo

    ## Use an alternative value, if var already exists and has a value
    echo ${Y:+Alternative value}
    # ->
    Y=""
    echo ${Y:+Alternative value}
    # ->
    Y="Some value"
    echo ${Y:+Alternative value}
    # -> Alternative value

    ## Substrings
    # :{start}:{size}
    # If no size specified, a substring from {start}, to end
    # first char is 0
    TEXT="Un texto de ejemplo"
    echo ${TEXT:3}
    # -> texto de ejemplo
    echo ${TEXT:3:5}
    # -> texto

    ## Substring substracting from beginning
    # With one # first ocurrence of what comes after # will be substracted
    B="blablabla..."
    echo ${B#bla}
    # -> blabla...
    echo ${B#*bla}
    # -> blabla...
    # With 2 # longer strings will be substracted
    echo ${B##bla}
    # -> blabla...
    echo ${B##*bla}
    # -> ...
    # Another example:
    D="/srv/chroot/var/chroot/etc/apache"
    echo ${D#*chroot}
    # -> /var/chroot/etc/apache
    echo ${D##*chroot}
    # -> /etc/apache

    ## Substring from end
    # With one % first ocurrence of what comes after # will be substracted
    B="blablabla...blablabla"
    echo ${B%bla}
    # -> blablabla...blabla
    echo ${B%bla*}
    # -> blablabla...blabla
    # With 2 % longer strings will be substracted
    echo ${B%%bla}
    # -> blablabla...blabla
    echo ${B%%bla*}
    # ->
    # Example:
    D="/srv/chroot/var/chroot/etc/apache"
    echo ${D%chroot*}
    # -> /srv/chroot/var/
    echo ${D%%chroot*}
    # -> /srv/

    ## Variables names starting with a prefix
    echo ${!U*}
    # -> UID USER
    echo ${!B*}
    # -> B BASH BASH_VERSINFO BASH_VERSION
    echo ${!BASH*}
    # -> BASH BASH_VERSINFO BASH_VERSION

    ## Number of chars of variable value
    X="Un texto cualquiera"
    echo ${#X}
    # -> 19
    N=22435
    echo ${#N}
    # -> 5

    # Sustituir una cadena
    a=/etc/kung/foo
    echo ${a/foo/fu}
    # -> /etc/kung/fu

  • Bash cron job for ipfw

    With this lines in a cronjob,usually (/etc/cron.d/yourcronjob) in gnu/linux, you can add some rules to your firewall so no one can go out of your lan in the intervals specified, and then back to normal.

    In this case, ip 10.0.0.4 would be blocked.

    0 21 * * 0-4 /sbin/ipfw add 1 deny all from 10.0.0.4 to any >/dev/null 2>&1
    0 21 * * 0-4 /sbin/ipfw add 2 deny all from any to 10.0.0.4 >/dev/null 2>&1
    0 5 * * 1-5 /sbin/ipfw del 1 >/dev/null 2>&1
    0 5 * * 1-5 /sbin/ipfw del 2 >/dev/null 2>&1

  • Shell script menu

    This is an example of how to create a bash menu using while and read.

    #!/bin/sh
    echo "Use one of the following options"
    echo " d : run date command"
    echo " : "
    echo " q: quit this menu"
    echo "enter your choice or type "q" to quit : \c"
    read option
    while [ "$option" != "q" ]
    do
    case $option in
    d ) date
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    )
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;

    # ...

    * )
    echo "invalid choice!"
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    q ) exit 0
    ;;
    esac
    done
    exit 0