2. UNIT – III Contents
Unit III : Working with the Bash Shell
Introduction, Shell responsibilities, pipes and input
redirection, output redirection, here documents, running a
shell script, shell as a programming language, shell meta
characters, filename substitution, shell variables,
command substitution, shell commands, the environment,
quoting, test command, control structures, arithmetic in
shell, Shell script examples, functions, debugging shell
scripts.
3. Unit III outcomes
From the III unit Student can
Understand and learn the shell basics, meta characters,
responsibilities, variables, control statements, functions and
debugging.
Write and execute the shell scripts on his /her own.
4. Shell
Shell is the command interpreter, interprets the commands
and conveys them to the kernel, which executes them.
Types of shells:
Bourne Shell
C Shell
Korn Shell
Bash Shell (Bourne again Shell)
Common
core
Common
core
Bourne shell
Korn shell
C shell
Bourne Again Shell
6. Meta Characters
Characters that are processed by shell for a special
purpose are called Meta Characters.
> output redirection, writes standard output to a file.
>> output redirection, append standard output to a file.
< input redirection, reads standard input from a file
* File substitution wild card, it matches to zero or more
characters.
? File substitution wild card, it matches to any single character.
[…] File substitution wild card, it matches to any single
character between the brackets.
`cmd` command substitution, replaced by the output from
command
7. Meta Characters
| Pipe symbol, sends the output of one process to the input of
another
; Used to sequence commands
|| Conditional execution; executes a command if the
previous one fails
&& Conditional execution; executes a command if the
previous one succeeds
(...) Group of commands
& Runs a command in the background
# All characters that follow up to a new line are
comment $ Access a variable
8. Examples with meta characters
<< label input redirection, reads standard input from
script up to label lbl
To turn off the special meaning of a meta character,
precede it by a character
$ echo hi >file
$ cat file
hi
$ echo hi >file hi >file ... not stored to file but displayed at
screen
$
9. OUTPUT REDIRECTION: >, >>
$ cat >myfile
Ali Ahmet bas
^D
$ cat myfile
Ali Ahmet bas
$ cat >myfile
Cem Nil
^D
Examples with meta characters
11. PIPES
- Shells allow you to use the standard output of one process as the standard
input of another process by connecting the processes together using the
pipe(|) meta character.
$ command1 | command2
causes the standard output of command1 to “flow through” to the
standard input of command2.
- Any number of commands may be connected by pipes.
A sequence of commands changed together in this way is called a
pipeline.
12. Pipes examples
With Pipes large problems can often be solved by a chain of smaller processes, each
performed by a relatively small, reusable utility.
The standard error channel is not piped through a standard pipeline, although some
shells support this capability.
in the following example we pipe the output of the ls utility to the input of the wc
utility in order to count the number of files in the current directory.
$ ls ---> list the current directory.
a.c b.c cc.c dir1 dir2
$ ls | wc -w
5
$ _
13. Shell Responsibilities
1. customizing work environment :
To see the date, welcome message and the list of users
who are logged in we can write a shell script.
2. Automating daily tasks :
To take the back ups every day we can write a shell
script and run it
3. Automating repetitive tasks :
The repetitive tasks like compiling a C program, linking it
with libraries and executing can be assigned to a shell
script. Producing sales reports every month.
14. 4. execute important system procedures
like shutdown, formatting the disk etc..
5. performing same operation on many files.
to take the printouts of files in a directory.
15. SHELL PROGRAMS: SCRIPTS
• Any series of shell commands stored inside a regular text file for later
execution.
• A file that contains shell commands is called a script.
• Before you can run a script, you must give it execute permission by using the
chmod utility.
• To run it, sh type its name with .sh extention.
• Scripts are useful for storing commonly used sequences of commands, and they
range in complexity from simple one-liners to fully blown programs.
• When a script is run, the system determines which shell the script was written
for and then executes the shell using the script as its standard input.
16. - Every shell contains two data areas:
an environment space and a local-variable space.
A child shell inherits a copy of its parent’s environment space
and a clean local-variable space:
Parent shell
Child shell
Environment
Environment Copied from parent
Local
Local Clean, initialized
17. VARIABLES
• A shell supports two kinds of variables:
local and environment variables.
• Both kinds of variables hold data in a string format.
• The child shell gets a copy of its parent shell’s environment
variables, but not its local variables.
• Environment variables are therefore used for transmitting useful
information between parent shells and their children.
• Every shell has a set of predefined environment variables that
are
usually initialized by the startup files.
18. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 18
• VARIABLES
- In the following example,
we display the values of some common shell environment
variables:
$ echo HOME = $HOME, PATH=$PATH ---> list two variables.
HOME =/home/glass, PATH=/bin:/usr/bin:/usr/sbin
$ echo MAIL = $MAIL
MAIL=/var/mail/glass
$ echo USER = $USER, SHELL = $SHELL, TERM=$TERM
USER = glass, SHELL = /bin/sh, TERM=vt100
$ _
19. • VARIABLES
- Here is a list of the predefined environment variables that are
common to all shells:
Name Meaning
$HOME the full pathname of your home directory
$PATH a list of directories to search for commands
$MAIL the full pathname of your mailbox
$USER your username
$SHELL the full pathname of your login shell
$TERM the type of your terminal
20. • VARIABLES
- The syntax for assigning variables differs between shells,
but the way that you access the variables is the same:
If you precede the name of a variable with a $,
this token sequence is replaced by the shell with the value of
the named variable.
To create a variable, simply assign it a value;
variable does not have to be declared.
- the syntax for assigning a variable in the Bourne, Bash and Korn
shells is as follows:
variableName=value ---> place no spaces around the value
or
variableName=“ value ” ---> here, spacing doesn’t matter.
21. Prof. Andrzej (AJ) Bieszczad Email: andrzej@csun.edu Phone: 818-677-4954 21
- several common built-in variables that have special meanings:
Name Meaning
$$ The process ID of the shell.
$0 The name of the shell script( if applicable ).
$1..$9 $n refers to the nth command line argument
( if applicable ).
$* A list of all the command-line arguments.
22. here document or here doc.
A here document, or here doc, is one way to get text
input into a script without feeding it from a separate file.
a here document (here-document, heredoc, here-
string or here-script) is a file literal or input stream literal.
It is a section of a source code file that is treated as if it
were a separate file. The term is also used for a form of
multiline string literals that use similar syntax, preserving
line breaks and other white space (including indentation) in
the text.
23. Shell keywords
The words already explained to the shell. Also called as
reserved words.
echo if until trap read else case esac wait
set unset fi eval while break exec readonly
do continue ulimit shift done exit umask
export for return
24. Working with Variables
To make a variable as a constant
$ a=20
$ readonly a
when the variables are made readonly, the shell
does not allow us to change their values. All such
variables can be listed by entering readonly at the $
prompt
To erase the variable from the shell memory we have to
use:
$unset b
25. echo with backslash characters
echo “07” bell
echo “033[1m This is Bold”
echo “033[7m This is Bold and Reverse”
26. Control Statements
if – then – fi statement
if - then – else – fi statement
if - then –elif – else – fi statement
27. Operators and meaning
Operator Meaning
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to
-ne Not equal to
-eq Equal to
28. File Tests
Option Meaning
-s file True if the file exists and has a size > 0
-f file True if the file exists
-d file True if the file exists and it is a directory file
-c file True if the file exists and is a character special file
-b file True if the file exists and is a block special file
-r file True if the file exists and you have read permission
-w file True if the file exists and have write permission
-x file True if the file exists and have execute permission
29. String tests
Condition Meaning
string1 = string2 True if the strings are equal
string1 != string2 True if the strings are different
-n string True if the length of the string is > 0
-z string True if the length of the string is zero
string Ture if the string is not null