Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
COMP234 Perl Printing Special Quotes File Handling.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Guide To UNIX Using Linux Third Edition
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Script & Perl Programming. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any standard.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Shell Script & Perl Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Agenda Control Flow Statements Purpose test statement
Linux Shell Script Programming
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell Script & Perl Programming Lesson 9

Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any standard editor. You can then execute the commands in the file by using the filename as an argument to any dot (.) command.

Our first shell script $ vi helloscript i# this is a simple script echo “hello world” :w :q $./helloscript hello world $

Variables $ greeting=“hello” $ echo $greeting hello $ echo greeting greeting $ The set command will let you view all of your currently defined variables.

Some interesting variables Use the set command to see some of your defined variables (e.g. after you assign the value to “greeting” in previous slide). Also check out some other important variables (type “set | more”) PATH PS1

Starting to customize your settings In your home directory (“/home/ ) create a file called ‘.bash_profile’ Insert into this two lines: PATH=/usr/local/sbin:/usr/bin:$PATH:$HOME/bin:. PS1=‘> ‘ The first tells the system where to look for programs (commands) to execute, the second sets your prompt Default probably PS1=‘\s-\v\$ ‘, use set to see \s tells it to print what shell you are using \v tells it to print what version $ is the prompt you are telling it to use

Another variable example > school=“UTSA” > whereat=“you are at $school” > echo $whereat you are at UTSA > What would happen if > echo whereat whereat >

Variables and scripts > vi greetvar echo Please enter a greeting: read greeting echo “The greeting you entered was $greeting” #note optional quote > greetvar Please enter a greeting: howdy The greeting you entered was howdy >

Arguments to scripts > vi greetargs echo The first argument of $0 is: $1 echo the second argument is: $2 echo the third: $3 echo and finally: $4 > greetargs num1 number2 N3 “simply N4” The first argument of./greetargs is: num1 the second argument is: number2 the third: N3 and finally: simply N4 > greetargs num1 num2 num3 The first argument of./greetargs is: num1 the second argument is: num2 the third: num3 and finally: >

Some simple math > vi add1 let “num = $1” # quotes important let “num = num + 1” echo $num > add1 2 3 >

Some loops > vi loop3 again = 1 while let “again <= 3 “ # you need the let do echo $again howdy let “again = again + 1 “ done > loop3 1 howdy 2 howdy 3 howdy >

Conditional statements > vi myls if [ $1 = a ] # watch spaces, need after [ and before ] then ls -al # lists all files including hidden else ls -l # lists files in long format fi > myls a > myls x > myls./myls: [: =: unary operator expected >

Reading from a file > vi printlist read name while [ $name != end ] do echo $name read name done > printlist < list Line1 Line2 Line3 > printlist list #will have to ^c > vi list Line1 Line2 Line3 end >

Script for adding users #this is a script to create a bunch of accounts read grpid read name let "userid = grpid + 1“ while [ "$name" != end ] do echo $name $userid $grpid adduser -u $userid -g $grpid -d /home/$name -p "" -r $name mkdir /home/$name chown $name /home/$name let "userid = userid + 1" read name done THE FILE 500 Abarshay Jbell Abelvis end

Perl Practical Extraction and Report Language Designed to provide convenience of shell scripts and power and flexibility of a programming language. Perl programs are interpreted and executed directly like shell scripts. Similar in many respects to the C programming language Freeware, available from Free Software Foundation (comes with Linux)

Our first Perl Program $ vi sample #!/usr/bin/perl $inputline = ;# input text print ("$inputline");# output the text $./sample test line $ - # symbol indicates comment, except #! in first line which indicates the location of the program interpreter. - notice each line ends with a semi-colon - variable treated in a similar manner as in shell scripts

Quotes and Escape Sequences #!/usr/bin/perl $x = "a string"; $y = "This is $x"; # becomes "This is a string” $z = 'This is $x'; # remains 'This is $x’ print ("$x\n"); print ("$y\n"); print ("$z\n"); print ('$z\n'); print ("\a\LFREDDIE \Umary\n"); print ("a quote \"in a string\"\n"); $./sample a string This is a string This is $x $z\nfreddie MARY a quote "in a string” $

Some more escape sequences \aBell (beep) \bbackspace \eescape \Ecancel effect of \L, \U, \Q \Lall following letters are lowercase \nnewline \Qdo not look for special pattern characters \rcarriage return \ttab \Uall following letters are uppercase \vvertical tab

Performing Math $ vi math #!/usr/bin/perl $a = 15; print ("$a\n"); $a = ; print ("$a\n"); $a = ; print ("$a\n"); $a = 2.1 * 6; print ("$a\n"); $a = 48 / 1.5; print ("$a\n"); $a = 2 ** 3; print ("$a\n"); $a = 21 % 5; print ("$a\n"); $a = - $a; print ("$a\n"); $ $./math $

Performing Comparisons 11.0 < 16# less than 16 > 11# greater than 15 == 15# equals 11.0 <= 16# less than or equal to 16 >= 11# greater than or equal to 15 != 14# not equal to $a || $b# logical OR; true if either is non-zero $a && $b# logical AND; true only if both are non-zero ! $a# logical NOT; true if $a is zero 4 1# returns 1; 4> # returns 0; they are equal 1 4.0# returns -1; 1<4

Performing Comparisons - strings “aaa” lt “bbb”# less than “bbb” gt “aaa”# greater than “aaa” eq “aaa”# equals “aaa” le “bbb”# less than or equal to “bbb” ge “aaa”# greater than or equal to “aaa” ne “bbb” # not equal to “aaa” cmp “bbb”# similar to, returns 1; “aaa” < “bbb” “aaa” cmp “aaa”# returns 0 “bbb” cmp “aaa”# returns -1; “bbb” > “aaa” What would be returned for “40” lt “8”

Some assignment operators $a = 9;# most common assignment operator $a += 1;# equivalent to $a = $a + 1; $a -= 1;# same as $a = $a - 1; ++$a# same as $a += 1; $a++# same as $a += 1; --$a# same as $a -= 1; $a--# same as $a -= 1; be careful on the last four of these $a = 7; $b = ++$a;# $a and $b are both set to 8 $b = $a++;# $a is now 8, but $b is 7

Some cool things to do with strings The. operator joins the second operand to the first operand: $a = “be”. “witched”;# $a is now “bewitched” The x operator makes n copies of a string: $a = “t” x 5;# $a is now “ttttt” The =~ operator matches patterns and can substitute with s: $x =~ /jkl/ # returns true if “jkl” is in $x $val =~ s/abc/def/;#replaces abc with def The * character matches 0 or more, ? Matches zero or 1 copy /jk*l/# matches jl, jkl, jkkl, jkkkl, … /jk?l/# matches jl or jkl only

Lists and Arrays A list is a collection of values enclosed in parentheses. ( 1, 5.3, “hello”, 2)# contains four values ()# an empty list Perl allows you to store lists in array = (1, 2, character specifies var as an = (11, “my string”, = is now a list of one element # the value of $y Obtaining the length of a = (“string1”, “string2”, “string3”); $y $y contains the LENGTH

More about lists and arrays Array = (1, 2, = gets slice containing 2 # values (1, 2). NOTE, position = (1, 2, 5] = (10, now has 6 elements, the # fourth being a null = (10, 20, 30); $y $y now has the value = (10, “hello”); $y = to your friends”;

More fun with lists and arrays Using the built-in function = (“this”, “is”, “a”, = sort is now (“a”, “is”, “test”, “this”) Note that sort() is an ALPHABETIC sort, = (100, 50, = sort is really (“100”, “2”, “50”) The function reverse() reverse the = (“backwards”, “is”, “array”, = reverse is now # (“this”, “array”, “is”, “backwards”)

Some final list and array functions To create a single string from a list or array variable use = join(“ “, “this”, “is”, “a”, “sentence”); The first element contains the character to “glue” the rest is thus “this is a sentence” You’d get the same results = (“this”, “is”, = join (“ “sentence”); To undo the effects of join(), use = = split(/::/, $y); is now # (“words”, “separated”, “by”, “colons”)

Using Command-line arguments $ vi printfirstarg #!/usr/bin/perl Print(“the first argument is $ARGV[0]\n”); $ printfirstarg The first argument is 1 $ Note that $ARGV[0], the first element of the $ARGV array variable, does NOT contain the name of the program. This is a difference between Perl and C.

Controlling program flow if ($x == 14) { print(“\$x is 14\n”); } Two way branching if ($x == 14) { print(“\$x is 14\n”); } else { print(“\$x is not 14\n”); }

Files To open a file, use the function open(); open(MYFILE, “/home/gbwhite/testfile”); The second argument is the name of the file you want to open. You can supply the full pathname or just the filename itself. By default, Perl assumes you will open a file for reading. If you want to open one for writing, put a > character in front of your filename: open(MYFILE, “>/home/gbwhite/writefile”); If you want to append instead of overwriting, use >>

More about handling files The open() function returns true if it succeeded, false otherwise. You can use the || (logical OR) function to test it: open(MYFILE, “/home/gbwhite/testfile”) || die(“unable to open /home/gbwhite/testfile\n”); This works because the right side of a logical OR only is executed if the left side is false.

More about files To read from a file, enclose the name in angle brackets: $line = ; This statement reads a line of input. To write to a file, print MYFILE (“this is a test\n”, “this too\n”); Closing a file: close(MYFILE);

Subroutines Subroutines can appear anywhere in the program but by convention generally go at the end. #!/usr/bin/perl $thecount = 0; &getwords; while ($words[0] ne “”) { # stop when line is empty for ($index = 0; $words[$index] ne “”; $index += 1) { $thecount +=1 if $words[$index] eq “the”; #above line really  if($words[$index] eq “the”) {$thecount +=1;} } &getwords; } print (“Total number of occurrences of the: $thecount\n”); sub getwords{ $inputline = = split(/\s+/, $inputline); }

Reading from and Writing to Files To access a file on a UNIX file system from within your Perl program, you must perform the following steps: Your program must open the file. This tells the system that your Perl program wants to access the file. The program can either read from or write to the file, depending on how you have opened the file. The program can close the file. This tells the system that your program no longer needs access to the file.

Looping Constructs $ vi loops #!/usr/bin/perl $x = 1; while ($x <= 5) { print ("\$x is now $x\n"); ++$x; } until ($x <= 0) { print ("and \$x is now $x\n"); --$x; } $ $./loops $x is now 1 $x is now 2 $x is now 3 $x is now 4 $x is now 5 and $x is now 6 and $x is now 5 and $x is now 4 and $x is now 3 and $x is now 2 and $x is now 1 $