Shell Scripting

Free Software - Free Society

Mathan Kumar
Free Software Foundation TamilNadu

Shell Scripting

  • If you are using any major operating system you are indirectly interacting to shell.

  • If you are running Ubuntu, Linux Mint or any other Linux distribution, you are interacting to shell every time you use terminal.

  • Let’s discuss about linux shells and shell scripting so before understanding shell scripting we have to get familiar with following terminologies –


  • Kernel
  • Shell
  • Application

Kernal & Shell

  • Kernel is a program that constitutes the central core of a computer operating system.

  • Shell is a UNIX term for the interactive user interface with an operating system. It is the layer of programming that understands and executes the commands a user enters.

App
Shell
Kernel
App
Shell
Kernel

Application

  • An Application is a computer software package that performs a specific function for an end user or another application based on carefully designed features.

App
Shell
Kernel

APP - Shell - Kernal

Shell Scripting

  • It is often mistaken that Linus Torvalds has developed Linux OS, but actually he is only responsible for development of Linux kernel.

  • Complete Linux system = Kernel + GNU system utilities and libraries + other management scripts + installation. Scripts.

Shell Scripting

  • A shell is special user program which provide an interface to user to use operating system services.

  • Shell accept human readable commands from user and convert them into something which kernel can understand.

  • The shell gets started when the user logs in or start the terminal.

Types of Shell

    Shell is broadly classified into two categories –

    1. Command Line Shell

    2. Graphical shell

Command Line Shell

    A special program called Terminal in linux/macOS or Command Prompt in Windows OS is provided to type in the human readable commands such as “cat”, “ls” etc. and then it is being execute.

Graphical Shells

  • Graphical shells provide means for manipulating programs based on graphical user interface (GUI), by allowing for operations.

  • Such as opening, closing, moving and resizing windows, as well as switching focus between windows.

  • Window OS or Ubuntu OS can be considered as good example which provide GUI to user for interacting with program. User do not need to type in command for every actions.

Command line shells

There are several shells are available for Linux systems like –

  • BASH (Bourne Again SHell)

  • CSH (C SHell)

  • KSH (Korn SHell)

BASH (Bourne Again SHell)

  • It is most widely used shell in Linux systems.

  • It is used as default login shell in Linux systems and in macOS.

  • It can also be installed on Windows OS.

CSH (C SHell)

  • The C shell’s syntax and usage are very similar to the C programming language.

KSH (Korn SHell)

  • The Korn Shell also was the base for the POSIX Shell standard specifications etc.

  • Each shell does the same job but understand different commands and provide different built in functions.

Shell Commands

  • Usually shells are interactive which mean, they accept command as input from users and execute them.

  • However some time we want to execute a bunch of commands routinely, so we have to type all commands each time in terminal.

File and Directory Management

  • ls List contents of a directory.

    Ex: ls, ls –l , ls –al, ls –ld, ls –R

  • cd Change the current directory. With no arguments "cd" changes to the users home directory. (cd )

  • mkdir Make a directory.

    Ex: mkdir : Makes a directory

    Ex: mkdir –p /www/chache/var/log will create all the directories starting from www.

  • touchMake the file if it doesn't exist. (touch )

  • pwdPrint or list the present working directory with full path.

File and Directory Management

  • mv Move or rename a file or directory. Ex: mv

  • cp Copy a file or directory. Ex: cp

  • rm Delete files (Remove files). (rm –rf )

  • vi Editor with a command mode and text mode. Starts in command mode.

  • echo Display a line of text.

File and Directory Management

  • useradd Create User

    EX. useradd username. Will create a username user.

  • passwd Change or create password for the user

    EX. passwd username. Will ask for changing password for username user.

  • groupadd To create a new group.

    EX. sudo groupadd new_group. Will create new group.

  • adduser Add an user to a group.

    EX. sudo adduser user_name new_group. Will add user to the group.

  • chown Change owner.

    Ex: chown : Change ownership of a file to owner1.

File and Directory Management

  • chmod Change the file permissions.

    Ex: chmod 751 myfile : change the file permissions to rwx for owner, rx for group and x for others (x=1,r=4,w=2)

    Ex: chmod go=+r myfile : Add read permission for the group and others (character meanings u-user, g-group, o-other, + add permission,-remove,r-read,w-write,x-exe)

Permissions:   

  owner group other
read
write
execute

Shell Scripting

  • As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work.

  • These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS.

  • Each shell script is saved with .sh file extension eg. myscript.sh

  • A shell script have syntax just like any other programming language.

Programming features of the UNIX/LINUX shell

  • Shell variables: Your scripts often need to keep values in memory for later use. Shell variables are symbolic names that can access values stored in memory

  • Operators: Shell scripts support many operators, including those for performing mathematical operations

  • Logic structures: Shell scripts support sequential logic (for performing a series of commands), decision logic (for branching from one point in a script to another), looping logic (for repeating a command several times), and case logic (for choosing an action from several possible alternatives)

Shell variables

    Variables are symbolic names that represent values stored in memory

  • Global Variables: Environment and configuration variables, capitalized, such as HOME, PATH, SHELL, USERNAME, and PWD.

    When you login, there will be a large number of global System variables that are already defined. These can be freely referenced and used in your shell scripts.

  • Special Variables Reversed for OS, shell programming, etc. such as positional parameters $0, $1 …

  • Local Variables: Within a shell script, you can create as many new variables as needed. Any variable created in this manner remains in existence only within that shell.

Shell Operators

Arithmetic Operators

Shell Logic Structures

    The four basic logic structures needed for program development are:

  • Sequential logic: to execute commands in the order in which they appear in the program

  • Decision logic: to execute commands only if a certain condition is satisfied

  • Looping logic: to repeat a series of commands for a given number of times

  • Case logic: to replace “if then/else if/else” statements when making numerous comparisons

Conditional Statements (if constructs )

    The most general form of the if construct is;


    if command executes successfully

       then

         execute command

    elif this command executes successfully

      then

         execute this command

         and execute this command

    else

         execute default command

    fi

LOOPS

  • Loop is a block of code that is repeated a number of times.


  • The repeating is performed either a pre-determined number of times determined by a list of items in the loop count ( for loops ) or until a particular condition is satisfied ( while and until loops)

  • To provide flexibility to the loop constructs there are also two statements namely break and continue are provided.

For loops

    Syntax:


    for arg in list

    do

         command(s)

         ..........

    done

While loops

    Syntax:


    while this_command_execute_successfully

    do

         command(s)

         ..........

    done

Switch cases

    Syntax:


    case word in

      pattern1)

         Statement(s) to be executed if pattern1 matches

         ;;

      pattern2)

         Statement(s) to be executed if pattern2 matches

         ;;

    esac

Crontab

  • Crontab: A time-based job scheduler in Unix-like operating systems

  • Enables automatic execution of commands or scripts at specified intervals

  • Essential tool for scheduling recurring tasks, such as backups, updates, and maintenance

Crontab Syntax

    * * * * *   <[command or path to shell script]>

  • Crontab entries follow a specific syntax:

  •    Minute (0-59)

  •    Hour (0-23)

  •    Day of the month (1-31)

  •    Month (1-12)

  •    Day of the week (0-6, where Sunday is 0 or 7)

Crontab Syntax

  • # Run a backup script every day at 2:00 AM

    0 2 * * * /path/to/backup.sh

  • # Update system packages every Sunday at 10:00 PM

    0 22 * * 0 /usr/bin/apt-get update -y

  • # Clean up temporary files every hour

    0 * * * * /path/to/cleanup.sh

Why do we need shell scripts ?

  • To avoid repetitive work and automation.

  • System admins use shell scripting for routine backups.

  • System monitoring.

  • Adding new functionality to the shell etc.

Advantages of shell scripts

  • The command and syntax are exactly the same as those directly entered in command line, so programmer do not need to switch to entirely different syntax.

  • Writing shell scripts are much quicker.

  • Quick start.

  • Interactive debugging etc.

Disadvantages of shell scripts

  • Prone to costly errors, a single mistake can change the command which might be harmful.

  • Design flaws within the language syntax or implementation.

  • Not well suited for large and complex task.

  • Provide minimal data structure unlike other scripting languages. etc