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"

Bash shell script getops example

#! /bin/bash

#process the command line argument using getopts
#take action based on input

while getopts :h:r:l: OPTION
do
        case $OPTION in
        h)
                echo "help of $OPTARG"
                help "$OPTARG"
        ;;
        r)
                echo "Going to remove a file $OPTARG"
                rm -f "$OPTARG"
        ;;
        *)
                echo "Unknown option"
                echo "Usage: sh getopt.sh [-hrl] [value]"
        ;;
        esac
done

Bash shell script eval command

#!/bin/bash

#eval command - evaluate the expression and execute it
#provide input as username

if [ ! -z $1 ]
then
        proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | grep $1"
else
        proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu"
fi
eval $proccomm

Bash shell script expressions

#!/bin/bash

#command line arg processing

#find last command line value [
echo "-1 value ${@: -1}"
echo "0th value ${BASH_ARGV[0]}"
echo "last index value ${@: $#}"
echo "final process value ${!#}"

for value in $@; do :; done
echo "after loop $value"

#]

#wildcards
#read the input, search that file from the directory

echo "enter the user name"
read user
echo "user $user"

#list files for user
for file in `ls /home/$user`
do
        echo "$file"
done

#list file extension .sh
for file in `ls /home/$user/Training/Shell*/*.sh`
do
        echo "$file"
done

#list file that has 3 char file name size
for file in `ls /home/$user/Training/Shell*/fo?`
do
        echo "$file"
done

#list files those start with [lfh]
for file in `ls /home/$user/Training/Shell*/[lfh]*`
do
        echo "$file"
done

#list files that matches the list
echo -e "\n a-z \r\n"
for file in `ls /home/$user/Training/Shell*/[a-z]*`
do
        echo "$file"
done


#arguments

echo "arguments $*"

Bash shell script functions

#!/bin/bash
#definition of function
#passing arg to function
#command line args
#exit status of function

#define your function here
Hello() {
        echo "Hello World"
}

#Invoke the function
Hello

#Passing the values
Hello() {
        echo "Hello World $1 $2"
}

#Invoke the function
Hello Sankar S

#return  values
Hello () {
        echo "Hello $1 $2"
        echo $@
        return 0
}

Hello Sankar Suriya
echo "Exit status of Hello Function : $?"

#command line arguments
numofarg=$#

echo "Total Number Of Arguments Are : $numofarg"

if [ $numofarg -gt 0 ]
then
        for i in $@
        do
                echo "print $i"
        done
fi

Bash shell script case statement

#!/bin/bash

n=11

case "$n" in

1)  echo "value 1 is received"
                ;;
2)  echo  "value 2 is received"
                ;;
3)  echo  "value is 3 received"
                ;;
10) echo  "value is 10 received"
                ;;
*) echo "number $n is not processed"
                ;;
esac

n="abc"

case "$n" in

"abc")  echo "value abc is received"
                ;;
2)  echo  "value 2 is received"
                ;;
3)  echo  "value is 3 received"
                ;;
10) echo  "value is 10 received"
                ;;
*) echo "number $n is not processed"
                ;;
esac


for filename in $(ls)
                                do
# Take extension available in a filename
                ext=${filename#*\.}
                case "$ext" in
                                c) echo "$filename : C source file"
                                ;;
                                o) echo "$filename : Object file"
                                ;;
                                sh) echo "$filename : Shell script"
                                ;;
                                txt) echo "$filename : Text file"
                                ;;
                                *) echo " $filename : Not processed"
                                ;;
                esac
done

Bash shell script looping statements

#!/bin/bash

#for loop , while loop

declare -a arr=( 1 2 3 4 5 )

echo ${arr[@]}

#< len of array

for (( i=0; i < ${#arr[@]} ; i++ ))
do
        echo "$i -> ${arr[$i]}"
done

#while loop

n=${#arr[@]}
i=0
while [ $i -lt $n ]
do
        echo "$i -- > ${arr[$i]}"
        let i++
done

#until
i=0
until [ ! $i -lt $n ]
do
        echo "$i -- > ${arr[$i]}"
        i=`expr $i + 1`
done

Bash shell script assignment operations

#!/bin/bash

#assignments

x=10
y=$x

echo "$x $y"

a=1.2
b=$a
echo "$a $b"

p="abc"
q=$p

echo "$p $q"

readonly n=100
s=$n

echo "$n $s"

#array to scalar

a=( 1 2 3 4 5 )
echo "${a[@]}"

m=${a[0]}
echo "a[0] $m"

arr=( "aa" "bb" "cc")
echo "${arr[@]}"

m=${arr[0]}
echo "arr[0] $m"

p=( "${arr[@]}" )

echo "p -> ${p[@]} "

v=100
a[2]=$v
echo "${a[@]}"

u="dd"
arr[1]=$u
echo "${arr[@]}"

Bash shell script if-else statements

#!/bin/bash

#if-else conditional statements

n=100
if [ $n -eq 100 ]
then
  echo "n is 100"
fi

n=99
if [ $n -eq 100 ]
then
  echo "n is 100"
else
          echo "n is not 100"
fi

n=99
if [ $n -eq 100 ]
then
  echo "n is 100"
elif [ $n -gt 100 ]
        then
          echo "n is greater than 100"
else
                  echo "n is less than 100"
fi


n=99
if [ $n -eq 100 ]
then
  echo "n is 100"
else
          if [ $n -gt 100 ]
                then
                    echo "n is greater than 100"
                else
                          echo "n is less than 100"
          fi
fi

Bash shell scripting Array Operations

#!/bin/bash

#array declarations and its operations

arr[0]='apple'
arr[1]='orange'
arr[2]='lemon'
arr[3]='kiwi'

a[0]=1
a[1]=2
a[2]=3
a[3]=4

echo "${arr[@]}"

echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"
echo "${arr[3]}"

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

echo "size of arr[0] : ${#arr[0]}"


#using declare

declare -a city=('Chennai' 'Trichy' 'Madurai' 'Kovai')
city=("${city[@]}" 'Villupuram' 'Vellore')

echo "${city[@]}"

#offset

echo "${city[0]:0:3}"


echo "${city[@]}"

#search and replace

echo "${city[@]/Chennai/Madras}"

#remove
unset city[0]

echo "${city[@]}"

#remove with pattern
declare -a newcity=( ${city[@]/*ai/} )

echo "${newcity[@]}"

#copy entire array to newone

mycitylist=("${newcity[@]}")

echo -e "my final city list: \n ${mycitylist[@]} "

#join arrays
total=("${newcity[@]}"  "${arr[@]}" "${a[@]}" )

echo "${total[@]}"

#delete entire array
unset total
echo "${total[@]}"

Bash shell scripting different variables

#!/bin/bash

#understanding different system and user varibles

echo "whoami $USER"

#global variables
# use ` for getting output of shell commands within echo output

#echo -e "Global Current Shell Values Are:\n `printenv`"

#user defined variables

i=10
j=20.5
k=30.254

echo "$i $j $k"

#strings

name="jataayu"
name1='jataayu'

echo $name $name1

echo '$name'

#constant

readonly n=100

echo $n

Shell Scripting Echo Examples

#!/bin/bash

echo "Hello World..!!"

printf "My First Shell Program\nAm after new line\n"

#-e enable interpretation of backslash escapes.
echo -e "Have Fun\nI love to Code\n"

#-n supresses the new line which is implicit when use echo command
echo -e -n "I dont introduce extra new lines\n"

Shell Scripting Arithmetic Operations

#!/bin/bash

#arithmetic operations

a=10
b=20

echo "a = $a b = $b"

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

readonly n=100

total=`expr $val + $n`

echo $total

#n=$total

#echo $n

#floating point arithmetic

x=10.1
y=20.2

echo "$x + $y" | bc
echo "$x - $y" | bc
echo "$x * $y" | bc
echo "$x / $y" | bc