Wednesday, 16 January 2019

Essential Shell Commands

Sunday, 9 April 2017

How to increase Virtual Box Hard Disk Size

Here is what I've fixed the issue. 

Initially I created Virtual Box of 15GB of space. 

When I run into out-of-space issue. I did following. 

Two steps involved to resize existing virtual disk space. 

1) Using VBoxManage utility tool provided by Oracle VirtualBox ;  Allocate enough space based on requirement. In my case I've increased from 15GB to 25GB. 

I did this; 

Checked if this present ; 

"C:\Program Files\Oracle\VirtualBox\VBoxManage"

Then Run- CMD , to set environment variable
set PATH=%PATH%;C:\Program Files\Oracle\VirtualBox
Go to path where you have installed VirtualBox. My case
C:\Users\ssankar\VirtualBox VMs\ssankar-ubuntu>VBoxManage modifyhd ssankar-ubuntu.vdi --resize 25000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Users\ssuriyanarayanan\VirtualBox VMs\ssankar-ubuntu>

Done Now If you open VirtualBox you will able to see "Virtual Box Size: 25GB " . Actual Size "14 GB". 

Step:2: Now create partition from un-allocated space of 10GB. This can be achieved using tool call Gparted

For this step I've followed : http://derekmolloy.ie/resize-a-virtualbox-disk/#prettyPhoto


Tuesday, 14 June 2016

Reverse Linked List

#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void AppendToList (struct node **head, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL)
                        *head = newNode;
        else {
                newNode->next = *head;
                *head   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

void ReverseList (struct node **head)
{
        struct node *prev = NULL, *curr = *head, *next = NULL;

        while (curr != NULL) {
                        next = curr->next;
                        curr->next = prev;
                        prev = curr;
                        curr = next;
        }
        *head = prev;

        return;
}

int main(void)
{
        int i;
        struct node *head = NULL;

        for (i = 1; i <= 10; i++) {
                AppendToList(&head, i);
        }

        printf("List \r\n");
        PrintList(&head);

        ReverseList(&head);
        printf("Reversed List\r\n");
        PrintList(&head);

        return 0;
}

CC = gcc

CFLAGS = -g -Wall

all: reverselist

reverselist: reverselist.o
        ${CC} ${CLAGS} -o $@ reverselist.o

clean:
        -@rm *.o

Swap linked list node without swapping data

swapnode.c
#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void AppendToList (struct node **head, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL)
                        *head = newNode;
        else {
                newNode->next = *head;
                *head   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

void swapNodes (struct node **head, int a, int b)
{
        struct node *prev_x, *curr_x;
        struct node *prev_y, *curr_y, *temp;

        curr_x = *head;
        while (curr_x && (curr_x->data != a)) {
                prev_x = curr_x;
                curr_x = curr_x->next;
        }

        curr_y = *head;
        while (curr_y && (curr_y->data != b)) {
                prev_y = curr_y;
                curr_y = curr_y->next;
        }

        prev_x->next = curr_y;
        temp                             = curr_y->next;
        curr_y->next = curr_x->next;

        prev_y->next = curr_x;
        curr_x->next = temp;

        return;
}


int main(int argc, char *argv[])
{
        int i;
        struct node *head = NULL;

        if (argc <= 2) {
                printf("usage: ./swapnode <a> <b> \r\n");
                return 0;
        }

        for (i = 1; i <= 10; i++) {
                AppendToList(&head, i);
        }

        printf("List\r\n");
        PrintList(&head);

        swapNodes(&head, atoi(argv[1]), atoi(argv[2]));
        printf("After swap\r\n");
        PrintList(&head);

        return 0;
}



CC = gcc

CFLAGS = -g -Wall

all : swapnode
swapnode: swapnode.o
        ${CC} ${CFLAGS} -o $@ swapnode.o

clean:
        -@rm *.o

Wednesday, 8 June 2016

Linked List FIFO

FIFO List

#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void InsertToFIFO (struct node **head, struct node **tail, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL) {
                        *head = newNode;
                        *tail = newNode;
        } else {
                        (*tail)->next = newNode;
                        *tail   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

int main(void)
{
        int i;
        struct node *head = NULL, *tail = NULL;

        for (i = 1; i <= 10; i++) {
                InsertToFIFO(&head, &tail, i);
        }
        printf("FIFO List \r\n");
        PrintList(&head);

        return 0;
}

Makefile:
CC = gcc

CFLAGS = -g -Wall

all:  queue_list
queue_list: queue_list.o
        ${CC} ${CFLAGS} -o $@ queue_list.o

clean:
        -@rm *.o

distclean: clean
         -@rm    queue_list

Output:
FIFO List
1
2
3
4
5
6
7
8
9
10

Find the middle node in the linked list

#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void AppendToList (struct node **head, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL)
                        *head = newNode;
        else {
                newNode->next = *head;
                *head   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

/* traverse double the time
 * return the first pointer
 * basically divide by 2 algorithm
 * **/
void FindMiddleNode (struct node **head, struct node **middle)
{
        struct node *temp1, *temp2;

        temp1 = temp2 = *head;

        while (temp2 != NULL) {
                temp1 = temp1->next;
                temp2 = temp2->next->next;
        }
        *middle = temp1;

        return;
}

int main(void)
{
        int i;
        struct node *head = NULL;
        struct node *middle = NULL;

        for (i = 1; i <= 10; i++) {
                AppendToList(&head, i);
        }

        printf("List \r\n");
        PrintList(&head);

        FindMiddleNode(&head, &middle);
        printf("Middle Node %d \r\n", middle->data);

        return 0;
}



Makefile: 


CC = gcc

CFLAGS = -g -Wall

all: mergenode findAndRemoveLoop middlenode

mergenode: mergenode.o
        ${CC} ${CFLAGS} -o $@ mergenode.o

findAndRemoveLoop: findAndRemoveLoop.o
        ${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o

middlenode: middlenode.o
        ${CC} ${CFLAGS} -o $@ middlenode.o

clean:
        -@rm *.o

distclean: clean
        -@rm    mergenode
        -@rm    findAndRemoveLoop
        -@rm    middlenode


Output: 

List
10
9
8
7
6
5
4
3
2
1
Middle Node 5

Find loop in the list and remove it

Following program to find the loop in the list and correct the loop


- Algorithm :  loop through the list and return any node from the list

- Remove the loop :
                    Traverse the head and check if the looped node matches


#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void AppendToList (struct node **head, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL)
                        *head = newNode;
        else {
                newNode->next = *head;
                *head   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

void CreateLoopedList (struct node **head)
{
        struct node *temp = *head;
        struct node *merge = *head;

        while (temp->next != NULL) {
                temp = temp->next;
        }
        printf ("Created Loop Between Node\r\n == %d -> %d == \r\n", temp->data, (merge->next->next->next)->data);
        temp->next = merge->next->next->next;

        return;
}

void FindLoopInList (struct node **head, struct node **loopNode)
{
        struct node *slow = NULL, *fast = NULL;

        slow = fast = *head;

        /* alternatively use visit flag
         * to find loop node
         * */
        while (slow && fast) {
                slow = slow->next;
                fast = fast->next->next;
                if (slow == fast) {
                        /* return any node in the loop
                         * where slow and fast ptr are met
                         **/
                        *loopNode = slow;
                        break;
                }
        }

        return;
}

void RemoveLoopFromList (struct node **head, struct node *loopNode)
{
        struct node *temp1 = *head;
        struct node *temp2 = NULL;

        while (*head) {
                        temp2 = loopNode;
                        /* start from head move temp1 for each looped node not found case
                         * keep move temp2 , unless looping node found
                         * temp1 traverse from head
                         * temp2 loop from looped one side node
                         * any point of time temp1 == temp2 then break
                         * **/
                        while (temp2->next != loopNode && temp2->next != temp1) {
                                                temp2 = temp2->next;
                        }

                        if (temp2->next == temp1) {
                                                /* found the loop, make next to null
                                                **/
                                                temp2->next = NULL;
                                                return;
                        }

                        temp1 = temp1->next;
        }

        return;
}

int main(void)
{
        int i;
        struct node *head = NULL;
        struct node *loopNode = NULL;

        for (i = 1; i <= 10; i++) {
                AppendToList(&head, i);
        }
        printf("Head List \r\n");
        PrintList(&head);

        CreateLoopedList(&head);
        FindLoopInList(&head, &loopNode);

        RemoveLoopFromList(&head, loopNode);

        printf("After removing looped List \r\n");
        PrintList(&head);

        return 0;
}

Makefile:

CC = gcc

CFLAGS = -g -Wall

all: mergenode findAndRemoveLoop

mergenode: mergenode.o
        ${CC} ${CFLAGS} -o $@ mergenode.o

findAndRemoveLoop: findAndRemoveLoop.o
        ${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o

clean:
        -@rm *.o

distclean: clean
        -@rm    mergenode
        -@rm    findAndRemoveLoop


Output: 

Head List
10
9
8
7
6
5
4
3
2
1
Created Loop Between Node
 == 1 -> 7 ==
After removing looped List
10
9
8
7
6
5
4
3
2
1

Find Intersect Node On Merged Linked List

Program to find intersect node in merged linked list


#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        int     visited:1;
        struct node *next;
};

void AppendToList (struct node **head, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->visited = 0;
        newNode->next = NULL;

        if (*head == NULL)
                        *head = newNode;
        else {
                newNode->next = *head;
                *head   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

void MergeTwoList (struct node **head1, struct node **head2)
{
        struct node *temp = *head1;
        struct node *merge = *head2;

        while (temp->next != NULL) {
                temp = temp->next;
        }
        temp->next = merge->next->next->next;

        return;
}


void FindIntersectNode (struct node **head1, struct node **head2, struct node **join)
{
        struct node *temp = *head1;

        while (temp->next != NULL) {
                temp->visited = 1;
                temp = temp->next;
        }

        temp = *head2;
        while (temp->next != NULL) {
                if (temp->visited) {
                                *join = temp;
                                return;
                }
                temp = temp->next;
        }

        return;
}

int main(void)
{
        int i;
        struct node *head1 = NULL, *head2 = NULL;
        struct node *join = NULL;

        for (i = 1; i <= 10; i++) {
                AppendToList(&head1, i);
        }

        for (i = 11; i <= 20; i++) {
                AppendToList(&head2, i);
        }

        MergeTwoList(&head1, &head2);

        printf("Merged Head1 List \r\n");
        PrintList(&head1);

        printf("Merged Head2 List \r\n");
        PrintList(&head2);

        FindIntersectNode(&head1, &head2, &join);

        printf("Joined Node %d \r\n", join->data);


        return 0;
}



CC = gcc

CFLAGS = -g -Wall

all: mergenode

mergenode: mergenode.o
        ${CC} ${CFLAGS} -o $@ mergenode.o

clean:
        -@rm *.o

distclean: clean
        -@rm    mergenode


Output: 


Merged Head1 List
10
9
8
7
6
5
4
3
2
1
17
16
15
14
13
12
11
Merged Head2 List
20
19
18
17
16
15
14
13
12
11
Joined Node 17

Tuesday, 16 February 2016

DNS Server Configuration named in RHEL

Pr-requisition install named

Very simple method just two steps to setup dns server for private LAN network.

Step-1:

File :   /etc/named.config

/
// named.conf for caching-nameserver
//

options {
        directory "/var/named";
        listen-on-v6 { any; };

        /*
         * If there is a firewall between you and nameservers you want
         * to talk to, you might need to uncomment the query-source
         * directive below.  Previous versions of BIND always asked
         * questions using port 53, but BIND 8.1 uses an unprivileged
         * port by default.
         */
        query-source address * port 53;
};


include "/etc/rndc.key";

zone "abc.com" {
        type master;
        notify no;
        file "acb.com.zone";
};




Step-2


vi /var/named/abc.com.zone

$TTL    86400
@               IN SOA  centos6.abc.com. root.abc.com. (
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum
                IN NS           centos6
centos6         IN AAAA 2001:1::100
centos6         IN A    192.168.1.100


Keep Add your  IPv4 - A Record &  IPV6 AAAA record in the zone file.

Start named daemon

/etc/init.d/named start
root@localhost network-scripts]# /etc/init.d/named status
version: 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.6
CPUs found: 1
worker threads: 1
number of zones: 17
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running
named-sdb (pid  5911) is running...


DNS Client : Host
---------------

Configure nameserver to resolve dns request .

/etc/resolve.conf

nameserver 192.168.1.100

ping centos6.abc.com




Sunday, 17 January 2016

Mount ISO and Install FTP Client & Server

If ftp server not running in your Linux Box.

If you have ISO image. mount to local machine.


[root@localhost ~]# mount -o loop /home/ssankar/rhel-server-6.0-i386-dvd.iso /mnt/
[root@localhost ~]# cd /mnt/
[root@localhost mnt]#
[root@localhost Packages]# pwd
/mnt/Packages

Install FTP Client
----------------


[root@localhost Packages]# rpm -ivh ftp-0.17-51.1.el6.i686.rpm
warning: ftp-0.17-51.1.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:ftp                    ########################################### [100%]
[root@localhost Packages]# ftp
ftp> quit
[root@localhost Packages]# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 (vsFTPd 2.2.2)

Install FTP  Server
----------------

[root@localhost Packages]# rpm -ivh vsftpd-2.2.2-6.el6.i686.rpm
warning: vsftpd-2.2.2-6.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:vsftpd                 ########################################### [100%]
[root@localhost Packages]#
[root@localhost Packages]# service vsftpd status
vsftpd is stopped
[root@localhost Packages]# service vsftpd start
Starting vsftpd for vsftpd:                                [  OK  ]


AWK Script Basic

[ssankar@localhost AWK]$ cat empdata.txt
1|100|sankar|dell|chennai
2|101|rahul|tcs|chennai
3|102|pal|cts|bangalore
4|103|kali|cts|bangalore
5|104|john|cts|bangalore
6|105|pal2|cts|bangalore
[ssankar@localhost AWK]$ cat sum.awk
BEGIN{
        FS="|"
        SNO=1 ;
        EMPID=2 ;
        NAME=3 ;
        COMPANY=4 ;
        LOCATION=5 ;
}
{
        list[$4] += 1;
        sum += $1 ;
}
END{
        printf ("%d\n", sum) ;

        for (comp in list)
        {
                printf("%s %d \n", comp, list[comp]);
        }
}
[ssankar@localhost AWK]$ awk -f sum.awk empdata.txt
21
tcs 1
dell 1
cts 4
[ssankar@localhost AWK]$

Bash shell script trap command example

#/bin/bash

#trace ctrl+c for kill a process
#trap handles seperately before terminating the process

function trap_handler()
{
        echo "trap_handler called  $$ $@"
        rm -f /tmp/input0.$$
        exit 1
}

trap trap_handler 2

echo "provide emp name"
read name

Bash shell script shift operation

#!/bin/bash

echo "passed arguments: $1 $2 $3.. array $@"
shift
echo "passed arguments: $1 $2 $3.. array $@"

declare -a arr=("sankar" "suriya" "pal" )

echo "length of array arr: ${#arr[@]}"

echo -e "$@"
shift 2

echo -e "$@"

echo "array ${arr[@]}"

Bash shell script shopt command sample

#!/bin/bash

## Before enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"
shopt -s  xpg_echo
## After enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"

# Before disabling aliases
alias l.='ls -l .'
l.

# After disabling aliases
shopt -u expand_aliases
l.

Bash shell script let command example

#!/bin/bash

#let command alter for expr command

let a=1
let b=2

let add=$a+$b
let sub=$a-$b
let mul=$a*$b
let div=$a/$b

echo $add $sub $mul $div

Bash shell script Set Unset Command Examples

#!/bin/bash

var="Learning Bash Shell Scripting"

set -- $var
echo "\$1=" $1
echo "\$2=" $2
echo "\$3=" $3
echo "\$4=" $4


echo "Var $var"

unset var

echo "After unset $var"