First Shell Script In linux



1. Open terminal window in Linux

2. Create a directory for all of you shell script you going to do
              mkdir shell

3. Go to the directory and create a file to  write your script
             cd shell
             vi first.sh

Introduction to shell script

KERNEL:

Kernel act as the mediator between the computer hardware and application.

SHELL:

Shell reads the commands from the user and translate them to understandable by kernel.

SHELL SCRIPT:

Shell script is a file which stores the series of commands. Shell will execute this text file instead of entering the commands.



Display All The Text Files From A Directory Using C

DESCRIPTION:
  • This program get the directory path from the user and display all the text files available in the directory.
  • We should add dirent.h header file to accomplish this program.
  •  
PROGRAM:

#include <string.h>
#include <dirent.h>
#include <string.h>
const char *only_txt (const char *);

int main (int argc, char *argv[])
{
    DIR *directory;
    struct dirent *file;    
    directory = opendir (argv[1]);
    int directory_length = strlen(argv[1]);
    if (directory != NULL){
        while (file = readdir (directory)){
            if( !strcmp((only_txt (file->d_name)), ".txt") )
                      puts (file->d_name);
        }
        (void) closedir (directory);
      }
      else
        perror ("Not able to open the directory\n");
    return 0;
}

/**load only .txt file**/

const char *only_txt (const char *filespec)
{
    char *file = strrchr (filespec, '.');
        if (file == NULL)
            file = "";
    return file;
}

COMPILE:

    gcc -o out scan.c

RUN:
  • Pass the directory name with the path as argument to the program
        ./out /home/sujin/directory

OUTPUT:

      test.txt
   README.txt
      file.txt
 
CONCLUSION:
 
   Changing ".txt" to any type file extension, you can customize this program for file type you like.

 

Shell script to find sum of two numbers

DESCRIPTION:
  • This script will read two integer value from users and give output as sum of two values
SCRIPT:

    #!/bin/bash
   
    echo -n "Enter the first number : "
    read num1
    echo -n "Enter the second number : "
    read num2
    sum=`expr $num1 + $num2`
    echo "sum of two value is $sum"

RUN: 

    sh sum.sh

OUTPUT:

   Enter the first number : 23
   Enter the second number : 45
   sum of two value is 68


Copy A File From RemoteHost to LocalHost

DESCRIPTION:
  • Consider we want to copy test.txt file from remote machine(192.168.2.5) to local machine(192.168.1.10)
  • We need to use scp(secure copy) command to establish above need
  • For this you want the root password of the remote host.

COMMAND:

    scp  <remote username>@<remote ipaddress>:<source with path> <dest path with file name>

    example:
    scp root@192.168.2.5:/home/remote/test.txt /home/user/Desktop/test.txt
  • It will ask the remote machine password to enter
  • Now file will successfully copy to remote machine