Author: ronald

  • Bash Scripting Basics (Variable Assignment, If, Loop)


    #!/bin/bash
    ##################################################
    # Benchmark the processing time when wkhtmltopdf
    # converts an html file to a pdf file
    ##################################################

    if [[ $1 = "" || $2 = "" || $3 = "" ]]; then
    echo "Usage: `basename $0` sourceFile.html destinationFile.pdf iteartionRunTime"
    else
    _min=9999
    _max=0
    _total=0
    _iteration=$3
    for (( _i=1; _i<=$_iteration; _i++ )) do _start_time=`date +%s` echo "running $_i" ~MYUSER/bin/benchmarkWkhtmltopdf.sh $1 $2 > /dev/null 2>&1
    _end_time=`date +%s`
    _processing_time=$((_end_time-_start_time))
    if [ "$_processing_time" -lt "$_min" ]; then
    _min=$_processing_time
    fi
    if [ "$_processing_time" -gt "$_max" ]; then
    _max=$_processing_time
    fi
    _total=$(($_total+$_processing_time))
    done
    _processing_time_average=$(echo - | awk "{print $_total/$_iteration}")
    echo "Processing time avg: $_processing_time_average min:$_min max:$_max total:$_total"
    fi

  • Compress and Extract Files in Linux

    Extract:

    .tar.bz2
    To extract a tar (Tape ARchiver) file, type the following in the shell prompt:

    tar xvjf yourFileName.tar.bz2

    Which will untar it to the current directory. Depending on the file structure that being compressed, it might create sub-directories.
    Parameters:
    x – extract
    v – verbose output (lists all files as they are extracted)
    j – deal with bzipped file
    f – read from a file, rather than a tape device

    .tar.gz or .tgz

    tar xvzf file.tar.gz
    tar xvzf file.tgz

    Parameters:
    -x, –extract, –get extract files from an archive
    -v, –verbose verbosely list files processed
    -z, –gzip, –gunzip, –ungzip filter the archive through gzip
    -f, –file=ARCHIVE use archive file or device ARCHIVE

    .bz2 file

    tar jxf YOUR_FILE_NAME.bz2

    .zip file

    unzip YOUR_FILE_NAME.zip

  • Using VBA macro for Excel

    Let’s start with something simple, assume that we have a big spread sheet and we want to highlight it based on a certain subject. For example expenses about car. For the sake of the example we’ll keep it short, but in real life you could be dealing with hundreds of rows.
    Excel challange 1

    We can see that cell B2 and B7 contains the word “car”, so how you do you highlight these cells using an excel formula? You can’t! The only way to do this is with a VBA macro. VBA macro is much more robust than excel formulas – in essence VBA is a programming for Excel.

    So let’s start programming!

    1. Enable the VBA development environment. In your Excel, press Alt + F11 . If that doesn’t work:

    • Go to File > Options > Customize > Ribbon
    • In the main tabs. Go to “Customize The Ribbon” > check “Developer”
    • Click on the “Visual Basic” icon.
    • A new window will open saying “Microsoft Visual Basic for Applications”

    Double click on “Sheet 1”. Then at the top menu go to “Insert” > “Procedure”. Give it a meaningful name “HighlightCarExpenses”


    Public Sub HighlightCarExpenses()
    Rem this is a comment
    Dim emptyCounter, currentRow As Integer
    Dim currentCell As Range
    Dim currentCellValue As String

    Rem Set initial value for the empty cell counter
    emptyCounter = 0
    currentRow = 1
    currentCellValue = ""

    Rem Scan al column B values
    Rem Stop scanning if we see 3 empty cell in column B
    Do While (emptyCounter < 3) currentCellValue = ActiveSheet.Cells(currentRow, 2).Value Debug.Print "Value at row " & currentRow & ": " & currentCellValue If (currentCellValue = "") Then emptyCounter = emptyCounter + 1 ElseIf InStr(1, currentCellValue, "car", 1) Then Debug.Print "Car is found at row: " & currentRow ActiveSheet.Cells(currentRow, 2).Interior.Color = RGB(256, 0, 0) End If currentRow = currentRow + 1 Loop Debug.Print "last row" & currentRow End Sub

    Glossary

    Comment: Part of the code (sentences) that will not be executed by the system. This can be a way for the user/programmer to put a note to her/himself
    Debug: Trace a value(s) to find and remove mistake, or just to ensure program correctness.
    Dim: A declaration for variable
    Integer: A data type that is numerical. Example: 1, 2, 3, etc
    String: A data type that is alpha-numerical. Example: "I have 2 dogs", "Expenses are bad", etc. Be aware that a string is always written within double quotes (")

  • Remote copy using scp

    Do you ever need to copy one file from one server to another? If you have ssh access to the remote server then you can do an scp command like so:


    $ scp remoteUsername@remoteServername:remoteFile localFile

  • Delete files older than X number of days in Linux

    Say you backup your database daily and you need to delete the old backup files.
    You can issue the command bellow which will erase any files within the specified directory that are older than 7 days.


    $ find /your/target/directory* -mtime +7 -exec rm {} \;

  • DSQUERY

    dsquery * “CN=Users,DC=myadserver,DC=com” -scope onelevel -attr objectguid proxyaddresses -limit 2000 >C:\myadserver.user.list.txt

  • Calling a mysql query from linux or dos command line

    There are times when you want to call a mysql command from the bash or dos script then call this script in a scheduler (cron job).
    The example below will show you how to do so.

    mysql -uYOURUSERNAME -pYOURPASSWORD -DYOURDATABASE -e"CALL YOURSTOREDPROCEDURE('YOUR_SP_PARAMETER')"

    Explanation:
    -u is where you put your username, you can either put a space or no space at all after the -u (both works). As you can see I prefer not to put any space.
    -p is where you put your password.
    -D is your database name. That’s a capital D.
    -e this is where your query will go. It needs to be quoted (“). If it’s calling a stored procedure or stored function, you’ll need to use the CALL keyword, otherwise if it’s just a simple query don use the CALL keyword.

  • Remove all svn directory

    Subversion (SVN) sometimes clutter our source directory by creating .svn folder in each folder or sub folder that we version control.

    The easiest way to erase them is by using a bash command:


    find . -name ".svn" -type d -exec rm -rf {} \;

  • Adding New VirtualHost in Apache2

    On linux Ubuntu
    1. If your apache has /etc/apache2/sites-available directory then just create a new file “dev.newwebsite.com.conf”
    with the following content:


    ServerName dev.newwebsite.com
    DocumentRoot "/var/www/www.newwebsite.com"

    # Error handlers
    ErrorDocument 500 /errordocs/500.html


    AllowOverride All
    Options -Indexes FollowSymLinks
    Allow from all


    2. Also copy the same file into /etc/apache2/sites-enabled

    3. Restart your apache

    $ /etc/init.d/apach2 restart

    4. Edit your hosts file: /etc/hosts
    add the following line to it
    127.0.0.1 dev.newwebsite.com

    5. Open your web browser and open http://dev.newwebsite.com

    If you are a windows user and are using xampp then you need to alter the vhost configuration file.
    The default xampp file is in
    “\xampp\apache\conf\extra\httpd-vhosts”
    Then restart your apache

  • Memcache test connection script


    connect('127.0.0.1', 11211) or die ("Could not connect");

    // Add it to memcached server
    // The parameters are: KEY, VALUE, USE COMPRESSION, EXPIRY IN SECONDS
    $memcache->set('MyKey1', 'The value of My Key1 is me', false, 100);

    echo $memcache->get('MyKey1');
    // It will show you: The value of My Key1 is me
    ?>