Monday 15 January 2018

Creating extract custom function to extract files

As there are different file formats present for archiving and compressing files using different utilities. So, to make a uniform function for extracting those files can be helpful. Add the below piece of code in your .bash_profile or .profile file present in home directory to extract the data.



extract () {
        if [ -f $1 ] ; then
           case $1 in
                *.tar.bz2) tar xvjf $1 ;;
                *.tar.gz) tar xvzf $1 ;;
                *.bz2) bunzip2 $1 ;;
                *.rar) rar x $1 ;;
                *.gz) gunzip $1 ;;
                *.tar) tar xvf $1 ;;
                *.tbz2) tar xvjf $1 ;;
                *.tgz) tar xvzf $1 ;;
                *.zip) unzip $1 ;;
                *.Z) uncompress $1 ;;
                *.7z) 7z x $1 ;;
                *) echo "don't know how to extract '$1′…" ;;
            esac
        else
                echo "'$1′ is not a valid file!"
        fi
}
 

Saturday 19 August 2017

Understanding the execution of if-else block using fork

Here is a brief explanation on how both if/else gets execute in case of forking and piping example. Consider the below example:

[code]
#include <stdio.h>
#include <unistd.h>
int main() {
  pid_t pid;
  pid = fork();
  if(pid == 0) {
    printf("Hi Coding-Scripting");
  }
  else {
    printf("Bye.! Coding-Scripting");
  }
  return EXIT_SUCCESS;
}
[/code]

Explanation:

Whenever the call to fork() function is made, it replicates the whole code written down below the fork. So, now the two processes are created child process, with PID =0, and parent process with PID = X. So, the child execute the if block and exits, and after that parent executes the else block and exits. In this manner, both the if-else blocks will get execute.

[firefox] View Source in your favorite editor



To view source code of any website in your favorite source editor:
You can do this by changing 2 hidden preferences.
- Type about:config into the location bar and press enter.
- Accept the warning message that appears, you will be taken to a list of preferences
- In the filter box type view_source
- Double-click on view_source.editor.external to change its value to true.
- Double-click on view_source.editor.path and set it to C:\Program Files\Notepad++\notepad++.exe (if Notepad++ is stored elsewhere change that to where it is stored or you can choose any editor of your preference)

Cheers,

Friday 11 August 2017

PHP script to convert string into 1337 characters

A very basic PHP code, for leet users, that will convert your string into leet characters.



[php]
<?php
/**
* PHP code to convert your string to leet characters.
*/
$input =  strtolower($argv[1]);
$output = "";
$count = strlen($input);
for ($i=0;$i<$count;$i++) {
  switch ($input[$i]) {
    case "a" : $output .= "4";
               break;
    case "b" : $output .= "8";
               break;
    case "c" : $output .= "(";
               break;
    case "d" : $output .= "|)";
               break;
    case "e" : $output .= "3";
               break;
    case "f" : $output .= "|\"";
               break;
    case "g" : $output .= "6";
               break;
    case "h" : $output .= "|-|";
               break;
    case "i" : $output .= "1";
               break;
    case "j" : $output .= "_/";
               break;
    case "k" : $output .= "|<";
               break;
    case "l" : $output .= "|_";
               break;
    case "m" : $output .= "|\\/|";
               break;
    case "n" : $output .= "|\\|";
               break;
    case "o" : $output .= "0";
               break;
    case "p" : $output .= "|*";
               break;
    case "q" : $output .= "0_";
               break;
    case "r" : $output .= "12";
               break;
    case "s" : $output .= "5";
               break;
    case "t" : $output .= "7";
               break;
    case "u" : $output .= "|_|";
               break;
    case "v" : $output .= "\\/";
               break;
    case "w" : $output .= "\\/\\/";
               break;
    case "x" : $output .= "><";
               break;
    case "y" : $output .= "`/";
               break;
    case "z" : $output .= ">_";
               break;
    case " " : $output .= " ";
               break;
    default : $output .= $input[$i];
              break;

  }
}
echo $output;
?>
[/php]

Firefox shortcuts to make your life easier

Search in your opened tabs, enter the % in URL bar



Find Links As You Type ' (apostrophe)
Find Text As You Type /
Open Manage Bookmarks Window Ctrl+B
Add Page to Bookmarks Ctrl+D
Reload Ctrl+R
Force Reload (not from cache) Ctrl+Shift+R
Location Bar Ctrl+L
Move to Next/Previous Link or Form Element in a Web Page Tab/Shift+Tab
Full Screen (toggle) F11
Zoom Text Smaller Ctrl+- (minus sign)
Zoom Text Larger Ctrl+= (plus sign)
View Page Source Ctrl+U

Introduction to applescript

Hi Everyone,

Lets start with some intro of apple script.



Open your apple script editor and enter the text below.

[code]
display dialog "Hello, coding-scripting!"
[/code]

compile and run, a display box will appear with text "Hello, user".

Now to listen to your name, you need to enter below code

[code]
Say "Hello, coding-scripting!"
[/code]

Cool, isnt it.

Now to repeat any command (looping), you have to use repeat command, use the below code, to display the dialog box twice.

[code]
repeat 2 times
display dialog "Hello, coding-scripting!"
end repeat
[/code]

To produce a beep, you need to use beep command.

[code]
beep
[/code]

To beep multiple times, just enter the number (the time you want to produce beep)

[code]
beep 5
[/code]

To set a variable, you need to use set command.

[code]
set myName to "coding-scripting!"
display dialog myName
[/code]

Okay, now how to concatenate the text, we need to use & operator

[code]
set myName to "coding-scripting!"
display dialog "Hello, " & myName
[/code]

Displaying a text box, to grab input from user.

[code]
set myName to the text returned of (display dialog "Enter your name." default answer "" buttons {"Enter"})
display dialog "Hello, " & myName & ". Welcome to coding-scripting!"
[/code]

Mathematical operations for variables :

[code]
set number1 to 5
set number2 to 5
set number3 to number1 + number2
display dialog number3
display dialog "Thanks for reading my tutorial."
[/code]

Creating loop and displaying multiple messages :

[code]
set myMessage to {"Hello user!", "Welcome to coding-scripting!"}
set x to 1
repeat 2 times
    display dialog (item x of myMessage)
    set x to (x + 1)
end repeat
[/code]

While and if else structure

[code]
set a to true
set b to 0
repeat while x
    if a > 5 then
        set b to false
    else
        set b to b + 1
    end if
end repeat
[/code]

Thanks for reading my tutorial.

Compare two files for duplicate entries using bash and php

Hi Friends,

Need to compare two files and searching for different entries in them. Here is the solution.

Create a new file named cmp.sh and put it in a folder.



[code]
#!/bin/bash
uniq temp1 > sort_temp1
sort sort_temp1 > sort1.txt
uniq temp2 > sort_temp2
sort sort_temp2 >> sort1.txt
sort sort1.txt > sort_dup.txt
cat sort_dup.txt | uniq > sort.txt
echo "Elements present in file temp1 not in file temp2"
php index.php 1
echo "Elements present in file temp1 not in file temp2"
php index.php 2
[code]
Save it.

Now, we need to create a php file named index.php in same folder.
[php]
<?php
$handle = @fopen("sort.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);

//======================================
if($argv[1] == 1)
{
$handle1 = @fopen("sort_temp1", "r");
if ($handle1) {
$test = 1;
while (!feof($handle1)) {
$buffer1 = fgets($handle1, 4096);
if ($buffer==$buffer1)
$test = 0;
}
if ($test == 1)
echo $buffer;
}
fclose($handle1);
}

if($argv[1] == 2)
{
$handle2 = @fopen("sort_temp2", "r");
if ($handle2) {
$test = 1;
while (!feof($handle2)) {
$buffer2 = fgets($handle2, 4096);
if ($buffer==$buffer2)
$test = 0;
}
if ($test == 1)
echo $buffer;
}
fclose($handle2);
}
//========================================

    }
    fclose($handle);
}
?>
[/php]
Save the file in same folder. Add your both files need to compare, with the name temp1 and temp2.

Run the cmd.sh script and it will give you the desired result.

Hope it helps.