SlideShare a Scribd company logo
https://commons.wikimedia.org/wiki/File:Bivalve_Sea_Shell.png
SHELL SCRIPT
SHELL SCRIPT
IN A
NUTSHELL
https://commons.wikimedia.org/wiki/File:Noix_Fernor_Cl_J_weber06_(23675300905).jpg
SHELL SCRIPT
IN A
NUTSHELL
http://www.dublinconcerts.ie/band/xzibit/
Why
“in a nutshell?”
Scripting is
more practice
than concepts.
How can shell
script help me?
“Many C/C++/Java candidates, even some with
10+ years of experience, would happily spend a
week writing a 2,500-line program to do
something you could do in 30 seconds with a
simple Unix command.”
– Steve Yegge
https://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions
How can shell scripts help me?
● As developers, we make computers work for
others.
● Computers could work a lot for ourselves, too.
● Shell script is a faster way to get work done.
Which shell
should I use?
Whatever you prefer!
(But we will talk about Bash here.)
How good is
shell script?
How good is shell script?
● Very good for quick, one-off tasks.
● Great for small tasks.
● Also good to deal with a lot of files, download
stuff.
● Great option to automate a set of steps.
● Can get confusing fast.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
“Python [and other languages -me] scales up to
thousand of lines, shell script scales down to a
few keystrokes.”
– Somebody at Stack Overflow
Alas, I couldn’t find the link, not even the name of the user :(
Unix Philosophy
Unix Philosophy
● Write programs that do one thing and do it well.
● Write programs to work together.
● Write programs to handle text streams.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
Unix Philosophy
● Small utilities.
● Pipes and redirects.
● “Everything is a file.”
– Including standard input/output/error.
Peter H. Salus. A Quarter-Century of Unix apud http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
Simplest
Tools
echo
● Print content to standard output.
echo
$ echo This is my message.
This is my message.
echo
$ echo "This is my message."
This is my message.
$ echo 'This is my message.'
This is my message.
echo
$ echo -e 'JavanC++'
Java
C++
cat
● Concatenate files.
● Good for showing content.
cat
$ echo -e "JavanC++" > languages
$ cat languages
Java
C++
cat
$ echo -e "WindowsnLinuxnMac OS" > OSes
$ cat languages OSes
Java
C++
Windows
Linux
Mac OS
Quoting
and
Escaping
https://twitter.com/YossiKreinin/status/778552625310625792
Quoting and Escaping
$ echo This is my message.
This is my message.
$ echo "This is my message."
This is my message.
$ echo 'This is my message.'
This is my message.
$ echo Good times (are coming)
bash: syntax error next to unexpected `token' `('
$ echo "Good times (are coming)"
Good times (are coming)
$ echo 'Good times (are coming)'
Good times (are coming)
Quoting
$ echo Good times (are coming)
bash: syntax error next to unexpected `token' `('
$ echo "Good times (are coming)"
Good times (are coming)
$ echo 'Good times (are coming)'
Good times (are coming)
Quoting
$ echo This is a little star: *
This is a little star: languages OSes
$ echo 'This is a little star: *'
This is a little star: *
$ echo This is a little star: *
This is a little star: *
Escaping
What if my parameter
has quotes?
$ echo "This is Dave's "situation.""
This is Dave's "situation."
What if my parameter
both types of quotes?
$ echo "This is Dave's situation."
This is Dave's situation.
$ echo This is a "situation."
This is a situation.
$ echo 'This is a "situation."'
This is a "situation."
$ echo "This is "Dave's situation.""
This is "Dave's situation".
Quoting
https://twitter.com/YossiKreinin/status/778552625310625792
Variables
$ VAR=this
$ echo My value is $VAR
My value is this
$ echo "My value is $VAR"
My value is this
$ echo 'My value is $VAR'
My value is $VAR
Variables
$ EXAMPLES=quotes
$ echo "too ""many """$EXAMPLES""""""""
too many quotes
$ echo '"too ""many """$EXAMPLES""""""""'
"too ""many """$EXAMPLES""""""""
$ echo '"too ""many """'$EXAMPLES'""""""""'
"too ""many """quotes""
Variables
https://twitter.com/YossiKreinin/status/778552625310625792
The Triad of
Searching
grep
● Search for lines in one or more files.
– Or from standard input.
● “Basic” or “extended” regular expressions.
– Or even plain strings and PERL regexes.
● Many options
– Inverse match.
– Only match.
– Show line numbers...
grep
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judite (61) 9 9612 9222
grep
$ grep '(61)' phones.txt
Paula (61) 9 8112 6751
Judite (61) 9 9612 9222
$ grep 'P.*o ' phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
grep
$ cat phones2.txt
John (732) 554 8749
Phillip (552) 982 9839
Paul (882) 830 3802
Jared (893) 923 3820
$ echo *
languages name OSes phones2.txt phones.txt
grep
$ grep Paul *
phones2.txt:Paul (882) 830 3802
phones.txt:Paulo (81) 9 8614 1092
phones.txt:Paula (61) 9 8112 6751
sed
● A little weird programming language.
● Most of the time, we use the s/// command.
● Can alter the file inline.
sed
$ sed 's/Judite/Judith/' phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
sed
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judite (61) 9 9612 9222
$ sed -i 's/Judite/Judith/' phones.txt
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
awk
● A full-fledged programming language.
– Similar to (predecessor of) Perl, PHP.
– Most programmers would be comfortable with.
● Read files as tables.
– Each line is a record.
– Each space-separated part a column.
awk
$ awk '$1 == "Paul"{print}' *
Paul (882) 830 3802
$ awk '$1 ~ "Paul"{print}' *
Paul (882) 830 3802
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Pipes
and
Command
Substitution
Pipes
● Redirect standard output from one command to
another command’s standard input.
● Can also redirect standard error, sure.
● This is how most of the magic happens :)
Pipes
$ cat phones.txt
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Judith (61) 9 9612 9222
Pipes
$ cat phones.txt | grep '^P'
Pedro (81) 9 9653 7734
Paulo (81) 9 8614 1092
Paula (61) 9 8112 6751
Pipes
$ cat phones.txt | grep '^P' | sed 's/ [0-9]+$/ ****/'
Pedro (81) 9 9653 ****
Paulo (81) 9 8614 ****
Paula (61) 9 8112 ****
Command Substitution
● Adds one command’s output to another
command.
Abrupt End

More Related Content

PPTX
Shell Script Tutorial
PPT
Unix Shell Scripting Basics
PDF
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
PPT
Chap06
PDF
Introduction to shell scripting
PPTX
Unix shell scripting
PPT
Unix Shell Scripting Basics
PPT
Shell Scripting
Shell Script Tutorial
Unix Shell Scripting Basics
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Chap06
Introduction to shell scripting
Unix shell scripting
Unix Shell Scripting Basics
Shell Scripting

What's hot (20)

PPTX
Unix shell scripting basics
PDF
Shell script-sec
PPTX
Unix shell scripts
PDF
Quick start bash script
ODP
OpenGurukul : Language : Shell Scripting
PDF
Shell scripting
PDF
Shell scripting
PDF
Groovy on the Shell
PPTX
Shell programming 1.ppt
PPT
Unix And Shell Scripting
PDF
Shell scripting
PPTX
Bash Shell Scripting
PPTX
Shell & Shell Script
PPTX
Unix - Shell Scripts
PPTX
Easiest way to start with Shell scripting
PDF
Redis & ZeroMQ: How to scale your application
PPTX
C to perl binding
PPTX
Cis 216 – shell scripting
ODP
Shellscripting
PPTX
Perl: Coro asynchronous
Unix shell scripting basics
Shell script-sec
Unix shell scripts
Quick start bash script
OpenGurukul : Language : Shell Scripting
Shell scripting
Shell scripting
Groovy on the Shell
Shell programming 1.ppt
Unix And Shell Scripting
Shell scripting
Bash Shell Scripting
Shell & Shell Script
Unix - Shell Scripts
Easiest way to start with Shell scripting
Redis & ZeroMQ: How to scale your application
C to perl binding
Cis 216 – shell scripting
Shellscripting
Perl: Coro asynchronous
Ad

Similar to Shell Script (20)

PPTX
shellScriptAlt.pptx
PDF
Bash production guide
KEY
Advanced Shell Scripting
PPTX
Shell scripting
PPT
390aLecture05_12sp.ppt
PPTX
KT on Bash Script.pptx
PDF
One-Liners to Rule Them All
PPTX
Linux Shell Scripting.pptx
PPT
Spsl by sasidhar 3 unit
DOCX
What is a shell script
PDF
Unleash your inner console cowboy
PDF
Unleash your inner console cowboy
PDF
PPT
ShellProgramming and Script in operating system
PPT
Shell programming
PPT
ShellAdvanced shell scripting programm.ppt
PPT
Unix shell scripting basics
PDF
8807290 shell-scripting
PDF
Shell scripting _how_to_automate_command_l_-_jason_cannon
shellScriptAlt.pptx
Bash production guide
Advanced Shell Scripting
Shell scripting
390aLecture05_12sp.ppt
KT on Bash Script.pptx
One-Liners to Rule Them All
Linux Shell Scripting.pptx
Spsl by sasidhar 3 unit
What is a shell script
Unleash your inner console cowboy
Unleash your inner console cowboy
ShellProgramming and Script in operating system
Shell programming
ShellAdvanced shell scripting programm.ppt
Unix shell scripting basics
8807290 shell-scripting
Shell scripting _how_to_automate_command_l_-_jason_cannon
Ad

More from Adam Victor Brandizzi (8)

PDF
Desenvolvimento Orientado a Documentação? Utilizando doctests para tornar seu...
PDF
Busca Textual com Elasticsearch
PDF
Learning to Rank
PDF
Centenas de Entidades, uma Única Pesquisa: Busca Textual com Elasticsearch
PDF
Centenas de Bases, uma Única Pesquisa: Busca Textual com Elasticsearch
PDF
Issue Trackers para Programadores
ODP
Dates, Times and Time Zones
Desenvolvimento Orientado a Documentação? Utilizando doctests para tornar seu...
Busca Textual com Elasticsearch
Learning to Rank
Centenas de Entidades, uma Única Pesquisa: Busca Textual com Elasticsearch
Centenas de Bases, uma Única Pesquisa: Busca Textual com Elasticsearch
Issue Trackers para Programadores
Dates, Times and Time Zones

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Unlocking AI with Model Context Protocol (MCP)
CIFDAQ's Market Insight: SEC Turns Pro Crypto
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Review of recent advances in non-invasive hemoglobin estimation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Monthly Chronicles - July 2025
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development

Shell Script