SlideShare a Scribd company logo
Basics of Shell
Programming
Date:02/7/2016
Chandan Kr. Rana
Table of Contents
1. Introduction
2. Usage
3. Basic System Commands
4. Useful Operations
5. Shell Variables
6. Command Substitution & Command Line Arguments
7. Decision making and Loop Constructs
8. Tools & Important Shell Features
9. References
Introduction
The Unix OS provides a flexible set of simple tools that allows us to perform a wide
variety of system-management,text-processing, and general-purpose tasks.
These tools are used by tying them together which is called shell scripts.
Usage
The Unix Shell is a user-interface program that accepts commands from the user and
executes them.
Excellent for system-management tasks but not for general-purpose programming. It is
simple to write but difficult to debug and slow in operation.
Basic System Commands
● ls-gives a simple listing of files.
● ll gives a listing of files with file details
● cp copy files
● mv move or rename files
● rm remove files
● rm -r remove entire directory subtree
● cd - change directories
● pwd print working directory
● cat lists a file or files sequentially
● more- displays a file screenful at a time
● pg variant on more
● mkdir
● rmdir
Useful Operations
WildCard
Shell allows files to be defined in terms of wildcard characters.
rm *.txt deletes all files that end with .txt.
? substitutes for any single character.
rm book?.txt
rm *book? deletes all.
Input/Output Redirection
sort sachin dravid dhoni sehwag kohli nehra
< operator
sort < names.txt redirecting contents of file with standard input
Taking input and redirecting output
sort < names.txt > output.txt
Sort & Tee
We can also append to an existing file using >> operator.
sort names.txt >> output.txt
| tee operator
It allows the standard input of one command to be chained into the standard output of
another command.
sort names.txt | tee output.txt
sort
sort -u eliminates redundant lines in output
sort -r sort in reverse order
sort -n sort numbers
sort +l skip first field in sorting
Standard Error
If a command generates an error, it is displayed to standard error. We use 2> to redirect
the error message.
Example:
ls xyzzyyy 2>dev/null
This is actually a special file that exists under UNIX where anything sent to it is simple
discarded.
Multiple command Execution
The shell allows to execute multiple command sequentially on one line by chaining them
with a ‘;’
rm *.txt ; ls
Shell Variables
The first useful command to know in building shell programs is echo, which allows to
perform output from shell program:
echo “This is a test!”
This sends the string to standard output.
Declare a variable
shvar=”This is a test!”
echo $shvar
Command Line Substitution
Say fgrep command which searches a file for a string. Suppose we want to search a file
named ‘source.txt’ for the string coyote.
fgrep Cullinan source.txt
And it prints the matching lines.
fgrep Pat E. Cullinan source.txt
Now we need to enclose the string in double qoutes.
fgrep “Pat E. Cullinan” source.txt
If a string has special character in it such as ‘*’ or ‘?’ that we want to interpret as a literal
and not a wildcard, the shell can get little confused. For this we use “*” or “?”
echo “$shvar”
echo ‘$shvar’
Expr Command
expr 2 + 4
expr 3 * 7
In general, shell programs operate in a batch mode that is without any interaction from
the user, and so most of their parameters are obtained on the cmd line.
Each argument on the command line can be seen inside the shell program as a shell
variable of the form “$1”,”$2”,”$3”, and so on.
$0-gives the name of the shell program
$#- which gives the number of arguments supplied
$* which gives a string with all the arguments supplied.
Decision Making and Looping
Shell programs can perform conditional tests on their arguments and variables and
execute different commands based on the results. Example:
if [ “$1” = “Volley” ]
then
echo “Volley not allowed.”
exit
elif [ “$1” = “Cricket” ]
then
echo “Cricket not allowed”
exit
else
echo “All other games are allowed”
fi
echo “Any other thing!”
Conditions
There are a wide variety of such test conditions:
“var” = “donald”
!=
= ””
!= “”
-eq 0
-ge 0
-gt 0
-le 0
-lt 0
-ne 0
-d tmp True if tmp is a directory
Conditions Contd.
-f tmp True if ™ is an ordinary file.
-r tmp True if tmp can be read
-s tmp True if tmp is non-zero length
-w tmp True if tmp can be written
-x tmp True if tmp is executable
Switch Case
case “$1”
in
“cricketers”) echo “Sorry, cricketers not allowed.”
exit;;
“writers”) echo “writers not welcome.”
exit;;
*) echo “All other athletes are Welcome!”;;
esac
For loop
for nvar in 1 2 3 4 5
do
echo $nvar
done
for file in *
do
echo $file
done
While & Until
While
n = 10
while [ “$n” -ne 0 ]
do
echo $n
n=’expr $n - 1’
done
Until [ “$n” -eq 0 ]
Other Useful Features
Comments
Comments is a very important in any programming language.
# This is a shell program.
Paste
The paste utility takes a list of text files and concatenates them on a line by line basis.
Example:
paste names.txt phone.txt > data.txt
Head & Tail
These are used to get first or last lines in a file respectively.
Grep
grep -v <regex> <file list>
grep -n
References
Online resources
Google :-)

More Related Content

What's hot

Shellscripting
ShellscriptingShellscripting
Shellscripting
Narendra Sisodiya
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
Amit Ghosh
 
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
shravan saini
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
 
Know the UNIX Commands
Know the UNIX CommandsKnow the UNIX Commands
Know the UNIX Commands
Brahma Killampalli
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
Emertxe Information Technologies Pvt Ltd
 
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
Akshay Siwal
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
MohanKumar Palanichamy
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
Ramasubbu .P
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1
Lilesh Pathe
 
Basic command ppt
Basic command pptBasic command ppt
Basic command pptRohit Kumar
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Nishant Munjal
 
Linux commands
Linux commandsLinux commands
Linux commands
penetration Tester
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
Shakeel Shafiq
 
Vi editor
Vi editorVi editor
Vi editor
Ramakrishna kapa
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
Hanan Nmr
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
sudhir singh yadav
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 

What's hot (20)

Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Know the UNIX Commands
Know the UNIX CommandsKnow the UNIX Commands
Know the UNIX Commands
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
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
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Shell programming
Shell programmingShell programming
Shell programming
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
Basic command ppt
Basic command pptBasic command ppt
Basic command ppt
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Vi editor
Vi editorVi editor
Vi editor
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Bash shell
Bash shellBash shell
Bash shell
 

Viewers also liked

Informe # 3
Informe # 3Informe # 3
Informe # 3
camilo granados
 
Environment
EnvironmentEnvironment
Environment
Gobierno de Navarra
 
Analisissemantico 110519213337-phpapp01
Analisissemantico 110519213337-phpapp01Analisissemantico 110519213337-phpapp01
Analisissemantico 110519213337-phpapp01
stephani salazar
 
Geometria2 u2 t3_aa4_yoav_haromontalvo
Geometria2 u2 t3_aa4_yoav_haromontalvoGeometria2 u2 t3_aa4_yoav_haromontalvo
Geometria2 u2 t3_aa4_yoav_haromontalvo
Yoav Haro
 
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
Red Sociojurídica - Nodo Antioquia
 
Quemaduras oficial
Quemaduras oficialQuemaduras oficial
Quemaduras oficial
edvin rosil
 
3.B.1 Παρακίνηση - Βασικές Έννοιες
3.B.1 Παρακίνηση - Βασικές Έννοιες3.B.1 Παρακίνηση - Βασικές Έννοιες
3.B.1 Παρακίνηση - Βασικές Έννοιες
Xristina Drosou
 
3.B.3 Παρακίνηση - Μέθοδοι & Τεχνικές
3.B.3  Παρακίνηση - Μέθοδοι & Τεχνικές 3.B.3  Παρακίνηση - Μέθοδοι & Τεχνικές
3.B.3 Παρακίνηση - Μέθοδοι & Τεχνικές
Xristina Drosou
 
Nature of Saving Faith (3)
Nature of Saving Faith (3)Nature of Saving Faith (3)
Nature of Saving Faith (3)
Don McClain
 
Investigación cualitativa
Investigación cualitativa Investigación cualitativa
Investigación cualitativa
Daniel Zúñiga
 
Uunidad junio 2016 rgr
Uunidad junio 2016 rgrUunidad junio 2016 rgr
Uunidad junio 2016 rgr
Josue Muñoz Sabino
 

Viewers also liked (12)

Informe # 3
Informe # 3Informe # 3
Informe # 3
 
Environment
EnvironmentEnvironment
Environment
 
Analisissemantico 110519213337-phpapp01
Analisissemantico 110519213337-phpapp01Analisissemantico 110519213337-phpapp01
Analisissemantico 110519213337-phpapp01
 
Institucional Sages v5
Institucional Sages v5Institucional Sages v5
Institucional Sages v5
 
Geometria2 u2 t3_aa4_yoav_haromontalvo
Geometria2 u2 t3_aa4_yoav_haromontalvoGeometria2 u2 t3_aa4_yoav_haromontalvo
Geometria2 u2 t3_aa4_yoav_haromontalvo
 
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
Relatoria casos de actores involucrados en la defensa, promocion y/o protecci...
 
Quemaduras oficial
Quemaduras oficialQuemaduras oficial
Quemaduras oficial
 
3.B.1 Παρακίνηση - Βασικές Έννοιες
3.B.1 Παρακίνηση - Βασικές Έννοιες3.B.1 Παρακίνηση - Βασικές Έννοιες
3.B.1 Παρακίνηση - Βασικές Έννοιες
 
3.B.3 Παρακίνηση - Μέθοδοι & Τεχνικές
3.B.3  Παρακίνηση - Μέθοδοι & Τεχνικές 3.B.3  Παρακίνηση - Μέθοδοι & Τεχνικές
3.B.3 Παρακίνηση - Μέθοδοι & Τεχνικές
 
Nature of Saving Faith (3)
Nature of Saving Faith (3)Nature of Saving Faith (3)
Nature of Saving Faith (3)
 
Investigación cualitativa
Investigación cualitativa Investigación cualitativa
Investigación cualitativa
 
Uunidad junio 2016 rgr
Uunidad junio 2016 rgrUunidad junio 2016 rgr
Uunidad junio 2016 rgr
 

Similar to Basics of shell programming

Shell scripting
Shell scriptingShell scripting
Shell scripting
Mufaddal Haidermota
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
harikrishnapolaki
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
Jaibeer Malik
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Using Unix
Using UnixUsing Unix
Using UnixDr.Ravi
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
SuKyeong Jang
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
Sagar Kumar
 
intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02
duquoi
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
Gaurav Bisht
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
Harsha Patel
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
Harsha Patel
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
Prof. Dr. K. Adisesha
 
Linux presentation
Linux presentationLinux presentation
Linux presentationNikhil Jain
 
Mastering unix
Mastering unixMastering unix
Mastering unixRaghu nath
 

Similar to Basics of shell programming (20)

Unix
UnixUnix
Unix
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Chap06
Chap06Chap06
Chap06
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
 
1 4 sp
1 4 sp1 4 sp
1 4 sp
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
 
Shell Scripting and Programming.pptx
Shell Scripting and Programming.pptxShell Scripting and Programming.pptx
Shell Scripting and Programming.pptx
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Linux presentation
Linux presentationLinux presentation
Linux presentation
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 

Recently uploaded

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 

Recently uploaded (20)

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 

Basics of shell programming

  • 2. Table of Contents 1. Introduction 2. Usage 3. Basic System Commands 4. Useful Operations 5. Shell Variables 6. Command Substitution & Command Line Arguments 7. Decision making and Loop Constructs 8. Tools & Important Shell Features 9. References
  • 3. Introduction The Unix OS provides a flexible set of simple tools that allows us to perform a wide variety of system-management,text-processing, and general-purpose tasks. These tools are used by tying them together which is called shell scripts.
  • 4. Usage The Unix Shell is a user-interface program that accepts commands from the user and executes them. Excellent for system-management tasks but not for general-purpose programming. It is simple to write but difficult to debug and slow in operation.
  • 5. Basic System Commands ● ls-gives a simple listing of files. ● ll gives a listing of files with file details ● cp copy files ● mv move or rename files ● rm remove files ● rm -r remove entire directory subtree ● cd - change directories ● pwd print working directory ● cat lists a file or files sequentially ● more- displays a file screenful at a time ● pg variant on more ● mkdir ● rmdir
  • 6. Useful Operations WildCard Shell allows files to be defined in terms of wildcard characters. rm *.txt deletes all files that end with .txt. ? substitutes for any single character. rm book?.txt rm *book? deletes all. Input/Output Redirection sort sachin dravid dhoni sehwag kohli nehra < operator sort < names.txt redirecting contents of file with standard input Taking input and redirecting output sort < names.txt > output.txt
  • 7. Sort & Tee We can also append to an existing file using >> operator. sort names.txt >> output.txt | tee operator It allows the standard input of one command to be chained into the standard output of another command. sort names.txt | tee output.txt sort sort -u eliminates redundant lines in output sort -r sort in reverse order sort -n sort numbers sort +l skip first field in sorting
  • 8. Standard Error If a command generates an error, it is displayed to standard error. We use 2> to redirect the error message. Example: ls xyzzyyy 2>dev/null This is actually a special file that exists under UNIX where anything sent to it is simple discarded. Multiple command Execution The shell allows to execute multiple command sequentially on one line by chaining them with a ‘;’ rm *.txt ; ls
  • 9. Shell Variables The first useful command to know in building shell programs is echo, which allows to perform output from shell program: echo “This is a test!” This sends the string to standard output. Declare a variable shvar=”This is a test!” echo $shvar
  • 10. Command Line Substitution Say fgrep command which searches a file for a string. Suppose we want to search a file named ‘source.txt’ for the string coyote. fgrep Cullinan source.txt And it prints the matching lines. fgrep Pat E. Cullinan source.txt Now we need to enclose the string in double qoutes. fgrep “Pat E. Cullinan” source.txt If a string has special character in it such as ‘*’ or ‘?’ that we want to interpret as a literal and not a wildcard, the shell can get little confused. For this we use “*” or “?” echo “$shvar” echo ‘$shvar’
  • 11. Expr Command expr 2 + 4 expr 3 * 7 In general, shell programs operate in a batch mode that is without any interaction from the user, and so most of their parameters are obtained on the cmd line. Each argument on the command line can be seen inside the shell program as a shell variable of the form “$1”,”$2”,”$3”, and so on. $0-gives the name of the shell program $#- which gives the number of arguments supplied $* which gives a string with all the arguments supplied.
  • 12. Decision Making and Looping Shell programs can perform conditional tests on their arguments and variables and execute different commands based on the results. Example: if [ “$1” = “Volley” ] then echo “Volley not allowed.” exit elif [ “$1” = “Cricket” ] then echo “Cricket not allowed” exit else echo “All other games are allowed” fi echo “Any other thing!”
  • 13. Conditions There are a wide variety of such test conditions: “var” = “donald” != = ”” != “” -eq 0 -ge 0 -gt 0 -le 0 -lt 0 -ne 0 -d tmp True if tmp is a directory
  • 14. Conditions Contd. -f tmp True if ™ is an ordinary file. -r tmp True if tmp can be read -s tmp True if tmp is non-zero length -w tmp True if tmp can be written -x tmp True if tmp is executable
  • 15. Switch Case case “$1” in “cricketers”) echo “Sorry, cricketers not allowed.” exit;; “writers”) echo “writers not welcome.” exit;; *) echo “All other athletes are Welcome!”;; esac
  • 16. For loop for nvar in 1 2 3 4 5 do echo $nvar done for file in * do echo $file done
  • 17. While & Until While n = 10 while [ “$n” -ne 0 ] do echo $n n=’expr $n - 1’ done Until [ “$n” -eq 0 ]
  • 18. Other Useful Features Comments Comments is a very important in any programming language. # This is a shell program. Paste The paste utility takes a list of text files and concatenates them on a line by line basis. Example: paste names.txt phone.txt > data.txt Head & Tail These are used to get first or last lines in a file respectively. Grep grep -v <regex> <file list> grep -n