200 likes | 388 Views
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 $?
E N D
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 $? • true || false; echo $? • ls –l testop && cat testop; echo $? • ls –l testop || cat testop; echo $?
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 • 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)
Summary • Special variables • $$, $? • More • trap • Read user input • Array • Short-circuiting
HW8 • Question 1 • The option is specified before arguments • The end of a file • read < file line • Exit status is false • $line is “”
Introduction • Perl • Practical Extraction and Report Language • Larry Wall, 1987 • Scripting Language • Scheme, Python, Tcl, Ruby, … • Interpreted
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 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 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 • if, if..else, if..elsif..else if (condition) {…} • Comparison (numbers) • ==,!=,<,>, … • ~it244/it244/lec20/age.pl • $var=<>
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 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 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 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 • Read from files open (file-handle, [‘mode’], “file-ref”) $line=<file-handle> $line=<> ~it244/it244/lec20/readfiles.pl ~it244/it244/lec20/readfiles2.pl
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 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 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