SlideShare a Scribd company logo
1 of 52
Download to read offline
Scripting and the Shell
Prepared By
Prof.Bhushan Pawar
MESCOE,Pune(Wadia Campus)
bhushan.pawar@mescoepune.org
SHELL BASICS
• All major shells in the sh(including Bourne
shell(Bash),ksh(KornShell).
• Command for editing :-
– emacs commands
– <Control-E> goes to the end of the line
– <Control-A> to the beginning.
– <Control-P> steps backward through recently
executed commands and recalls them for editing.
– <Control-R> searches incrementally through your
history to find old commands.
– http://www.computerhope.com/unix/uemacs.htm
SHELL BASICS (Continue…)
• If you like vi, put your shell’s command-line
editing into vi mode like this:
$ set -o vi
$ set -o emacs
Pipes and redirection
• Every process has at least three
communication channels available to it:
– “standard input” (STDIN)
– “standard output”(STDOUT)
– “standard error” (STDERR).
Pipes and redirection(Continue…)
• UNIX has a unified I/O model in which each
channel is named with a small integer called a
(File Descriptor).
• The exact number assigned to a channel is not
usually significant, but STDIN, STDOUT, and
STDERR are guaranteed to correspond to file
descriptors 0, 1, and 2, so it’s safe to refer to
these channels by number. In the context of an
interactive terminal window, STDIN normally
reads from the keyboard and both STDOUT and
STDERR write their output to the screen.
Pipes and redirection(Continue…)
• The shell interprets the symbols <, >, and >>
as instructions to reroute a command’s input
or output to or from a file. A < symbol
connects the command’s STDIN to the
contents of an existing file. The > and >>
symbols redirect STDOUT.
• > replaces the file’s existing contents, and >>
appends to them.
Pipes and redirection(Continue…)
• $ echo "This is a test message." > /tmp/mymessage
-it stores a single line in the file /tmp/mymessage,
creating the file if necessary.
• To connect the STDOUT of one command to the STDIN of
another, use the | symbol, commonly known as a pipe.
– E.g. $ ps -ef | grep httpd
(example runs ps to generate a list of processes and pipes it
through the grep command to select lines that contain the
word httpd.)
Variables and quoting
• Variable names are unmarked in assignments
but prefixed with a dollar sign ($)when their
values are referenced.
– E.g. $ etcdir='/etc‘
$ echo $etcdir
Common filter commands
• cut: separate lines into fields
– Syntax:- cut -c4 file.txt
– Explanation : The above cut command prints the
fourth character in each line of the file
– ( http://www.folkstalk.com/2012/02/cut-
command-in-unix-linux-examples.html )
sort command
• Sort: Sort command in unix or linux system is used
to order the elements or text. Sort command has the
capability of sorting numerical values and strings.
– Syntax :- sort [options] filename
– (http://www.folkstalk.com/2012/08/sort-
command-examples-in-unix-linux.html )
sort options
• -b : Ignores leading spaces in each line
• -d : Uses dictionary sort order. Conisders only spaces and
alphanumeric characters in sorting
• -f : Uses case insensitive sorting.
• -M : Sorts based on months. Considers only first 3 letters as month.
Eg: JAN, FEB
• -n : Uses numeric sorting
• -R : Sorts the input file randomly.
• -r : Reverse order sorting
• -k : Sorts file based on the data in the specified field positions.
• -u : Suppresses duplicate lines
• -t : input field separator
Uniq command
• Uniq command in unix or linux system is used
to suppress the duplicate lines from a file
• Syntax:- uniq [option] filename
– (http://www.folkstalk.com/search?q=uniq+comm
and+in+%3Blinux )
uniq options
• c : Count of occurrence of each line.
• d : Prints only duplicate lines.
• D : Print all duplicate lines
• f : Avoid comparing first N fields.
• i : Ignore case when comparing.
• s : Avoid comparing first N characters.
• u : Prints only unique lines.
• w : Compare no more than N characters in lines
wc command
• wc command in unix or linux is used to find
the number of lines, words and characters in a
file.
• Syntax :- wc [options] filenames
• ( http://www.folkstalk.com/2012/07/wc-
command-examples-in-unix-linux.html#more )
wc options
• -l : Prints the number of lines in a file.
• -w : prints the number of words in a file.
• -c : Displays the count of bytes in a file.
• -m : prints the count of characters from a file.
• -L : prints only the length of the longest line in
a file.
other commands
• tee
• head
• tail
• grep
tee command
• tee :- tee command writes to the STDOUT, and to
a file at a time .
• The following command writes the output only to
the file and not to the screen.
– $ ls > filename
• The following command (with the help of tee
command) writes the output both to the screen
(stdout) and to the file.
– $ ls | tee filename
(http://linux.101hacks.com/unix/tee-command-
examples/ )
head command
• The head command reads the first few lines of
any text given to it as an input and writes
them to standard output (which, by default, is
the display screen).
• head's basic syntax is:
– head [options] [file(s)]
( http://www.linfo.org/head.html )
Options of head command
• -c, --bytes=[-]N
– print the first N bytes of each file; with the leading '-', print all but the
last N bytes of each file
• -n, --lines=[-]N
– print the first N lines instead of the first 10; with the lead- ing '-', print
all but the last N lines of each file
• -q, --quiet, --silent
– never print headers giving file names
• -v, --verbose
– always print headers giving file names
tail command
• tail :-output the last part of files
• Syntax :-
– tail [OPTION]... [FILE]...
(http://linux.about.com/library/cmd/blcmdl1_tail.ht
m )
Options of tail command
• -c, --bytes=N
– output the last N bytes
• -f, --follow[={name|descriptor}]
– output appended data as the file grows; -f, --
follow, and --follow=descriptor are equivalent
• -F
– same as --follow=name --retry
• -n, --lines=N
– output the last N lines, instead of the last 10
grep command
• grep :- The grep command allows you to
search one file or multiple files for lines that
contain a pattern. Exit status is 0 if matches
were found, 1 if no matches were found, and
2 if errors occurred.
• Syntax
– grep [options] pattern [files]
(http://www.techonthenet.com/unix/basic/grep.ph
p )
Options of grep command
Bash Scripting
• Comment start with hash mark (#) and
continue to the end of line.
#!/bin/bash
echo "Hello, world!“
– The first line is known as the “shebang” statement
and declares the text file to be a script for
interpretation by /bin/bash.
Bash Scripting (Continue…)
• The kernel looks for this syntax when deciding
how to execute the file. From the perspective
of the shell spawned to execute the script, the
shebang line is just a comment.
• If bash were in a different location, we need to
adjust this line
Bash Scripting (Continue…)
• To prepare the file for running, just turn on its execute bit
$ chmod +x helloworld
$ ./helloworld
– Hello, world!
• You can also invoke the shell as an interpreter directly:
$ bash helloworld
– Hello, world!
$ source helloworld
– Hello, world!
• The first command runs helloworld in a new instance of
bash, and the second makes your existing login shell read
and execute the contents of the file
Regular Expression
• Regular expressions are powerful, but they
cannot recognize all possible grammars.
• They are so common that the name is usually
shortened to “regex.”
The matching process
• Code that evaluates a regular expression attempts to
match a single given text string to a single given
pattern. The “text string” to match can be very long
and can contain embedded newlines.
• For the matcher to declare success, the entire search
pattern must match a contiguous section of the search
text. However, the pattern can match at any position.
• After a successful match, the evaluator returns the text
of the match along with a list of matches for any
specially delimited subsections of the pattern.
Literal characters
• In general, characters in a regular expression
match themselves. So the pattern
I am the walrus
• Matches the string “I am the walrus” and that
string only. Since it can match anywhere in the
search text, the pattern can be successfully
matched to the string “I am the egg man. I am
the walrus. Koo koo ka-choo!” However, the
actual match is limited to the “I am the walrus”
portion. Matching is case sensitive.
Special characters
http://perldoc.perl.org/perlrecharclass.html
Example
I am the (walrus|egg man).
matches either “I am the walrus.” or “I am the egg man.”.
This example also demonstrates escaping of special
characters (here, the dot).
The pattern
(I am the (walrus|egg man). ?){1,2}
matches any of the following:
• I am the walrus.
• I am the egg man.
• I am the walrus. I am the egg man.
• I am the egg man. I am the walrus.
PERL PROGRAMMING
• Perl created by Larry Wall
• More power than bash
• Perl does not impose much stylistic discipline
on developers, so Perl code written without
regard for readability can be cryptic. Perl has
been accused of being a write-only language
• Perl’s catch phrase is that “there’s more than
one way to do it.”
PERL PROGRAMMING(Continue…)
• Perl statements are separated by semicolons.
Comments start with a hash mark (#) and
continue to the end of the line. Blocks of
statements are enclosed in curly braces.
Program
#!/usr/bin/perl
print "Hello, world!n";
• As with bash programs, you must either
chmod +x the executable file or invoke the
Perl interpreter directly.
$ chmod +x helloworld
$ ./helloworld
Hello, world!
Variables and arrays
• three fundamental data types
– Scalars (that is, unitary values such as numbers
and strings)
– Arrays
– Hashes (also known as associative Arrays)
• Scalar variables start with $, Array variables
start with @, and Hash variables start with %
Variables and arrays(Continue…)
• In Perl, the terms “list” and “array” are often
used interchangeably, but it’s more accurate to
say that a list is a series of values and an array is
a variable that can hold such a list.
• The individual elements of an array are scalars, so
like ordinary scalar variables, their names begin
with $. Array subscripting begins at zero, and the
index of the highest element in array
@a is $#a.
• The array @ARGV contains the script’s command
line arguments.
Program
#!/usr/bin/perl
@items = ("socks", "shoes", "shorts");
printf "There are %d articles of clothing.n",$#items + 1;
print "Put on ${items[2]} first, then ", join(" and ",
@items[0,1]), ".n";
• The output:
$ perl clothes
There are 3 articles of clothing.
Put on shorts first, then socks and shoes.
Explanation of the program
• Array and string literals
– Perl does not strictly require that all strings be
quoted so @items = (socks, shoes, shorts); is also
same
• Function calls
– Both print and printf accept an arbitrary number
of arguments, and the arguments are separated
by commas
Explanation of the program
• join receives three string arguments: “ and ”,
“socks”, and “shoes”. It concatenates its second
and subsequent arguments, inserting a copy of
the first argument between each pair. The result
is “socks and shoes”.
Explanation of the program
• Type conversions in expressions
The magic is in the + operator, which always
implies arithmetic. It converts its arguments to
numbers and produces a numeric result. Similarly,
the dot operator (.), which concatenates strings,
converts its operands as needed: "2" . (12 ** 2) yields
“2144”.
Regular expressions in Perl
• Regular expressions in Perl by “binding” strings to
regex operations with the
=~ operator.
e.g if ($text =~ m/ab+c/) { }
• checks to see whether the string stored in $text
matches the regular expression ab+c.
• To operate on the default string, $_ , you can simply
omit the variable name and binding operator.
• In fact, you can omit the m, too, since the operation
defaults to matching
Program
#!/usr/bin/perl
$names = "huey dewey louie";
$regex = '(w+)s+(w+)s+(w+)';
if ($names =~ m/$regex/)
{
print "1st name is $1.n2nd name is $2.n3rd name is$3.n";
$names =~ s/$regex/2 1/;
print "New names are "${names}".n";
}
else {
print qq{"$names" did not match "$regex".n};
}
(http://www.tutorialspoint.com/perl/perl_qq.htm)
Output
The output:
$ perl testregex
1st name is huey.
2nd name is dewey.
3rd name is louie.
New names are "dewey huey".
Input and output
• When you open a file for reading or writing,
you define a “filehandle” to identify the
channel.
• INFILE & OUTFILE are the filehandlers.
• The while loop condition is <INFILE>
Program
#!/usr/bin/perl
open(INFILE, "</etc/passwd") or die "Couldn’t open
/etc/passwd";
open(OUTFILE, ">/tmp/passwd") or die "Couldn’t open
/tmp/passwd";
while (<INFILE>)
{
($name, $pw, $uid, $gid, $gecos, $path, $sh) = split /:/;
print OUTFILE "$uidt$namen";
}
Continue…
PYTHON SCRIPTING
• Python was created by Guido van Rossum
• It’s easier to code and more readable than Perl.
Python offers a simple-to-understand syntax that
is easy to follow even if you didn’t develop the
code. If you’re tired of remembering which
comparison operators to use, you’ll appreciate
Python’s unified approach.
• Python also offers additional data types that
some system administrators find useful.
Simple Program
#!/usr/bin/python
print "Hello, world!“
Invoke the python interpreter directly:
$ chmod +x helloworld
$ ./helloworld
Hello, world!
sys — (System-specific parameters
and functions)
• import sys
(https://docs.python.org/2/library/sys.html)
Program
#!/usr/bin/python
name = 'Gwen'
rating = 10
characters = [ 'SpongeBob', 'Patrick', 'Squidward' ]
elements = ( 'lithium', 'carbon', 'boron' )
print "name:t%snrating:t%d" % (name, rating)
print "characters:t%s" % characters
print "elements:t%s" % (elements, )
This example produces the following output:
$ python objects
name: Gwen
rating: 10
characters: ['SpongeBob', 'Patrick', 'Squidward']
elements: ('lithium', 'carbon', 'boron')
Loops
• The fragment below uses a for…in construct to
iterate through the range 1 to 10.
for counter in range(1, 10):
print counter,
Output:
1 2 3 4 5 6 7 8 9
Continue…
• Both for and while loops can have else clauses
at the end. The else clause is executed only if
the loop terminates normally, as opposed to
exiting through a break statement.
• This feature may initially seem
counterintuitive, but it handles certain use
cases quite elegantly.

More Related Content

What's hot

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programmingsudhir singh yadav
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpen Gurukul
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scriptingVIKAS TIWARI
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsManav Prasad
 
Linux shell env
Linux shell envLinux shell env
Linux shell envRahul Pola
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 

What's hot (20)

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Bash shell
Bash shellBash shell
Bash shell
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
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
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Chap06
Chap06Chap06
Chap06
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 

Viewers also liked

TMG Intro To Linux
TMG Intro To LinuxTMG Intro To Linux
TMG Intro To LinuxRichBos
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linuxKevin OBrien
 
Intro To Linux
Intro To LinuxIntro To Linux
Intro To Linuxtechlug
 
Linux day 2016 la shell in linux
Linux day 2016   la shell in linuxLinux day 2016   la shell in linux
Linux day 2016 la shell in linuxGiuseppe Piccolo
 
关于Linux的许多
关于Linux的许多关于Linux的许多
关于Linux的许多Yun Teng
 
Jenkins &amp; scriptable build
Jenkins &amp; scriptable buildJenkins &amp; scriptable build
Jenkins &amp; scriptable buildBryan Liu
 
SVN Usage & Best Practices
SVN Usage & Best PracticesSVN Usage & Best Practices
SVN Usage & Best PracticesAshraf Fouad
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslogashok191
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanshipbokonen
 

Viewers also liked (19)

Shell programming
Shell programmingShell programming
Shell programming
 
TMG Intro To Linux
TMG Intro To LinuxTMG Intro To Linux
TMG Intro To Linux
 
Linux Intro
Linux   IntroLinux   Intro
Linux Intro
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Intro To Linux
Intro To LinuxIntro To Linux
Intro To Linux
 
Encryption basics
Encryption basicsEncryption basics
Encryption basics
 
Linux day 2016 la shell in linux
Linux day 2016   la shell in linuxLinux day 2016   la shell in linux
Linux day 2016 la shell in linux
 
关于Linux的许多
关于Linux的许多关于Linux的许多
关于Linux的许多
 
Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
 
Jenkins &amp; scriptable build
Jenkins &amp; scriptable buildJenkins &amp; scriptable build
Jenkins &amp; scriptable build
 
60761 linux
60761 linux60761 linux
60761 linux
 
Linux shell scripting_v2
Linux shell scripting_v2Linux shell scripting_v2
Linux shell scripting_v2
 
SVN Usage & Best Practices
SVN Usage & Best PracticesSVN Usage & Best Practices
SVN Usage & Best Practices
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
Trouble shoot with linux syslog
Trouble shoot with linux syslogTrouble shoot with linux syslog
Trouble shoot with linux syslog
 
Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanship
 

Similar to Scripting and the shell in LINUX

Similar to Scripting and the shell in LINUX (20)

Unit 4 scripting and the shell
Unit 4 scripting and the shellUnit 4 scripting and the shell
Unit 4 scripting and the shell
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Unix
UnixUnix
Unix
 
tools
toolstools
tools
 
tools
toolstools
tools
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Unit 6 bash shell
Unit 6 bash shellUnit 6 bash shell
Unit 6 bash shell
 
2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptx2. UNIX OS System Architecture easy.pptx
2. UNIX OS System Architecture easy.pptx
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
 
Group13
Group13Group13
Group13
 
Slides
SlidesSlides
Slides
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
workshop_1.ppt
workshop_1.pptworkshop_1.ppt
workshop_1.ppt
 
Linux
LinuxLinux
Linux
 
Linux powerpoint
Linux powerpointLinux powerpoint
Linux powerpoint
 
Commands and shell programming (3)
Commands and shell programming (3)Commands and shell programming (3)
Commands and shell programming (3)
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
Love Your Command Line
Love Your Command LineLove Your Command Line
Love Your Command Line
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

Scripting and the shell in LINUX

  • 1. Scripting and the Shell Prepared By Prof.Bhushan Pawar MESCOE,Pune(Wadia Campus) bhushan.pawar@mescoepune.org
  • 2. SHELL BASICS • All major shells in the sh(including Bourne shell(Bash),ksh(KornShell). • Command for editing :- – emacs commands – <Control-E> goes to the end of the line – <Control-A> to the beginning. – <Control-P> steps backward through recently executed commands and recalls them for editing. – <Control-R> searches incrementally through your history to find old commands. – http://www.computerhope.com/unix/uemacs.htm
  • 3. SHELL BASICS (Continue…) • If you like vi, put your shell’s command-line editing into vi mode like this: $ set -o vi $ set -o emacs
  • 4. Pipes and redirection • Every process has at least three communication channels available to it: – “standard input” (STDIN) – “standard output”(STDOUT) – “standard error” (STDERR).
  • 5. Pipes and redirection(Continue…) • UNIX has a unified I/O model in which each channel is named with a small integer called a (File Descriptor). • The exact number assigned to a channel is not usually significant, but STDIN, STDOUT, and STDERR are guaranteed to correspond to file descriptors 0, 1, and 2, so it’s safe to refer to these channels by number. In the context of an interactive terminal window, STDIN normally reads from the keyboard and both STDOUT and STDERR write their output to the screen.
  • 6. Pipes and redirection(Continue…) • The shell interprets the symbols <, >, and >> as instructions to reroute a command’s input or output to or from a file. A < symbol connects the command’s STDIN to the contents of an existing file. The > and >> symbols redirect STDOUT. • > replaces the file’s existing contents, and >> appends to them.
  • 7. Pipes and redirection(Continue…) • $ echo "This is a test message." > /tmp/mymessage -it stores a single line in the file /tmp/mymessage, creating the file if necessary. • To connect the STDOUT of one command to the STDIN of another, use the | symbol, commonly known as a pipe. – E.g. $ ps -ef | grep httpd (example runs ps to generate a list of processes and pipes it through the grep command to select lines that contain the word httpd.)
  • 8. Variables and quoting • Variable names are unmarked in assignments but prefixed with a dollar sign ($)when their values are referenced. – E.g. $ etcdir='/etc‘ $ echo $etcdir
  • 9. Common filter commands • cut: separate lines into fields – Syntax:- cut -c4 file.txt – Explanation : The above cut command prints the fourth character in each line of the file – ( http://www.folkstalk.com/2012/02/cut- command-in-unix-linux-examples.html )
  • 10. sort command • Sort: Sort command in unix or linux system is used to order the elements or text. Sort command has the capability of sorting numerical values and strings. – Syntax :- sort [options] filename – (http://www.folkstalk.com/2012/08/sort- command-examples-in-unix-linux.html )
  • 11. sort options • -b : Ignores leading spaces in each line • -d : Uses dictionary sort order. Conisders only spaces and alphanumeric characters in sorting • -f : Uses case insensitive sorting. • -M : Sorts based on months. Considers only first 3 letters as month. Eg: JAN, FEB • -n : Uses numeric sorting • -R : Sorts the input file randomly. • -r : Reverse order sorting • -k : Sorts file based on the data in the specified field positions. • -u : Suppresses duplicate lines • -t : input field separator
  • 12. Uniq command • Uniq command in unix or linux system is used to suppress the duplicate lines from a file • Syntax:- uniq [option] filename – (http://www.folkstalk.com/search?q=uniq+comm and+in+%3Blinux )
  • 13. uniq options • c : Count of occurrence of each line. • d : Prints only duplicate lines. • D : Print all duplicate lines • f : Avoid comparing first N fields. • i : Ignore case when comparing. • s : Avoid comparing first N characters. • u : Prints only unique lines. • w : Compare no more than N characters in lines
  • 14. wc command • wc command in unix or linux is used to find the number of lines, words and characters in a file. • Syntax :- wc [options] filenames • ( http://www.folkstalk.com/2012/07/wc- command-examples-in-unix-linux.html#more )
  • 15. wc options • -l : Prints the number of lines in a file. • -w : prints the number of words in a file. • -c : Displays the count of bytes in a file. • -m : prints the count of characters from a file. • -L : prints only the length of the longest line in a file.
  • 16. other commands • tee • head • tail • grep
  • 17. tee command • tee :- tee command writes to the STDOUT, and to a file at a time . • The following command writes the output only to the file and not to the screen. – $ ls > filename • The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file. – $ ls | tee filename (http://linux.101hacks.com/unix/tee-command- examples/ )
  • 18. head command • The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the display screen). • head's basic syntax is: – head [options] [file(s)] ( http://www.linfo.org/head.html )
  • 19. Options of head command • -c, --bytes=[-]N – print the first N bytes of each file; with the leading '-', print all but the last N bytes of each file • -n, --lines=[-]N – print the first N lines instead of the first 10; with the lead- ing '-', print all but the last N lines of each file • -q, --quiet, --silent – never print headers giving file names • -v, --verbose – always print headers giving file names
  • 20. tail command • tail :-output the last part of files • Syntax :- – tail [OPTION]... [FILE]... (http://linux.about.com/library/cmd/blcmdl1_tail.ht m )
  • 21. Options of tail command • -c, --bytes=N – output the last N bytes • -f, --follow[={name|descriptor}] – output appended data as the file grows; -f, -- follow, and --follow=descriptor are equivalent • -F – same as --follow=name --retry • -n, --lines=N – output the last N lines, instead of the last 10
  • 22. grep command • grep :- The grep command allows you to search one file or multiple files for lines that contain a pattern. Exit status is 0 if matches were found, 1 if no matches were found, and 2 if errors occurred. • Syntax – grep [options] pattern [files] (http://www.techonthenet.com/unix/basic/grep.ph p )
  • 23. Options of grep command
  • 24. Bash Scripting • Comment start with hash mark (#) and continue to the end of line. #!/bin/bash echo "Hello, world!“ – The first line is known as the “shebang” statement and declares the text file to be a script for interpretation by /bin/bash.
  • 25. Bash Scripting (Continue…) • The kernel looks for this syntax when deciding how to execute the file. From the perspective of the shell spawned to execute the script, the shebang line is just a comment. • If bash were in a different location, we need to adjust this line
  • 26. Bash Scripting (Continue…) • To prepare the file for running, just turn on its execute bit $ chmod +x helloworld $ ./helloworld – Hello, world! • You can also invoke the shell as an interpreter directly: $ bash helloworld – Hello, world! $ source helloworld – Hello, world! • The first command runs helloworld in a new instance of bash, and the second makes your existing login shell read and execute the contents of the file
  • 27. Regular Expression • Regular expressions are powerful, but they cannot recognize all possible grammars. • They are so common that the name is usually shortened to “regex.”
  • 28. The matching process • Code that evaluates a regular expression attempts to match a single given text string to a single given pattern. The “text string” to match can be very long and can contain embedded newlines. • For the matcher to declare success, the entire search pattern must match a contiguous section of the search text. However, the pattern can match at any position. • After a successful match, the evaluator returns the text of the match along with a list of matches for any specially delimited subsections of the pattern.
  • 29. Literal characters • In general, characters in a regular expression match themselves. So the pattern I am the walrus • Matches the string “I am the walrus” and that string only. Since it can match anywhere in the search text, the pattern can be successfully matched to the string “I am the egg man. I am the walrus. Koo koo ka-choo!” However, the actual match is limited to the “I am the walrus” portion. Matching is case sensitive.
  • 31. Example I am the (walrus|egg man). matches either “I am the walrus.” or “I am the egg man.”. This example also demonstrates escaping of special characters (here, the dot). The pattern (I am the (walrus|egg man). ?){1,2} matches any of the following: • I am the walrus. • I am the egg man. • I am the walrus. I am the egg man. • I am the egg man. I am the walrus.
  • 32. PERL PROGRAMMING • Perl created by Larry Wall • More power than bash • Perl does not impose much stylistic discipline on developers, so Perl code written without regard for readability can be cryptic. Perl has been accused of being a write-only language • Perl’s catch phrase is that “there’s more than one way to do it.”
  • 33. PERL PROGRAMMING(Continue…) • Perl statements are separated by semicolons. Comments start with a hash mark (#) and continue to the end of the line. Blocks of statements are enclosed in curly braces.
  • 34. Program #!/usr/bin/perl print "Hello, world!n"; • As with bash programs, you must either chmod +x the executable file or invoke the Perl interpreter directly. $ chmod +x helloworld $ ./helloworld Hello, world!
  • 35. Variables and arrays • three fundamental data types – Scalars (that is, unitary values such as numbers and strings) – Arrays – Hashes (also known as associative Arrays) • Scalar variables start with $, Array variables start with @, and Hash variables start with %
  • 36. Variables and arrays(Continue…) • In Perl, the terms “list” and “array” are often used interchangeably, but it’s more accurate to say that a list is a series of values and an array is a variable that can hold such a list. • The individual elements of an array are scalars, so like ordinary scalar variables, their names begin with $. Array subscripting begins at zero, and the index of the highest element in array @a is $#a. • The array @ARGV contains the script’s command line arguments.
  • 37. Program #!/usr/bin/perl @items = ("socks", "shoes", "shorts"); printf "There are %d articles of clothing.n",$#items + 1; print "Put on ${items[2]} first, then ", join(" and ", @items[0,1]), ".n"; • The output: $ perl clothes There are 3 articles of clothing. Put on shorts first, then socks and shoes.
  • 38. Explanation of the program • Array and string literals – Perl does not strictly require that all strings be quoted so @items = (socks, shoes, shorts); is also same • Function calls – Both print and printf accept an arbitrary number of arguments, and the arguments are separated by commas
  • 39. Explanation of the program • join receives three string arguments: “ and ”, “socks”, and “shoes”. It concatenates its second and subsequent arguments, inserting a copy of the first argument between each pair. The result is “socks and shoes”.
  • 40. Explanation of the program • Type conversions in expressions The magic is in the + operator, which always implies arithmetic. It converts its arguments to numbers and produces a numeric result. Similarly, the dot operator (.), which concatenates strings, converts its operands as needed: "2" . (12 ** 2) yields “2144”.
  • 41. Regular expressions in Perl • Regular expressions in Perl by “binding” strings to regex operations with the =~ operator. e.g if ($text =~ m/ab+c/) { } • checks to see whether the string stored in $text matches the regular expression ab+c. • To operate on the default string, $_ , you can simply omit the variable name and binding operator. • In fact, you can omit the m, too, since the operation defaults to matching
  • 42. Program #!/usr/bin/perl $names = "huey dewey louie"; $regex = '(w+)s+(w+)s+(w+)'; if ($names =~ m/$regex/) { print "1st name is $1.n2nd name is $2.n3rd name is$3.n"; $names =~ s/$regex/2 1/; print "New names are "${names}".n"; } else { print qq{"$names" did not match "$regex".n}; } (http://www.tutorialspoint.com/perl/perl_qq.htm)
  • 43. Output The output: $ perl testregex 1st name is huey. 2nd name is dewey. 3rd name is louie. New names are "dewey huey".
  • 44. Input and output • When you open a file for reading or writing, you define a “filehandle” to identify the channel. • INFILE & OUTFILE are the filehandlers. • The while loop condition is <INFILE>
  • 45. Program #!/usr/bin/perl open(INFILE, "</etc/passwd") or die "Couldn’t open /etc/passwd"; open(OUTFILE, ">/tmp/passwd") or die "Couldn’t open /tmp/passwd"; while (<INFILE>) { ($name, $pw, $uid, $gid, $gecos, $path, $sh) = split /:/; print OUTFILE "$uidt$namen"; }
  • 47. PYTHON SCRIPTING • Python was created by Guido van Rossum • It’s easier to code and more readable than Perl. Python offers a simple-to-understand syntax that is easy to follow even if you didn’t develop the code. If you’re tired of remembering which comparison operators to use, you’ll appreciate Python’s unified approach. • Python also offers additional data types that some system administrators find useful.
  • 48. Simple Program #!/usr/bin/python print "Hello, world!“ Invoke the python interpreter directly: $ chmod +x helloworld $ ./helloworld Hello, world!
  • 49. sys — (System-specific parameters and functions) • import sys (https://docs.python.org/2/library/sys.html)
  • 50. Program #!/usr/bin/python name = 'Gwen' rating = 10 characters = [ 'SpongeBob', 'Patrick', 'Squidward' ] elements = ( 'lithium', 'carbon', 'boron' ) print "name:t%snrating:t%d" % (name, rating) print "characters:t%s" % characters print "elements:t%s" % (elements, ) This example produces the following output: $ python objects name: Gwen rating: 10 characters: ['SpongeBob', 'Patrick', 'Squidward'] elements: ('lithium', 'carbon', 'boron')
  • 51. Loops • The fragment below uses a for…in construct to iterate through the range 1 to 10. for counter in range(1, 10): print counter, Output: 1 2 3 4 5 6 7 8 9
  • 52. Continue… • Both for and while loops can have else clauses at the end. The else clause is executed only if the loop terminates normally, as opposed to exiting through a break statement. • This feature may initially seem counterintuitive, but it handles certain use cases quite elegantly.