Basics of Shell Scripting

Home  >>  Shell Scripting  >>  Basics of Shell Scripting

Basics of Shell Scripting

25
Sep,2017

0

Shell Script

Shell Scripting tutorial provides basic and advanced concepts of Shell Scripting. Shell Scripting is an open-source operating system.

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. There are three types of variables in Shell Scripting which are as follows:-

  • Shell Variables

    $PATH         ————————–               displays all directories
    $USER         ————————–               display username
    $SHELL       ————————–               absolute path name of login shell
    $TERM        ————————–               displays types of terminals
    $HOME       ————————–               displays home of the user

  • Environment Variables

    $$                ——————————          displays process id of current shell
    $*                 ——————————         return argument parameter which are quoted together
    $@               ——————————          return argument parameters which are quoted individually
    $?                 ——————————          status (successful=0, unsuccessful=1) of the previous command
    $0                ——————————          filename of current script
    $!                ——————————           process number/id of the last background command

  • Local Variables

    For example :- a=12, x=”thezenith”, etc.

    Creating shell Script

  • Create a file with extension .sh
  • For Example :- gedit file1.sh
  • Inside that file write script for Hello World program as follows :-
    #!/bin/bash
    echo”Hello World”
  • In order to run/execute the file, type ./file1.sh on the terminal.
  • Output will be shown on the terminal as:- Hello World

    Taking input from User

  • In order to take input from user, read keyword is used for taking input from user.
  • read variable-name

    Arithmetic Operations

  • Create a file with filename gedit file2.sh
  • Write the following code into it :-
    #!/bin/bash
    echo”Enter the values for two variable : -”
    read a
    read b
    x=`expr$a + expr$b`
    y=$(expr”$a” – expr”$b”)
    z=$(($a \* $b))
    echo”Addition = $x”
    echo”Subtraction = $y”
    echo”Multiplication = $z”
  • Execute file by ./file2.sh
  • Output will be:-
    Enter the values for two variable
    3
    2
    Addition = 5
    Subtraction = 1
    Multiplication = 6
  • Note that all the given ways are valid to perform arithmetic operations. Also, in order to do multiplication operation we need to put backslash (“\”) before *.

    Unset the variable

  • Create a file with filename gedit file3.sh
  • In order to unset a variable we’ll an example as follows :-
    x=”hello Zenith”
    unset x
    echo $x
  • Execute file, file3.sh
  • Output will be :-
  • Nothing will be printed on the terminal, as we have unset the variable x.

Leave a Reply

Your email address will not be published. Required fields are marked *