Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming.

Similar presentations


Presentation on theme: "CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming."— Presentation transcript:

1 CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming

2 CIT 140: Introduction to ITSlide #2 Topics 1.Numbers in the shell. 2.Here Documents. 3.Reading and parsing files with read. 4.Functions. 5.Debugging

3 CIT 140: Introduction to ITSlide #3 Numeric Data Processing Bourne shell variables Store character strings, not numbers. To perform arithmetic/logic ops Need expr command.

4 CIT 140: Introduction to ITSlide #4 Numeric Data Processing expr args Purpose:Evaluate the expression arguments, ‘args’, and send the result to standard output. Commonly used operators/features: \| return the first expression if it is not null or 0; else return the second expression \& Return the first expression if neither is null or 0;else return 0 =, \>, \>=, Integer comparison operators;equal, \<, \<=,!= greater than, greater than or equal to, less than,less than or equal to, not equal +, -, \*, /, % Integer arithmetic operators, add, subtract, multiply, integer divide (return quotient), remainder

5 CIT 140: Introduction to ITSlide #5 Numeric Data Processing $ var1=10 $ var1=`expr $var1 + 1` $ echo $var1 11 $ var1=`expr $var1 \* $var1` $ echo $var1 121 $ echo `expr $var1 / 10` 12 $ echo `expr $var1 % 10` 1 $ See ~waldenj/cit140/expr_example.sh for more examples.

6 CIT 140: Introduction to ITSlide #6 Counting wtih expr #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: countup number" exit 1 fi count=0 limit=$1 while [ "$count" -lt "$limit" ] do echo -n "$count " count=`expr $count + 1` done echo

7 CIT 140: Introduction to ITSlide #7 Here Documents We use echo for single line printing. What do we do for multi-line printing? We use I/O redirection from our script. Use the previously unused << operator. cat <<HERE Cat’ing a here document will print every line that follows to the screen until it encounters the word following <<. HERE

8 CIT 140: Introduction to ITSlide #8 Here example: disk usage e-mail #!/bin/sh if [ $# -ne 1 ]; then cat <<EOF Usage: mail username Sends a notice mail to username. EOF fi diskusage=`du -k $HOME` mail $1 <<EOF This is a disk usage notice. You are using the following disk space: $diskusage EOF

9 CIT 140: Introduction to ITSlide #9 Reading Files The read command can use I/O redirection. > read line </etc/passwd > echo $line root:x:0:1:Super-User:/:/sbin/sh

10 CIT 140: Introduction to ITSlide #10 Reading Files Loops can use I/O redirection with read. while read line do echo $line done </etc/passwd

11 CIT 140: Introduction to ITSlide #11 Example: cat #!/bin/sh if [ $# -lt 1 ]; then echo "Usage: cat file1 [file2,...]" fi for file in $@ do while read line do echo $line done <$file done

12 CIT 140: Introduction to ITSlide #12 Parsing files using read > cat hosts.sh #!/bin/sh egrep -v "^#" /etc/hosts >hosts.txt while read ipaddress hostname ignore do echo "Host $hostname has IP address $ipaddress." done <hosts.txt rm hosts.txt >./hosts.sh Host localhost has IP address 127.0.0.1. Host zappa has IP address 172.20.20.40. Host mailfe2 has IP address 72.31.101.68. Host axp1 has IP address 110.1.128.3.

13 CIT 140: Introduction to ITSlide #13 Functions function () { commands; } “Mini-scripts” inside your script Invoked like another shell script. Handle arguments like shell scripts do. Exit status is exit status of last command.

14 CIT 140: Introduction to ITSlide #14 Function Example #!/bin/sh findstr() { dir=$1 string=$2 find $dir -type f -print | xargs fgrep $string } if [ $# -ne 2 ]; then echo "Usage: findstring dir string" exit 1 fi findstr $1 $2

15 CIT 140: Introduction to ITSlide #15 Function Reuse Example #!/bin/sh backupfile() { target=$1 if [ -f $target ]; then cp -p $target $target.bak fi } if [ $# -lt 1 ]; then echo "Usage: backupdir dir" exit 1 fi cd $1 for file in * do backupfile $file done

16 CIT 140: Introduction to ITSlide #16 Why use Functions? 1.Organize your program. –Break up program into pieces that fit on one screen so you can read and understand it. 2.Code reuse –Can call a function multiple times. –DRY (Don’t Repeat Yourself) Principle.

17 CIT 140: Introduction to ITSlide #17 Debugging Shell Programs Noexec: check errors but don’t run > sh –n script set –o noexec Verbose trace (can be combined with –n) > sh –v script set –o verbose Short trace > sh –x script set –o xtrace

18 CIT 140: Introduction to ITSlide #18 Debugging Shell Scripts > sh –n backupdir.sh tmp > sh -x backupdir.sh tmp + [ 1 -lt 1 ] + cd tmp + backupfile crypt.txt target=crypt.txt + [ -f crypt.txt ] + cp -p crypt.txt crypt.txt.bak + backupfile lcdays.txt target=lcdays.txt + [ -f lcdays.txt ] + cp -p lcdays.txt lcdays.txt.bak + backupfile who.txt target=who.txt + [ -f who.txt ] + cp -p who.txt who.txt.bak


Download ppt "CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming."

Similar presentations


Ads by Google