Bash Scripting Basics (Variable Assignment, If, Loop)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/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
Posted in Bash, Linux | Leave a comment

Compress and Extract Files in Linux

Extract:

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

1
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

1
2
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

1
tar jxf YOUR_FILE_NAME.bz2

.zip file

1
unzip YOUR_FILE_NAME.zip
Posted in Bash, Linux | Tagged | Leave a comment

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”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 (“)

Posted in Visual Basic | Leave a comment

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:

1
$ scp remoteUsername@remoteServername:remoteFile localFile
Posted in Linux, Operating System | Tagged | Leave a comment

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.

1
$ find /your/target/directory* -mtime +7 -exec rm {} \;
Posted in Bash, Linux, Operating System | Leave a comment

DSQUERY

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

Posted in LDAP and Active Directory | Leave a comment

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.

1
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.

Posted in Database, Linux, MySQL | Leave a comment

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:

1
find . -name ".svn" -type d -exec rm -rf {} \;
Posted in Linux, Version Control | Leave a comment

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost *:80>
    ServerName dev.newwebsite.com
    DocumentRoot "/var/www/www.newwebsite.com"

    # Error handlers
    ErrorDocument 500 /errordocs/500.html

    <Directory "/var/www/www.newwebsite.com">
        AllowOverride All
        Options -Indexes FollowSymLinks
        Allow from all
    </Directory>   
</VirtualHost>

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

3. Restart your apache

1
$ /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

Posted in Operating System, php, Ubuntu | Leave a comment

Memcache test connection script

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
error_reporting(E_ALL);
$memcache = new Memcache;
// Connect to memcached server
$memcache->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
?>
Posted in php, Web Development | Leave a comment