lecture 20 shell programming ch 10 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 20: Shell Programming ( ch 10) PowerPoint Presentation
Download Presentation
Lecture 20: Shell Programming ( ch 10)

Loading in 2 Seconds...

play fullscreen
1 / 20

Lecture 20: Shell Programming ( ch 10) - PowerPoint PPT Presentation


  • 148 Views
  • Uploaded on

Lecture 20: Shell Programming ( ch 10). IT244 - Introduction to Linux / Unix Instructor: Bo Sheng. Expressions. Logical operators true, false true; echo $?; false; echo $? !, ==, != ((1==1)); echo $?; echo $((1==1)) ((1!=1)); echo $?; echo $((1!=1)) &&, || true && false; echo $?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 20: Shell Programming ( ch 10)' - zev


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 20 shell programming ch 10

Lecture 20: Shell Programming (ch 10)

IT244 - Introduction to Linux / Unix

Instructor: Bo Sheng

expressions
Expressions
  • Logical operators
    • true, false
      • true; echo $?; false; echo $?
    • !, ==, !=
      • ((1==1)); echo $?; echo $((1==1))
      • ((1!=1)); echo $?; echo $((1!=1))
    • &&, ||
      • true && false; echo $?
      • true || false; echo $?
      • ls –l testop && cat testop; echo $?
      • ls –l testop || cat testop; echo $?
expressions1
Expressions
  • Logical operators
    • &&, ||
      • Short-circuiting:
        • Cond1 && Cond2: does not evaluate Cond2 if Cond1 is false
        • Cond1 || Cond2: does not evaluate Cond2 if Cond1 is true
      • Example
        • mkdirbkup && cp testopbkup
        • rmbkup/*
        • mkdirbkup && cp testopbkup; lsbkup
        • mkdirbkup || echo “mkdir failed”
        • a=1;b=2;echo $((a||b++));echo $b
summary
Summary
  • Arguments
    • $#, $0-$n, $@, $*
    • shift, set
  • Handle exceptions
    • Sufficient number of arguments ($#)
    • Valid arguments (e.g., -d)
    • Valid options/choices (select)
    • Null/unset variables (use/set a default value, err msg)
summary1
Summary
  • Special variables
    • $$, $?
  • More
    • trap
    • Read user input
    • Array
    • Short-circuiting
slide6
HW8
  • Question 1
    • The option is specified before arguments
    • The end of a file
      • read < file line
      • Exit status is false
      • $line is “”
introduction
Introduction
  • Perl
    • Practical Extraction and Report Language
    • Larry Wall, 1987
    • Scripting Language
      • Scheme, Python, Tcl, Ruby, …
      • Interpreted
get started
Get Started
  • Print out
    • ~it244/it244/lec20/simple.pl
    • perl simple.pl
    • chmoda+x simple.pl
    • ./simple.pl
    • ./simple2.pl
    • perl –e ‘print “hi\n”’
get started1
Get Started
  • Basic syntax
    • Semicolon(;) at the end of each statement
    • Double/single quote
      • ~it244/it244/lec20/quote.pl
    • Special characters (Table 11-1)
      • \n, \t, \
get started2
Get Started
  • Variables
    • Scalar (singular)
      • $name=“Sam”;
      • ~it244/it244/lec20/scalars.pl
    • Array (plural)
      • @array=(1, 2, “Sam”, “Max”);
        • $array[1], @array[1,2], @array[1..3]
          • ~it244/it244/lec20/arrays.pl
        • @array, @#array
          • ~it244/it244/lec20/arrays2.pl
control structure
Control Structure
  • if, if..else, if..elsif..else

if (condition) {…}

    • Comparison (numbers)
      • ==,!=,<,>, …
      • ~it244/it244/lec20/age.pl
      • $var=<>
control structure1
Control Structure
  • if, if..else, if..elsif..else

if (condition) {…}

    • Compare strings
      • str1 eq str2
      • perl –e ‘if (“aaa” eq “bbb”) {print “true\n”;} else {print “false\n”;}’
    • defined($var)
      • ~it244/it244/lec20/defined.pl
control structure2
Control Structure
  • for/foreach

foreach $item (“mo”, “larry”, “curly”){

say “$item says hello.”;

}

foreach (“mo”, “larry”, “curly”){

say “$_ says hello.”;

}

~it244/it244/lec20/foreach.pl

control structure3
Control Structure
  • while/until

while (condition) {…}

~it244/it244/lec20/while1.pl

$./while1.pl

$./while1.pl while1.pl

    • $.:number of input lines, $_:default operant
    • ~it244/it244/lec20/while2.pl
  • last and next
    • break and continue
file operation
File Operation
  • File handle (*file descriptor)

open (file-handle, [‘mode’], “file-ref”)

*exec file-descriptor <& filename

    • File-handle: a variable, a number
    • Mode: <, >, >>, ‘read’ by default if not specified
    • File-ref: file name
file operations
File Operations
  • Read from files

open (file-handle, [‘mode’], “file-ref”)

$line=<file-handle> $line=<>

~it244/it244/lec20/readfiles.pl

~it244/it244/lec20/readfiles2.pl

file operations1
File Operations
  • Write to files

open (file-handle, [‘mode’], “file-ref”)

print file-handle “text”

*echo “text” >&file-descriptor

~it244/it244/lec20/writefiles.pl

file operations2
File Operations
  • Special file handles (<>, STDOUT, STDERR)
    • ~it244/it244/lec20/writefiles2.pl
      • ./writefiles2.pl >out.log 2>err.log
      • ./writefiles2.pl log >out.log 2>err.log
  • Chop a string (chomp/chop)
    • chomp: remove the newline
    • chop: remove any trailing character
    • ~it244/it244/lec20/chopstr.pl
file operations3
File Operations
  • Command-line arguments
    • @ARGV
      • ~it244/it244/lec20/args.pl
      • ./args.pl a b c
    • $ARGV (<>)
      • ~it244/it244/lec20/file3.pl
      • ./file3.pl file1 file2 file3