0% found this document useful (0 votes)
17 views

Bash Shell 1.0

Commands

Uploaded by

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

Bash Shell 1.0

Commands

Uploaded by

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

Bash Programming under Linux

By
CHITRAKANT BANCHHOR
IT, SCOE, Pune
References

1. Sumitabha Das, “Your UNIX: The Ultimate Guide”, McGraw Hill


2. Forouzan B. et al, “UNIX
UNIX and Shell Programming”
Programming , Brooks Cole
UNIX features

• Multi-user
Multi-
• Hierarchical file system
• Multi--tasking
Multi
• Threads
• Built--in networking
Built
• Virtual memory
UNIX Architecture

user

Hardware
ls

cp cd

who

GUI
UNIX Shells

Standard Shells

Bourne C Korn

bash tcsh
UNIX Session
How the Shell Processes Commands ?

1 The shell displays prompt on the screen ( $ or # )

2 The user types command ( press return )

3 The shell searches for the program of the command

4 The kernel runs the requested program

5 The shell again shows prompt


Redirection

• The shell allows you to redirect the standard output so that output goes into a
file instead.

$ wc < input > output


Grouping Commands

• Multiple commands executions on the same line

$ who ; date
1. Execute who

2. Execute date

3. Execute who & date as a group command


Pipes

• A vertical bar (I) is the pipe symbol.

Command-1 | Command-2
who wc -l 3

who wc -l 3
tee command

Command - 1 file

Command - 2

 save the output from a command in a file at the same time the output is piped to another command.
Command-1 | tee file-1 | command-2
Filters

Performs operations
on the inputs

Inputs from
Output
other programs
or keyboard filter result
screen
Some filters

cat grep tr
comm head sort
cut more uniq
diff paste wc
Shell Script
Shell as a command interpreter

UNIX
Shell
Interprets commands
Shell as a high-
high-level programming language

UNIX
Shell
Executes file

Contains
commands

Shell Script

 A program consisting of shell commands is called a shell script.


Simplest shell script

UNIX
Shell
Executes file

echo “Today’s date “


Today’s date
date
Displayed today’s
date

Shell Script
Shell programming
Overview

• Shell as a high level programming language .

• Features:

1. Variables
2. Input/output statements
3. Arithmetic operations
4. Branching and looping
1. Variables

I Environment Variables

II User-created Variables
User-

III Positional Parameters


I. Environment variables

• used to tailor the operating environment to suit our needs.

• Examples: TERM, HOME, and MAIL.


II. User-
User-created Variables

• These are variables that we create ourself


ourself..
III. Positional parameters

• These are used by the shell to store the values of command line arguments.
Special shell variables

$# The number of positional parameters


$? The exit status of the last executed command

$* The list of positional parameters

$0 The name of the command being executed

$$ The process number (PID) of the current shell

$! The process number of the last background command


2. Input/output statements

…..
……
read
read …….
………

Shell script
…..
……
read
read echo
………

Shell script
3. Arithmetic operations using expr utility

sum=`expr $num1 + $num2`


Floating--Point Arithmetic
Floating
• bc is the utility useful if need to perform more complex calculations.

n=`echo "scale=3; 13 / 2" | bc`

echo $n

6.500
4. Branching and looping

if,
if else,
if elif else

case

while
until
for
test command

 Numerical test

 String test

 File test
 Numerical test
test Number1 operator Number2

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
 String test
test String1 Condition String2

Condition Meaning

Str1 = Str2 True if the strings are same

Str1 != Str2 True if the strings are different

-n String True if the length string is greater than 0

-z String True if the length of the string is zero

String True if the string is not a null string


 File test test Option file_name

Option Meaning

-s file True if the file exists and has a size greater than zero

-f file True if the file exists and not a directory

-d file True if the file exists and 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 we have a read permission to it

-w file True if the file exists and we have a write permission to it

-x file True if the file exists and we have a execute permission to it


if statement

if condition expression
then
command(s)
fi
if test condition expression
then
command(s)
fi

Or

if [ condition expression ]
then
command(s)
fi
if else statement

if condition expression
then
command(s)
else
command(s)
fi
if [ condition expression ]
then
command(s)
else
command(s)
fi
if elif statement

if condition expression
then
command(s)
elif condition expression
then
command(s)
else
command(s)
fi
if [ condition expression ]
then
command(s)
elif [ condition expression ]
then
command(s)
else
command(s)
fi
case statement

case word in
pattern1)
command(s)
;;
pattern2)
command(s)
;;
……………
patternN)
command(s)
;;
*)
esac
while loop

while condition
do
command(s)
done

 As long as the condition is true, the commands between the do and the done
are executed.
until loop

until condition
do
command(s)
done

 This loop continues to execute the command(s) between the do and done until
the condition is true.
for loop

for var_1 in arg_list


do
command(s)
done
for loop cont.

for var_1 in { start..end }


do
command(s)
done
for loop cont.

for (( init ; condition ; increment ))


do
command(s)
done
Arrays in bash

• BASH supports one dimensional array.

• Array assignment: array_name


array_name[index]
[index]=value
=value

• Array dereference: value=


value=${
${array_name
array_name[index]}
[index]}
User defined functions
• Bash has functions, but somewhat limited implementations.

Function definition syntax

function fun_name()
{
# body of the function
}

You might also like