SlideShare a Scribd company logo
1 of 13
Vibrant Technologies
& Computers
Shell scripting COURSE
Make Career With Us!!
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Why Shell?
• The commercial UNIX used Korn Shell
• For Linux, the Bash is the default
• Why Shell?
– For routing jobs, such as system administration, without writing
programs
– However, the shell script is not efficient, therefore, can be used for
prototyping the ideas
• For example,
% ls –al | more (better format of listing
directory)
% man bash | col –b | lpr (print man page of man)
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
What is Shell?
• Shell is the interface between end user and
the Linux system, similar to the commands in
Windows
• Bash is installed as in /bin/sh
• Check the version
% /bin/sh --version
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Pipe and Redirection
• Redirection (< or >)
% ls –l > lsoutput.txt (save output to lsoutput.txt)
% ps >> lsoutput.txt (append to lsoutput.txt)
% more < killout.txt (use killout.txt as parameter to
more)
% kill -l 1234 > killouterr.txt 2 >&1 (redirect to the
same file)
% kill -l 1234 >/dev/null 2 >&1 (ignore std output)
• Pipe (|)
– Process are executed concurrently
% ps | sort | more
% ps –xo comm | sort | uniq | grep –v sh | more
% cat mydata.txt | sort | uniq | > mydata.txt
(generates an empty file !)
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Writing a Script
• Use text editor to generate the “first” file
#!/bin/sh
# first
# this file looks for the files containing POSIX
# and print it
for file in *
do
if grep –q POSIX $file
then
echo $file
fi
done
exit 0
% /bin/sh first
% chmod +x first
%./first (make sure . is include in PATH
parameter)
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Syntax
• Variables
• Conditions
• Control
• Lists
• Functions
• Shell Commands
• Result
• Document
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Environment Variables
• $HOME home directory
• $PATH path
• $PS1 第一層提示符號 (normally %)
• $PS2 第二層提示符號 (normally >)
• $$ process id of the script
• $# number of input parameters
• $0 name of the script file
• $IFS separation character (white space)
• Use „env‟ to check the value
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Loop Structure
• Use wildcard „*‟
#!/bin/sh
for file in $(ls f*.sh); do
lpr $file
done
exit 0
Print all f*.sh files
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Regular Expressions
• Search for lines ending with “e”
% grep e$ words2.txt
• Search for “a”
% grep a[[:blank:]] word2.txt
• Search for words starting with “Th.”
% grep Th.[[:blank:]] words2.txt
• Search for lines with 10 lower case characters
% grep –E [a-z]{10} words2.txt
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Command
• $(command) to execute command in a script
• Old format used “`” but it can be confused with
“‟”
#!/bin/sh
echo The current directory is $PWD
echo the current users are $(who)
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Arithmetic Expansion
• Use $((…)) instead of expr to evaluate arithmetic equation
#!/bin/sh
x=0
while [ “$x” –ne 10]; do
echo $x
x=$(($x+1))
done
exit 0
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Debug
• sh –n<script> set -o noexec check syntax
set –n
• sh –v<script> set -o verbose echo command before
set –v
• sh –x<script> set –o trace echo command after
set –x
set –o nounset gives error if undefined
set –x
set –o xtrace
set +o xtrace
trap „echo Exiting: critical variable =$critical_variable‟
EXIT
B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
shellscriptingtraining.vibranttechnologies.co.in
enquiry@vibrantgroup.co.in
shellscriptingtraining.vibranttechnologies.co.in
Where to Get More Information
Vibrant Group:
www.vibrantgroup.co.in
Vibrant Technologies & Computers
www.vibranttechnologies.co.in/technologies.vibrantgroup.co.
in
Vibrant HR Team
www.hr.vibrangroup.co.in

More Related Content

What's hot

Highload осень 2012 лекция 1
Highload осень 2012 лекция 1Highload осень 2012 лекция 1
Highload осень 2012 лекция 1
Technopark
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
Ngoc Dao
 

What's hot (19)

FPBrno 2018-05-22: Reason intro
FPBrno 2018-05-22: Reason introFPBrno 2018-05-22: Reason intro
FPBrno 2018-05-22: Reason intro
 
ZSH and RVM
ZSH and RVMZSH and RVM
ZSH and RVM
 
Highload осень 2012 лекция 1
Highload осень 2012 лекция 1Highload осень 2012 лекция 1
Highload осень 2012 лекция 1
 
Chromium Embedded Framework + Go at Brooklyn JS
Chromium Embedded Framework + Go at Brooklyn JSChromium Embedded Framework + Go at Brooklyn JS
Chromium Embedded Framework + Go at Brooklyn JS
 
Let's talk ChatOps - Hubot with less CoffeeScript
Let's talk ChatOps - Hubot with less CoffeeScriptLet's talk ChatOps - Hubot with less CoffeeScript
Let's talk ChatOps - Hubot with less CoffeeScript
 
LiveScript &lt;| Rocking your world.js
LiveScript &lt;| Rocking your world.js LiveScript &lt;| Rocking your world.js
LiveScript &lt;| Rocking your world.js
 
Automating WordPress Theme Development
Automating WordPress Theme DevelopmentAutomating WordPress Theme Development
Automating WordPress Theme Development
 
Angular js活用事例:filydoc
Angular js活用事例:filydocAngular js活用事例:filydoc
Angular js活用事例:filydoc
 
CSS in JSの話 #friday13json
CSS in JSの話 #friday13jsonCSS in JSの話 #friday13json
CSS in JSの話 #friday13json
 
Banquet 36
Banquet 36Banquet 36
Banquet 36
 
Html5, css3, canvas, svg and webgl
Html5, css3, canvas, svg and webglHtml5, css3, canvas, svg and webgl
Html5, css3, canvas, svg and webgl
 
Mac OS X Lion で作る WordPress local 環境
Mac OS X Lion で作る WordPress local 環境Mac OS X Lion で作る WordPress local 環境
Mac OS X Lion で作る WordPress local 環境
 
Conquering the command line for code hackers
Conquering the command line for code hackersConquering the command line for code hackers
Conquering the command line for code hackers
 
N hidden gems you didn't know hippo delivery tier and hippo (forge) could give
N hidden gems you didn't know hippo delivery tier and hippo (forge) could giveN hidden gems you didn't know hippo delivery tier and hippo (forge) could give
N hidden gems you didn't know hippo delivery tier and hippo (forge) could give
 
Como o Javascript Funciona - TDC Floripa
Como o Javascript Funciona - TDC FloripaComo o Javascript Funciona - TDC Floripa
Como o Javascript Funciona - TDC Floripa
 
Hgignore global
Hgignore globalHgignore global
Hgignore global
 
루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
 
Comets notes
Comets notesComets notes
Comets notes
 

Similar to Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-navi-mumbai

Similar to Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-navi-mumbai (20)

Shell scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
Shell scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...Shell scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
Shell scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
 
PASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and TricksPASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and Tricks
 
Building CLIs with Ruby
Building CLIs with RubyBuilding CLIs with Ruby
Building CLIs with Ruby
 
Geecon 2019 - Taming Code Quality in the Worst Language I Know: Bash
Geecon 2019 - Taming Code Quality  in the Worst Language I Know: BashGeecon 2019 - Taming Code Quality  in the Worst Language I Know: Bash
Geecon 2019 - Taming Code Quality in the Worst Language I Know: Bash
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scripts
 
CSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & CompassCSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & Compass
 
QConNY 2023 - Implementing OSSF Scorecards Across an Organisation
QConNY 2023 - Implementing OSSF Scorecards Across an OrganisationQConNY 2023 - Implementing OSSF Scorecards Across an Organisation
QConNY 2023 - Implementing OSSF Scorecards Across an Organisation
 
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdfAll Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
 
The details of CI/CD environment for Ruby
The details of CI/CD environment for RubyThe details of CI/CD environment for Ruby
The details of CI/CD environment for Ruby
 
Bridging the gap between designers and developers at the Guardian
Bridging the gap between designers and developers at the GuardianBridging the gap between designers and developers at the Guardian
Bridging the gap between designers and developers at the Guardian
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific Languages
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
 
Tested and Correct, How to Make Sure Your Documentation Keeps Working
Tested and Correct, How to Make Sure Your Documentation Keeps WorkingTested and Correct, How to Make Sure Your Documentation Keeps Working
Tested and Correct, How to Make Sure Your Documentation Keeps Working
 
Measuring Your Code
Measuring Your CodeMeasuring Your Code
Measuring Your Code
 
Measuring Your Code 2.0
Measuring Your Code 2.0Measuring Your Code 2.0
Measuring Your Code 2.0
 
Shell scrpting(payal harne)
Shell scrpting(payal harne)Shell scrpting(payal harne)
Shell scrpting(payal harne)
 
The secret of programming language development and future
The secret of programming  language development and futureThe secret of programming  language development and future
The secret of programming language development and future
 
PHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding stylePHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding style
 
PVS-Studio and static code analysis technique
PVS-Studio and static code analysis techniquePVS-Studio and static code analysis technique
PVS-Studio and static code analysis technique
 

More from VibrantGroup

More from VibrantGroup (11)

Zend training-course-navi-mumbai-zend-course-provider-navi-mumbai
Zend training-course-navi-mumbai-zend-course-provider-navi-mumbaiZend training-course-navi-mumbai-zend-course-provider-navi-mumbai
Zend training-course-navi-mumbai-zend-course-provider-navi-mumbai
 
Weblogic Application Server training-course-navi-mumbai-weblogic application ...
Weblogic Application Server training-course-navi-mumbai-weblogic application ...Weblogic Application Server training-course-navi-mumbai-weblogic application ...
Weblogic Application Server training-course-navi-mumbai-weblogic application ...
 
Software-automation-testing-course-navi-mumbai-software-automation-testing-co...
Software-automation-testing-course-navi-mumbai-software-automation-testing-co...Software-automation-testing-course-navi-mumbai-software-automation-testing-co...
Software-automation-testing-course-navi-mumbai-software-automation-testing-co...
 
Siebel training-course-navi-mumbai-siebel-course-provider-navi-mumbai
Siebel training-course-navi-mumbai-siebel-course-provider-navi-mumbaiSiebel training-course-navi-mumbai-siebel-course-provider-navi-mumbai
Siebel training-course-navi-mumbai-siebel-course-provider-navi-mumbai
 
Oracle11g training-course-navi-mumbai-oracle11gl-course-provider-navi-mumbai
Oracle11g training-course-navi-mumbai-oracle11gl-course-provider-navi-mumbaiOracle11g training-course-navi-mumbai-oracle11gl-course-provider-navi-mumbai
Oracle11g training-course-navi-mumbai-oracle11gl-course-provider-navi-mumbai
 
Netbackup training-course-navi-mumbai-netbackup-course-provider-navi-mumbai
Netbackup training-course-navi-mumbai-netbackup-course-provider-navi-mumbaiNetbackup training-course-navi-mumbai-netbackup-course-provider-navi-mumbai
Netbackup training-course-navi-mumbai-netbackup-course-provider-navi-mumbai
 
Mainframe training-course-navi-mumbai-mainframe-course-provider-navi-mumbai
Mainframe training-course-navi-mumbai-mainframe-course-provider-navi-mumbaiMainframe training-course-navi-mumbai-mainframe-course-provider-navi-mumbai
Mainframe training-course-navi-mumbai-mainframe-course-provider-navi-mumbai
 
Jboss Application Server training-course-navi-mumbai-jboss-course-provider-na...
Jboss Application Server training-course-navi-mumbai-jboss-course-provider-na...Jboss Application Server training-course-navi-mumbai-jboss-course-provider-na...
Jboss Application Server training-course-navi-mumbai-jboss-course-provider-na...
 
Datastage training-course-navi-mumbai-datastage-course-provider-navi-mumbai
Datastage training-course-navi-mumbai-datastage-course-provider-navi-mumbaiDatastage training-course-navi-mumbai-datastage-course-provider-navi-mumbai
Datastage training-course-navi-mumbai-datastage-course-provider-navi-mumbai
 
Aix admin-course-provider-navi-mumbai | Aix admin course provider Navi Mumbai...
Aix admin-course-provider-navi-mumbai | Aix admin course provider Navi Mumbai...Aix admin-course-provider-navi-mumbai | Aix admin course provider Navi Mumbai...
Aix admin-course-provider-navi-mumbai | Aix admin course provider Navi Mumbai...
 
Aix admin course provider Navi Mumbai | AIX Admin Course Training Navi Mumbai...
Aix admin course provider Navi Mumbai | AIX Admin Course Training Navi Mumbai...Aix admin course provider Navi Mumbai | AIX Admin Course Training Navi Mumbai...
Aix admin course provider Navi Mumbai | AIX Admin Course Training Navi Mumbai...
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-navi-mumbai

  • 1. Vibrant Technologies & Computers Shell scripting COURSE Make Career With Us!! B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173
  • 2. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Why Shell? • The commercial UNIX used Korn Shell • For Linux, the Bash is the default • Why Shell? – For routing jobs, such as system administration, without writing programs – However, the shell script is not efficient, therefore, can be used for prototyping the ideas • For example, % ls –al | more (better format of listing directory) % man bash | col –b | lpr (print man page of man)
  • 3. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in What is Shell? • Shell is the interface between end user and the Linux system, similar to the commands in Windows • Bash is installed as in /bin/sh • Check the version % /bin/sh --version
  • 4. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Pipe and Redirection • Redirection (< or >) % ls –l > lsoutput.txt (save output to lsoutput.txt) % ps >> lsoutput.txt (append to lsoutput.txt) % more < killout.txt (use killout.txt as parameter to more) % kill -l 1234 > killouterr.txt 2 >&1 (redirect to the same file) % kill -l 1234 >/dev/null 2 >&1 (ignore std output) • Pipe (|) – Process are executed concurrently % ps | sort | more % ps –xo comm | sort | uniq | grep –v sh | more % cat mydata.txt | sort | uniq | > mydata.txt (generates an empty file !)
  • 5. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Writing a Script • Use text editor to generate the “first” file #!/bin/sh # first # this file looks for the files containing POSIX # and print it for file in * do if grep –q POSIX $file then echo $file fi done exit 0 % /bin/sh first % chmod +x first %./first (make sure . is include in PATH parameter)
  • 6. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Syntax • Variables • Conditions • Control • Lists • Functions • Shell Commands • Result • Document
  • 7. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Environment Variables • $HOME home directory • $PATH path • $PS1 第一層提示符號 (normally %) • $PS2 第二層提示符號 (normally >) • $$ process id of the script • $# number of input parameters • $0 name of the script file • $IFS separation character (white space) • Use „env‟ to check the value
  • 8. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Loop Structure • Use wildcard „*‟ #!/bin/sh for file in $(ls f*.sh); do lpr $file done exit 0 Print all f*.sh files
  • 9. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Regular Expressions • Search for lines ending with “e” % grep e$ words2.txt • Search for “a” % grep a[[:blank:]] word2.txt • Search for words starting with “Th.” % grep Th.[[:blank:]] words2.txt • Search for lines with 10 lower case characters % grep –E [a-z]{10} words2.txt
  • 10. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Command • $(command) to execute command in a script • Old format used “`” but it can be confused with “‟” #!/bin/sh echo The current directory is $PWD echo the current users are $(who)
  • 11. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Arithmetic Expansion • Use $((…)) instead of expr to evaluate arithmetic equation #!/bin/sh x=0 while [ “$x” –ne 10]; do echo $x x=$(($x+1)) done exit 0
  • 12. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Debug • sh –n<script> set -o noexec check syntax set –n • sh –v<script> set -o verbose echo command before set –v • sh –x<script> set –o trace echo command after set –x set –o nounset gives error if undefined set –x set –o xtrace set +o xtrace trap „echo Exiting: critical variable =$critical_variable‟ EXIT
  • 13. B2/6/2 Vashi ,Navi Mumbai, Contact:09892900103/9892900173 shellscriptingtraining.vibranttechnologies.co.in enquiry@vibrantgroup.co.in shellscriptingtraining.vibranttechnologies.co.in Where to Get More Information Vibrant Group: www.vibrantgroup.co.in Vibrant Technologies & Computers www.vibranttechnologies.co.in/technologies.vibrantgroup.co. in Vibrant HR Team www.hr.vibrangroup.co.in