Starting Basics for running a bash script

First of all you need to open the terminal [Ctrl + Alt + T] OR Konsole [for kubuntu]

Type in “pwd” to get the present working directory.

Then use cd to change the directory location to the location of your script.

 

For example:

$ pwd
/home/sh.gulati
$ cd Desktop/test

 

I have changed the directory to Desktop/test/ because my script test.sh lies inside test folder.

So, as now we are in the directory which contains our script file we need to edit it and write something inside it.

As the beginning program, we start with echo command.

Contents of my test.sh:

#!/bin/bash
echo "This is my first Bash script!!"

 

Now to run this script, we first need to give execute permissions to our script, for that we need to type the following command in terminal:

chmod +x test.sh

Now in order to run it, type in terminal:

./test.sh

And you will get the output as:

This is my first Bash script!!

which was the expected output as echo prints the things you write inside it.

 

More about echo:

Echo can be used in 3 ways:

echo statement
echo 'statement'
echo "statement"

However, both 3 of them the output will vary!

For example:

I set a variable named test=”Blogging”

Now for all the 3 types:

#!bin/bash
echo $test Simplified
echo '$test Simplified'
echo "$test Simplified"

 

Output for the above statements:

 

Blogging Simplified
$test Simplified
Blogging Simplified

As you can see while using single quotes we are unable to print the variable value.

Note:

1 more thing is to be kept in mind while using echo without any quotes that if we want to use special symbols we need to add an escaping character infront of it in order to get it printed without getting error.

For example:

echo testing (here)
gives me an error!
Whereas:
echo testing \(here\)

will run without any problem when we use \ as escaping character.

 

Check out the next post to continue learning bash scripting.

Hope you have a good day! 🙂

Program for binary search tree in C

In binary search tree,

The first number that we insert is the root element and every other number that is entered in the binary search tree is compared with the root element.

If the new number is > the root element then it is put in the right sub-tree.

And if new number is < the root element then it is put in the left sub-tree.

 

Program:

#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
struct node *r;
struct node* insert(struct node*,int val);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
int main()
{
    r=NULL;
    int i,a,b=1,n;
    printf("Enter the number of values you wanna insert in binary search tree.\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter the value %d:",i+1);
        scanf("%d",&a);
        r=insert(r,a);
    }
    while(b)
    {
    printf("In which format do you want to display it?\n1. Preorder\n2. Inorder\n3. Postorder");
    scanf("%d",&n);
    switch(n)
    {
    case 1:
        {
            preorder(r);
            break;
        }
    case 2:
        {
            inorder(r);
            break;
        }
    case 3:
        {
            postorder(r);
            break;
        }
    default:
        printf("Not a valid option.\n");
    }
    printf("Continue? 1/0\n");
    scanf("%d",&b);
    }
}

struct node* insert(struct node *r,int val)
{
    if(r==NULL)
    {
        r=(struct node*) malloc(sizeof(struct node));
        r->data=val;
        r->left=NULL;
        r->right=NULL;
    }
    else if(val>r->data)
    {
        r->right=insert(r->right,val);
    }
    else
    {
        r->left=insert(r->left,val);
    }
    return r;
};

void preorder(struct node *r)
{
    if(r!=NULL)
    {
    printf("%d\t",r->data);
    preorder(r->left);
    preorder(r->right);
}
}
void inorder(struct node *r)
{
if(r!=NULL)
{
    inorder(r->left);
    printf("%d\t",r->data);
    inorder(r->right);
}
}

void postorder(struct node *r)
{
    if(r!=NULL)
    {
    postorder(r->left);
    postorder(r->right);
    printf("%d\t",r->data);
}
}