KEMBAR78
Lecture 1 - Introduction To Bash Shell | PDF | Parameter (Computer Programming) | Shell (Computing)
0% found this document useful (0 votes)
65 views12 pages

Lecture 1 - Introduction To Bash Shell

introduction to bash script

Uploaded by

rooosemary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views12 pages

Lecture 1 - Introduction To Bash Shell

introduction to bash script

Uploaded by

rooosemary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 1:

Introduction to Bash Shell

Prepared by: Dr. Eman Alharbi


Overview of Unix shells

• The original scripting languages were commands


• Interpreters in operating systems
• Primary example: Unix shells
• A shell in a Linux operating system takes input from you in the form of commands,
processes it, and then gives an output. It is the interface through which a user works on
the programs, commands, and scripts. A shell is accessed by a terminal which runs it.
• Bash is the dominating Unix shell today

Prepared by: Dr. Eman Alharbi


What is Shell?

• The shell is a command interpreter.


• Shell is a UNIX term for an interface between a user and an
operating system service. Shell provides users with an
interface and accepts human-readable commands into the
system and executes those commands which can run
automatically and give the program’s output in a shell script..
• It is the layer between the operating system kernel and the
user.

Prepared by: Dr. Eman Alharbi


Components of Shell Program
What is Shell? (Cont..)

• A Kernel is at the nucleus of a computer. It makes the communication


between the hardware and software possible. While the Kernel is the
innermost part of an operating system, a shell is the outermost one.

Prepared by: Dr. Eman Alharbi


Types of Shell

There are two main shells in Linux:


1. The Bourne Shell (bash): The prompt for this shell is $
and its derivatives are listed below:
• POSIX shell also is known as sh
• Korn Shell also knew as ksh
2. The C shell: The prompt for this shell is %, and its
subcategories are:
• C shell also is known as csh
• Tops C shell also is known as tcsh

Prepared by: Dr. Eman Alharbi


Why learn Bash?

• Learning Bash means learning Unix


• Learning Bash means learning the roots of scripting
• Shell scripts, especially in Bourne shell, are frequently encountered
on Unix systems
• Bash is widely available (open source) and the dominating command
interpreter and scripting language on today’s Unix systems

Prepared by: Dr. Eman Alharbi


Some Special characters used in shell scripts

• #:Comments
• ~:home directory

Prepared by: Dr. Eman Alharbi


How to Write Shell Script in Linux/Unix

• Shell Scripts are written using text editors. On your Linux system, open a
text editor program, open a new file to begin typing a shell script or shell
programming, then give the shell permission to execute your shell script
and put your script at the location from where the shell can find it.
• Let us understand the steps in creating a Shell Script:
• Create a file using a vi editor(or any other editor). Name script file
with extension .sh
• Start the script with #! /bin/sh
“#!” is an operator called shebang which directs the script to the interpreter
location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell.

• Write some code.


• Save the script file as filename.sh
• For executing the script type bash
Prepared filename.sh
by: Dr. Eman Alharbi
Some Internal Commands and Builtins

• getopts:
getopts is a command that comes built-in with Bash. It is designed to help
manage the command line options and arguments that are provided to a script
or function. In other words, it aids in 'decoding' what the user has input and
determining what actions the script should take.
• getopts syntax:
getopts optstring name [args]
• Here's a breakdown of the syntax components:
• optstring: This is a string of characters that are recognized as valid options.
• name: This is a variable that getopts uses to store the current option that it is processing.
• [args]: These are optional. If provided, getopts will parse these arguments instead of the
positional parameters.
• In a typical use case, getopts is used within a while loop to process all the
options given to the script.Prepared
The case statement is often used to perform
by: Dr. Eman Alharbi
different actions based on the current option that getopts is processing.
Some Internal Commands and Builtins (cont.)

• read:
• The Bash read command is a built-in utility that reads text
from standard input. The tool offers many functionalities for
reading user input, helping make Bash scripts interactive.
• Syntax
read <options> <arguments>
• The read command takes the user input and splits the
string into fields, assigning each new word to an argument.
If there are fewer variables than words, read stores the
remaining terms into the final variable.
• Specifying the argument names is optional. The command
stores a user's inputPrepared
into by:
the $REPLY variable by default.
Dr. Eman Alharbi
The table below shows all the possible command
options and their description.

Option Description
-a <array> Assigns the provided word sequence to a variable named <array>.
-d <delimiter> Reads a line until the provided <delimiter> instead of a new line.
-e Starts an interactive shell session to obtain the line to read.
-i <prefix> Adds initial text before reading a line as a prefix.
Returns after reading the specified number of characters while
-n <number>
honoring the delimiter to terminate early.
Returns after reading the specified number of chars, ignoring the
-N <number>
delimiter.
-p <prompt> Outputs the prompt string before reading user input.
-r Disable backslashes to escape characters.
-s Does not echo the user's input (hide typing)
-t <time> The command times out after the specified time in seconds.
Prepared by: Dr. Eman Alharbi
-u <file descriptor> Read from file descriptor instead of standard input.
Some Internal Commands and Builtins (cont.)

• grep:
• grep pattern file
– search the files file, etc. for occurrences of pattern
• expr:
– evaluates the arguments according to the operation given
• y=`expr $y + 1` (same as y=$(($y+1))
• wait:
– Stop script execution until all jobs running in background have
terminated
• exit:
– Unconditionally terminates a script
Prepared by: Dr. Eman Alharbi

You might also like