1 / 20

Lecture 20: Shell Programming ( ch 10)

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 $?

zev
Download Presentation

Lecture 20: Shell Programming ( ch 10)

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 20: Shell Programming (ch 10) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng

  2. 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 $?

  3. 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

  4. 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)

  5. Summary • Special variables • $$, $? • More • trap • Read user input • Array • Short-circuiting

  6. HW8 • Question 1 • The option is specified before arguments • The end of a file • read < file line • Exit status is false • $line is “”

  7. Perl Programming (ch 11)

  8. Introduction • Perl • Practical Extraction and Report Language • Larry Wall, 1987 • Scripting Language • Scheme, Python, Tcl, Ruby, … • Interpreted

  9. 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”’

  10. 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, \

  11. 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

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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. File Operations • Write to files open (file-handle, [‘mode’], “file-ref”) print file-handle “text” *echo “text” >&file-descriptor ~it244/it244/lec20/writefiles.pl

  19. 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

  20. 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

More Related