SlideShare a Scribd company logo
1 of 21
Linux Shell script (Bash) Basics
PART - 1
1
2
 A script is a list of system commands stored in a
file.
 Steps to write a script :-
 Use any editor like vi or vim
 chmod permission your-script-name.
 Examples:-

$ chmod +x <filename.sh>

$ chmod 755 <filename.sh>
 Execute your script as:
 $ bash filename.sh
 $ bash fileneme.sh
 $ ./filename.sh
3
 My first shell script
clear
 echo “hello world“
 $ ./first
 $ chmod 755 first
 $ ./first
 Variables in Shell:
 In Linux (Shell), there are two types of variable:
 (1) System variables :
 (2) User defined variables :
4
 $ echo $USERNAME
 $ echo $HOME
 User defined variables :
 variable name=value
 Examples:
 $x = 10
 echo Command:
 echo command to display text
 arithmetic operations
 Syntax:
expr op1 math-operator op2
Examples:
$ expr 10 + 30
$ expr 20 – 10
$ expr 100 / 20
$ expr 200 % 30
$ expr 100 * 30
$ echo `expr 60 + 30`
5
Renaming files
mv test1 test2
Deleting files
rm -i test1
Creating directories
mkdir dir3
Deleting directories
rmdir dir3
6
 $ ps
PID TTY TIME CMD
 $ ps -ef
UID PID PPID C STIME TTY TIME CMD
 $ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME
CMD
 $ ps -efH
UID PID PPID C STIME TTY TIME CMD
7
Creating files:
$ touch test1
$ ls -il test1
Copying files:
cp source destination
cp test1 test2
Linking files:
There are two different types of file links in Linux:
a. A symbolic, or soft, link
b. A hard link
8
" Double
Quotes
Double Quotes" -
Anything enclose in
double quotes removed
meaning of that
characters (except  and
$).
' Single quotes 'Single quotes' - Enclosed in
single quotes remains
unchanged.
` Back quote `Back quote` - To execute
command
9
 Pipes:
who | wc –l
 Reading from Files:
$ read message
$ echo $message
Read command to read lines from files
 Command substitution:
Var=`date`
Var=$(date)
 Background Processes:
 ls -R /tmp &
1
0
while read ip name alias
do
if [ ! -z “$name” ]; then
# Use echo -en here to suppress ending the line;
# aliases may still be added
echo -en “IP is $ip - its name is $name”
if [ ! -z “$aliases” ]; then
echo “ Aliases: $aliases”
else
# Just echo a blank line
echo
fi
fi
done < /etc/hosts
1
1
Stopping processes
kill pid
disk space
$ df
$ df –h
Disk usages:
$ du
Commands:
$ cat file1
$ sort file1
$ cat file2
$ sort file2
$ sort -n file2
1
2
 grep [options] pattern [file]
 The grep command searches either the input
or the file you specify for lines that contain
characters that match the specified pattern.
The output from grep is the lines that contain
the matching pattern.
1
3
$ cat random.sh
#!/bin/bash
MIN=200
MAX=500
let “scope = $MAX - $MIN”
if [ “$scope” -le “0” ]; then
echo “Error - MAX is less than MIN!”
fi
for i in `seq 1 10`
do
let result=”$RANDOM % $scope + $MIN”
echo “A random number between $MIN and $MAX is $result”
Done
$ ./random.sh
1
4
$ cat hypotenuse.sh
#!/bin/sh
# calculate the length of the hypotenuse of a Pythagorean
triangle
# using hypotenuse^2 = adjacent^2 + opposite^2
echo -n “Enter the Adjacent length: “
read adjacent
echo -n “Enter the Opposite length: “
read opposite
osquared=$(($opposite ** 2)) # get o^2
asquared=$(($adjacent ** 2)) # get a^2
hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2
hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc`
# bc does sqrt
echo “The Hypotenuse is $hypotenuse”
1
5
 There are two types of environment variables in the
bash shell
 Global variables
 Local variables
1
6
 An array is a variable that can hold multiple values.
 To set multiple values for an environment variable, just list
them in parentheses, with each value
 separated by a space:
 $ mytest=(one two three four five)
 $
 Not much excitement there. If you try to display the array
as a normal environment variable,
 you’ll be disappointed:
 $ echo $mytest
 one
 $
 Only the first value in the array appears. To reference an
individual array element, you must use
 a numerical index value, which represents its place in the
array. The numeric value is enclosed in
 square brackets:
 $ echo ${mytest[2]}
 three
 $
1
7
$ date ; who
$ chmod u+x test1
$ ./test1
$ echo This is a test
This is a test
$ echo Let’s see if this’ll work
Lets see if thisll work
$
1
8
One of the most useful features of shell scripts is the lowly
back quote character, usually called the
backtick (`) in the Linux world.
You must surround the entire command line command with
backtick characters:
testing=`date`
$ cat test5
#!/bin/bash
# using the backtick character
testing=`date`
echo "The date and time are: " $testing
$
1
9
 Output redirection
The most basic type of redirection is sending output from a
command to a file. The bash shell uses the greater-than
symbol for this:
command > outputfile
Input redirection
Input redirection is the opposite of output redirection
The input redirection symbol is the less-than symbol (<):
command < inputfile
2
0
$ expr 1 + 5
6
The bash shell includes the expr command to stay
compatible with the Bourne shell; however, it
also provides a much easier way of performing mathematical
equations
$ var1=$[1 + 5]
$ echo $var1
6
$ var2 = $[$var1 * 2]
$ echo $var2
12
$
2
1

More Related Content

What's hot

Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commandsSagar Kumar
 
Linux Basic commands and VI Editor
Linux Basic commands and VI EditorLinux Basic commands and VI Editor
Linux Basic commands and VI Editorshanmuga rajan
 
Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Ahmed El-Arabawy
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell scriptBhavesh Padharia
 
Linux admin interview questions
Linux admin interview questionsLinux admin interview questions
Linux admin interview questionsKavya Sri
 
Course 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsCourse 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsAhmed El-Arabawy
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsManav Prasad
 
Introduction to Linux
Introduction to Linux Introduction to Linux
Introduction to Linux Harish R
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Zyxware Technologies
 
Input output redirection linux
Input output redirection linuxInput output redirection linux
Input output redirection linuxTanishq Soni
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell ScriptingJaibeer Malik
 

What's hot (20)

Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Linux Basic commands and VI Editor
Linux Basic commands and VI EditorLinux Basic commands and VI Editor
Linux Basic commands and VI Editor
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Know the UNIX Commands
Know the UNIX CommandsKnow the UNIX Commands
Know the UNIX Commands
 
Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Linux admin interview questions
Linux admin interview questionsLinux admin interview questions
Linux admin interview questions
 
Course 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild CardsCourse 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild Cards
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Introduction to Linux
Introduction to Linux Introduction to Linux
Introduction to Linux
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
Vi editor
Vi editorVi editor
Vi editor
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Input output redirection linux
Input output redirection linuxInput output redirection linux
Input output redirection linux
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Linux basics
Linux basicsLinux basics
Linux basics
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
Basic Unix
Basic UnixBasic Unix
Basic Unix
 

Viewers also liked

shell script introduction
shell script introductionshell script introduction
shell script introductionJie Jin
 
The bash – defensive scripting
The bash – defensive scriptingThe bash – defensive scripting
The bash – defensive scriptingtimobezjak
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell ScriptingMustafa Qasim
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptKenny (netman)
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)anandvaidya
 

Viewers also liked (19)

Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
shell script introduction
shell script introductionshell script introduction
shell script introduction
 
The bash – defensive scripting
The bash – defensive scriptingThe bash – defensive scripting
The bash – defensive scripting
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Bash script
Bash script Bash script
Bash script
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Shell programming
Shell programmingShell programming
Shell programming
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)
 
UNIX/Linux training
UNIX/Linux trainingUNIX/Linux training
UNIX/Linux training
 

Similar to Bash Shell Scripting

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualKuntal Bhowmick
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdfharikrishnapolaki
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptxNiladriDey18
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Acácio Oliveira
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptroot_fibo
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.Ravi
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.pptKiranMantri
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptEliezer Moraes
 

Similar to Bash Shell Scripting (20)

SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Unix
UnixUnix
Unix
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Lnx
LnxLnx
Lnx
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
 

More from Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013Raghu nath
 

More from Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Perl
PerlPerl
Perl
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013
 

Recently uploaded

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

Bash Shell Scripting

  • 1. Linux Shell script (Bash) Basics PART - 1 1
  • 2. 2  A script is a list of system commands stored in a file.  Steps to write a script :-  Use any editor like vi or vim  chmod permission your-script-name.  Examples:-  $ chmod +x <filename.sh>  $ chmod 755 <filename.sh>  Execute your script as:  $ bash filename.sh  $ bash fileneme.sh  $ ./filename.sh
  • 3. 3  My first shell script clear  echo “hello world“  $ ./first  $ chmod 755 first  $ ./first  Variables in Shell:  In Linux (Shell), there are two types of variable:  (1) System variables :  (2) User defined variables :
  • 4. 4  $ echo $USERNAME  $ echo $HOME  User defined variables :  variable name=value  Examples:  $x = 10  echo Command:  echo command to display text
  • 5.  arithmetic operations  Syntax: expr op1 math-operator op2 Examples: $ expr 10 + 30 $ expr 20 – 10 $ expr 100 / 20 $ expr 200 % 30 $ expr 100 * 30 $ echo `expr 60 + 30` 5
  • 6. Renaming files mv test1 test2 Deleting files rm -i test1 Creating directories mkdir dir3 Deleting directories rmdir dir3 6
  • 7.  $ ps PID TTY TIME CMD  $ ps -ef UID PID PPID C STIME TTY TIME CMD  $ ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD  $ ps -efH UID PID PPID C STIME TTY TIME CMD 7
  • 8. Creating files: $ touch test1 $ ls -il test1 Copying files: cp source destination cp test1 test2 Linking files: There are two different types of file links in Linux: a. A symbolic, or soft, link b. A hard link 8
  • 9. " Double Quotes Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except and $). ' Single quotes 'Single quotes' - Enclosed in single quotes remains unchanged. ` Back quote `Back quote` - To execute command 9
  • 10.  Pipes: who | wc –l  Reading from Files: $ read message $ echo $message Read command to read lines from files  Command substitution: Var=`date` Var=$(date)  Background Processes:  ls -R /tmp & 1 0
  • 11. while read ip name alias do if [ ! -z “$name” ]; then # Use echo -en here to suppress ending the line; # aliases may still be added echo -en “IP is $ip - its name is $name” if [ ! -z “$aliases” ]; then echo “ Aliases: $aliases” else # Just echo a blank line echo fi fi done < /etc/hosts 1 1
  • 12. Stopping processes kill pid disk space $ df $ df –h Disk usages: $ du Commands: $ cat file1 $ sort file1 $ cat file2 $ sort file2 $ sort -n file2 1 2
  • 13.  grep [options] pattern [file]  The grep command searches either the input or the file you specify for lines that contain characters that match the specified pattern. The output from grep is the lines that contain the matching pattern. 1 3
  • 14. $ cat random.sh #!/bin/bash MIN=200 MAX=500 let “scope = $MAX - $MIN” if [ “$scope” -le “0” ]; then echo “Error - MAX is less than MIN!” fi for i in `seq 1 10` do let result=”$RANDOM % $scope + $MIN” echo “A random number between $MIN and $MAX is $result” Done $ ./random.sh 1 4
  • 15. $ cat hypotenuse.sh #!/bin/sh # calculate the length of the hypotenuse of a Pythagorean triangle # using hypotenuse^2 = adjacent^2 + opposite^2 echo -n “Enter the Adjacent length: “ read adjacent echo -n “Enter the Opposite length: “ read opposite osquared=$(($opposite ** 2)) # get o^2 asquared=$(($adjacent ** 2)) # get a^2 hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2 hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc` # bc does sqrt echo “The Hypotenuse is $hypotenuse” 1 5
  • 16.  There are two types of environment variables in the bash shell  Global variables  Local variables 1 6
  • 17.  An array is a variable that can hold multiple values.  To set multiple values for an environment variable, just list them in parentheses, with each value  separated by a space:  $ mytest=(one two three four five)  $  Not much excitement there. If you try to display the array as a normal environment variable,  you’ll be disappointed:  $ echo $mytest  one  $  Only the first value in the array appears. To reference an individual array element, you must use  a numerical index value, which represents its place in the array. The numeric value is enclosed in  square brackets:  $ echo ${mytest[2]}  three  $ 1 7
  • 18. $ date ; who $ chmod u+x test1 $ ./test1 $ echo This is a test This is a test $ echo Let’s see if this’ll work Lets see if thisll work $ 1 8
  • 19. One of the most useful features of shell scripts is the lowly back quote character, usually called the backtick (`) in the Linux world. You must surround the entire command line command with backtick characters: testing=`date` $ cat test5 #!/bin/bash # using the backtick character testing=`date` echo "The date and time are: " $testing $ 1 9
  • 20.  Output redirection The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol for this: command > outputfile Input redirection Input redirection is the opposite of output redirection The input redirection symbol is the less-than symbol (<): command < inputfile 2 0
  • 21. $ expr 1 + 5 6 The bash shell includes the expr command to stay compatible with the Bourne shell; however, it also provides a much easier way of performing mathematical equations $ var1=$[1 + 5] $ echo $var1 6 $ var2 = $[$var1 * 2] $ echo $var2 12 $ 2 1