Unix Shell Scripting

Unix Shell Scripting

Unix is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, development starting in the 1970s at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and others.

Unix
Unix

 

  • Kernel : The core of the operating system.
    • Creation and management of processes
    • A file system
    • Communication
    • A means to start the system
  • Processes : is execution of a program. Basic unit of execution is called a “job” or “task”.
  • Signals : Processes communicate with each other and with the kernel with the help of signals.
    • SEGV – segmentation violation , when the user access illegal memory.
  • Virtual Memory, swapping, paging
  • Shell : Human user of the services of the kernel is a done through a interface called shell.
    • File and directory manipulation.
    • Command execution.
    • I/O redirection.
    • Job control.
  • Utilities : programs that perform the system functions.
    • File system management.
    • Local and network communications.
    • Editors
    • Filters and text processors

Introduction to Unix Shells

A shell is an interface between the user and the kernel. It acts as an interpreter or translator. In other words, the shell accepts commands issued by you, interprets these commands, and executes the appropriate programs.
Unix Shell
Unix shell is a command line interpreter for users to interact with OS
It is yet another application program that lets users make system calls and run some application level programs
You can write your own shell if you don’t like the one you are using.
Why use shells
Because they are a simple way to string together a bunch of UNIX commands for execution at any time without the need for prior compilation. Also because its generally fast to get a script going. Not forgetting the ease with which other scripter can read the code and understand what is happening. Lastly, they are generally completely portable across the whole UNIX world, as long as they have been written to a common standard.
 
The basic shells come in three main language forms. These are (in order of creation) sh, csh and ksh.

  1. Bourne shell ($) – Historically the sh language was the first to be created and goes under the name of The Bourne Shell. It is the default shell for the typical UNIX computing environment. It is typically used by system administrators. It has a very compact syntax which makes it obtuse for novice users but very efficient when used by experts.
  2. C shell (%) – Next up was The C Shell (csh), so called because of the similar syntactical structures to the C language. The UNIX man pages contain almost twice as much information for the C Shell as the pages for the Bourne shell, leading most users to believe that it is twice as good. Its a shell based on the C programming language. It has features such as aliasing and history.
  3. Korn shell ($) – Lastly we come to The Korne Shell (ksh) made famous by IBM’s AIX flavor of UNIX. The Korne shell can be thought of as a superset of the Bourne shell as it contains the whole of the Bourne shell world within its own syntax rules. The korn shell offers compatibility with the Bourne shell with some added features.
  4. Bourne again Bash  – Distributed with Linux
  5. Trusted C shell  tcsh – Enhanced csh

Why we Use shell Scripts :

  • Automate some day today life tasks
  • System administration part can also be automated
  • Saves lot of time
  • We can create our own commands
  • Shell scripts can take i/p from user, file and o/p them to screen or file.

Shell Scripting Basics

Text file containing Unix commands ( Ex:ls, date), shell commands (variables) and programming constructs ( if statement )
Shell script should normally have execute permission; using chmod program
Executing Shell program:

  • Passing the script name as an argument to the shell

       Ex:  ksh /export/home/learn_scripts/hello.sh

  • Assign exe. permission and execute it with name

       Ex:  ./Source_Env_For_Oracle

  • #!  :  Force to run in a defined shell by using “ #! ”  on the 1st line

#! /bin/ksh

  • Comment : Text beginning after “pound” (#) till the end of line
  • Exit status: Unix utilities should return exit status
  • 0 :exit status indicates things worked as expected
  • Non zero :exit status indicates error condition
  • $$ Shell pid
  • $? Exit status of previous command
  • $! Pid of the last back ground process

Passing Parameters

  • Pass parameters by entering them after the name of the script
  • With in the script refer them with $1 through $9
  • Good scripts should check for arguments/parameters
  • Use shift to access more than 9 parameters.
  • Parameters are passed as strings
  • To pass a string containing more than word use quotes
  • $* All the arguments
  • $# Total number of arguments
  • $@ set of arguments
  • $0 Return you the script name
  • Script : /export/home/Learn_Scripts/Scripts/passpara.ksh

#! /bin/ksh
# Script to read the parameters and display
echo ” Number of parameter Entered: $# \n”
echo ” Parameter Entered: $* \n”
echo ” 1st Parameter Entered: $1 \n”
echo ” 2nd Parameter Entered: $2 \n”
exit 0
 

Arithmetic Operations

  • bc: arbitrary precision arithmetic language ; Interactive
    • bc –l : sets scale= 20 and enables math fun.
    • scale = num of dig after decimal point.
    • sqrt (4) {square root}; operators ( + _ * / % ^ (power) )
    • functions: e(1){exponential} l(x){log}
  • Expr : evaluates arguments as expression
    • $num_sum=`expr $x + $y`
    • $num_diff=`expr $x – $y`
    • $num_multi=`expr $x \* $y`
    • $num_div=`expr $x / $y`
    • $num_rem=`expr $x % $y`
    • Ex:/export/home/nkarani/Scripts/arith
  • Script: arith_operations.sh

#! /bin/ksh
#
echo “Enter the first number \n”
read x
echo “Enter the another number \n”
read y
echo “Sum of the num is `expr $x + $y` \n”
echo “diff of the num is `expr $x – $y` \n”
echo “Multiplication of the num is `expr $x \* $y` \n”
echo “Division  of the num is `expr $x / $y` \n”
echo “Remainder is `expr $x % $y` \n”
 

read, test and if commands

read : Use to get input (data from user) from keyboard and store it  to variable.
Syntax:
read variable1 variable2…
variableN
Script: /export/home/Leran_Script/Read.sh
#! /bin/ksh
echo “Pls. enter your First and Last name\n”
read first last
echo “\n FIRST NAME IS:  $first”
echo “\n LAST NAME IS:  $last”
 

  • Expression can be formed of shell variables & supported operators.
  • Operator facilitate to compare numbers, strings, logical values, file types and modes.
  • “test” command returns zero exit status if the condition is true and non zero exit status if the condition is false.

Comparing integers:

-eq (equal to)
-ne ( not equal to)
-lt (less than)
-le (less than or equal to)
-gt (greater than)
-ge (greater than or equal to)
Syntax:
test int1 opr int2 OR [ int1 opr int2 ]

  • Comparing strings:
    • string1 = string2 (equal to)
    • string1 != string2 (not equal to)
    • string (string not NULL)
    • -n string ( string not null and exists)
    • -z string ( string is NULL and exists)
    • Syntax test str1 opr str2 OR [ str1 opr str2 ]
  • File types and access modes
    • -s file_name ( file exists and not empty)
    • -f file_name (file is exists and is a regular file)
    • -d file_name (exists and is a directory)
    • -r file_name ( readable)
    • -w file_name (writable)
    • -x file_name (executable)
    • -b file_name ( file is block device)
    • -c file_name (file is a character device)
    • -p file_name ( file is a named pipe)
    • -g file_name (file has a guid set)
    • -u file_name ( file has a setuid)
    • -k file_name ( file has a sticky bit)
    • -t file_name (file descriptor)
    • Syntax test –option filename OR [ -option filename ]
  • Combining condition
    • -a logical AND
    • -o logical OR
    • -! Logical NOT
  • “if” :Condition checking / making decisions
    Syntax: if condition ; then
    command1
    command2 …
    command n ;
    fi
  • Script: If – else
    #! /bin/ksh
    if [ “$LOGNAME = “root” ] ; then
    echo “You have logged in as Super User root”;
    else
    echo “You have logged in as Normal User $LOGNAME”;
    fi
  • Complex form of “if”
    Syntax: if condition ; then
    command1
    elif condition2; then
    command2;
    else
    command 3 ;
    fi