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

Linux shell env
Linux shell envLinux shell env
Linux shell envRahul Pola
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
Manav Prasad
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
Simon Su
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
Amit Ghosh
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
Prof. Dr. K. Adisesha
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
Geeks Anonymes
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
VIKAS TIWARI
 
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
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
Jaibeer Malik
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
Prakash Lambha
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
Anu Chaudhry
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell ScriptingMustafa Qasim
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
SRIKANTH ANDE
 

What's hot (20)

Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Chap06
Chap06Chap06
Chap06
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash 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
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
 
Bash shell
Bash shellBash shell
Bash shell
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 

Viewers also liked

Lucthur
LucthurLucthur
Lucthur
11mm22hh
 
three phase induction machine starter
three phase induction machine starterthree phase induction machine starter
three phase induction machine starter
Aditya More
 
Industrial Star Delta Starter for a 3-Phase Induction Motor
Industrial Star Delta Starter for a 3-Phase Induction MotorIndustrial Star Delta Starter for a 3-Phase Induction Motor
Industrial Star Delta Starter for a 3-Phase Induction Motor
elprocus
 
Starter of an induction motor
Starter of an induction motor Starter of an induction motor
Starter of an induction motor
sbpatel199688
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
Natasha Murashev
 

Viewers also liked (6)

Producer Consumer Problem
Producer Consumer Problem  Producer Consumer Problem
Producer Consumer Problem
 
Lucthur
LucthurLucthur
Lucthur
 
three phase induction machine starter
three phase induction machine starterthree phase induction machine starter
three phase induction machine starter
 
Industrial Star Delta Starter for a 3-Phase Induction Motor
Industrial Star Delta Starter for a 3-Phase Induction MotorIndustrial Star Delta Starter for a 3-Phase Induction Motor
Industrial Star Delta Starter for a 3-Phase Induction Motor
 
Starter of an induction motor
Starter of an induction motor Starter of an induction motor
Starter of an induction motor
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 

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
 
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
 
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
 
Linux presentation
Linux presentationLinux presentation
Linux presentationNikhil Jain
 
Mastering unix
Mastering unixMastering unix
Mastering unixRaghu nath
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
Intro commandline
Intro commandlineIntro commandline
Intro commandline
codeembedded
 
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
Kuntal Bhowmick
 

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
 
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
 
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
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Linux presentation
Linux presentationLinux presentation
Linux presentation
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
Intro commandline
Intro commandlineIntro commandline
Intro commandline
 
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
 

Recently uploaded

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
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
 
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
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
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
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
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
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
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
 
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
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.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
 
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...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
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
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
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|...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
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
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 

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