BASH/C2/Introduction-to-BASH-Shell-Scripting/English

From Script | Spoken-Tutorial
Revision as of 12:49, 26 July 2013 by Ashwini (Talk | contribs)

Jump to: navigation, search

Title of script: Introduction to BASH shell scripting

Author: FOSSEE and Spoken-tutorial Team

Keywords: video tutorial, Bash shell, hello world, execute a script


Visual Cue
Narration
Display Slide Dear friends, welcome to the spoken tutorial on Introduction to BASH shell scripting.
Display Slide

Learning Objectives

In this tutorial, we will learn
  • About different types of Shells
  • To write a Bash Shell script and
  • To execute it.


Display Slide

Prerequisites


www.spoken-tutorial.org

To follow this tutorial you should be familiar with Linux Operating System

If not, then for relevant Linux tutorials, please visit our website.

Display slide For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • GNU Bash version 4.1.10.

GNU bash version 4 or above is recommended to practice this tutorial.

Display slide

Bash Shell

Let us see what a Bash Shell is.


  • Bash Shell is a Command language interpreter, that executes commands.
  • These commands are read from the standard input device.
  • The input device can be
  • your keyboard
  • or a simple text file.


Let me show you what is a Bash Shell.
Press Ctrl+Alt+T Open the terminal by pressing

Ctrl+Alt+T keys simultaneously .

This will open the 'Gnome-terminal'.

On the Terminal, type -

echo $SHELL


Press Enter

To check which type of shell we are using, type:

echo space dollar sign SHELL('shell' in capital)


Press Enter.

Highlight

/bin/bash

You will see the output printed on the next line as:

slash bin slash bash

This indicates that we are using the Bash Shell.

Display Slide

Different Types of Shells

Bourne Shell- /bin/sh
Let us know different types of Shells available
Bourne Shell- /bin/sh 
  • == This was original UNIX shell written by Stephen Bourne. ==
  • It lacked interactivity provided by most modern shells today


C Shell- /bin/csh C Shell- /bin/csh
It provides features lacking in Bourne Shell 


K Shell- /bin/ksh


K Shell- /bin/ksh
It was created by David Korn

It has features of both B Shell and C Shell, along with some additional features.

Bash Shell- /bin/bash Bash Shell- /bin/bash
  • The Bash Shell was developed by GNU Project
  • It is based on B Shell language.
  • It has features of C and K Shells.


Display Slide

TC Shell- /bin/tcsh


TC Shell- /bin/tcsh
  • It is the default Shell of FreeBSD and its descendants.


Z Shell- /bin/zsh Z shell- /bin/zsh
  • It is a Shell designed for interactive use.
  • It has many useful features of bash, ksh, and tcsh.


Display Slide

Bash Shell script

Now let us see what a Bash Shell script is.

The Bash Shell script

  • contains a series of Bash commands in plain text file.
  • tells the Shell to execute this text file, instead of typing the commands.


Let us see how to write a simple Bash script.

Let us test the echo command, which will print Hello World on the terminal.

Switch to the terminal >> type

echo “Hello World”


Press Enter


Let's go to the terminal and type:

echo space open double quotes Hello space world close double quotes


and press Enter.

Output: Hello World This prints Hello World on the terminal.
The command worked as expected.
Highlight echo “Hello World” Now, what if we want to use this command in a file?
Just put this command in a file and execute that file.
So, let us write a simple “hello world” script.
We will use gedit text-editor for this purpose.

You are free to use your favourite text-editor.

Switch to the terminal >> type cd Desktop >> press Enter I want to create my file on the Desktop.


So, go back to the terminal and type:

cd space Desktop


Press Enter.

Type gedit hello_world.sh >> press Enter Now type

gedit space hello underscore world dot sh


and press Enter.

Point to the header of the gedit window We have opened a new file named hello_world.sh using gedit.
Type #!/bin/bash

>> press Enter

In this file, type

hash exclamation slash bin slash bash


and press Enter.

Highlight this line This is the first line of every bash script.

It is called shebang or bang line.

Type # my first Bash script

>> press Enter

Now, let's add a comment to the file by typing:

hash my space first space Bash space script


Press Enter.

Highlight #


Remember that any line after hash, is treated as a comment.


And comments are ignored by the Bash interpreter.

Type echo “Hello World”

>> press Enter


Now we can add the command which we used earlier.


So type:

echo space open double quotes Hello space world close double quotes


and press Enter.

Type echo $SHELL

>>Press Enter

Also in the next line, type:

echo space dollar sign SHELL

And press Enter.

Type echo `date`

>> Press Enter

Then type:

echo space backtick date backtick


backtick symbol is present on the key which has tilde character.


Now, press Enter.

Press CTRL+S Press CTRL+S to save this file.

We will close this file using ALT+F4.

Switch to the terminal Now, let's go back to the terminal and make this file executable.
Type

chmod +x hello_world.sh >> press Enter

So, type

chmod space plus x space hello underscore world dot sh


and press Enter.

Type

./ hello_world.sh

>> press Enter


Now execute this file by typing

dot slash hello underscore world dot sh

Press Enter.

Output: Hello World


You can see Hello World displayed on the terminal.
Output: /bin/bash The shell type is displayed on the next line. ie slash bin slash bash
Output: Wed May 8 15:59:36 IST 2013 We can see that Day, Month, Time, Time zone and Year are displayed.

The output may vary depending on the system.

Display Slide Summary

In this tutorial we have learnt about

  • Different types of Shells
  • The Bash Shell and
  • The Bash Shell script

We also wrote a simple Shell script and executed it.

Display Slide

Assignment

Write a simple Bash shell script to display the message -
  • Welcome to Bash learning
  • and “***************”

on separate lines.

Display Slide

Acknowledgement Slide

Watch the video available at the following link

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test


For more details, please write to

contact@spoken-tutorial.org

Display Slide

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

The script for this tutorial was written by the FOSSEE and Spoken Tutorial Teams, IIT Bombay.


And this is Ashwini Patil from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini