Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.

Similar presentations


Presentation on theme: "1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University."— Presentation transcript:

1 1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham Ningbo, China

2 2 Introduction More input/output –echo –printf Flow control Arithmetic P51UST: Unix and Software Tools

3 echo Displays a line of text to standard output. echo [options] [string] Options may be different in different shells Common options: -n suppress the trailing line (we’ve used this already) -e enable interpretation of backslash escapes 3 P51UST: Unix and Software Tools

4 echo If -e option is used, the following formatting characters can be used 4 P51UST: Unix and Software Tools OptionFunction \bBackspace \nNew line \tHorizontal tab \vVertical tab \aAlert (bell) \cSuppress trailing newline

5 Printf A powerful string formatting and printing command Similar to printf in C, it interprets % and / metacharacters. printf format[arguments] where format: %[flags][width][.precision]specifier oFlags -: left-justify +: Forces to proceed the result with a plus or minus sign oWidth : minimum number of characters to be printed oPrecision : the number of digits to be printed after the decimal point oSpecifier : is the most significant one and defines the type and the interpretation of the value of the corresponding argument 5 P51UST: Unix and Software Tools

6 Printf – specifier table Examples: #!/bin/sh name=“Ruibin” printf “Hello %-10s, average=%+7d\n” $name 30 6 P51UST: Unix and Software Tools SpecifierOutputExample sString of characterssample1 d or iSigned decimal integer342 fDecimal floating point342.65 e (or E)Scientific notation (mantise/exponent) using e (or E) character 3.4265e+2 3.4265E+2 %A % followed by another % character will write % to stdout %

7 Flow control Flow control are used to control the order in which execution happens. Common structures are conditionals (if-then-else) and loops (for loops) Keywords should appear at the start of a line 7 P51UST: Unix and Software Tools

8 Conditionals if some condition then command1 command2 [else command3...] fi Example: if [ $# -gt 1 ] then echo “Multi-arguments…” fi 8 P51UST: Unix and Software Tools

9 Representing Conditions 1.Using square bracket (like the previous example) –Careful with the space near [ and ] Using keyword test –For example the previous example can also be written as follows if test $# -gt 1 then echo “Multi-arguments…” fi 9 P51UST: Unix and Software Tools

10 More About test The test command can be used to evaluate three different general types of conditions –File conditions –String comparisons and conditions –Integer comparisons For example: if test –f $1 then echo –e “$1 is a regular file\n” fi 10 P51UST: Unix and Software Tools

11 Test File Conditions Some common tests on files/directories 11 P51UST: Unix and Software Tools ConditionMeaning (if True) -d fileFile exists and is a directory -f fileFile exists and is a regular file. -r fileFile exists and is readable -w fileFile exists and may be written to -x fileFile exists and is executable -s fileFile exists and has a size greater than zero.

12 String Comparisons String tests N.B: equality test vs assignment – white space before and after “=” 12 P51UST: Unix and Software Tools ConditionMeaning (if True) -n str1Str1 has a length greater than 0 -z str1str2 is a length of 0 bytes. s1 = s2s1 is equal to s2 s1 != s2s1 is not equal to s2

13 Numerical Comparison In sh, all mathematical comparisons are integer comparisons. sh does not natively deal with floating-point numbers. 13 P51UST: Unix and Software Tools ConditionMeaning (if True) n1 -eq n2n1 is equal in value to n1 n1 -le n2Less than or equal n1 -lt n2Less than n1 -gt n2Greater than n1 -ge n2Greater than or equal n1 -ne n2Not equal

14 loops for loop for variable [in list] do command1 command2... done while loop while condition do command1 command2... done 14 P51UST: Unix and Software Tools

15 Loops - examples #!/bin/sh for file in *.sh do if test ! -r $file then echo “$file is not readable.” else echo “$file is readable” fi done #!/bin/sh value=$1 while test $value -gt 0 do echo “\$value= $value” value=`expr $value - 1` done 15 P51UST: Unix and Software Tools

16 Arithmetic Bourne shell does integer math. Therefore, suppose I want to divide 10 by 3, the result is 3. The command is expr expr v1 operator v2 where operator can be + add - subtract * multiply / divide Examples: Correct format expr 10 / 3 expr 3 + 3 But not expr 10/3 16 P51UST: Unix and Software Tools

17 Command Substitution To store the math results into a variable, we need command substitution. Use backquotes “`” enclose the command. Also remember to protect meta-character “*” Example: v1=`expr 25 \* 6` v2=`expr $v1 / 3` v3=`expr $v1 ‘*’ $v2` v4=`expr $v1 + $v3` $echo $v1, $v2, $v3, $v4 17 P51UST: Unix and Software Tools

18 Comparisons using expr Example equal=`expr $1 = “ruibin”` flag=`expr $1 > 30` Other operators: != not equal < less >=greater than or equal to <=less than or equal to 18 P51UST: Unix and Software Tools

19 Debugging in Shell Programming To debug the shell script, you need to invoke shell using the following format sh [options] [arguments] where options -v(verbose) : show each line of the script -x(executed): display each command it executed, preceded by a plus sign (+). 19 P51UST: Unix and Software Tools

20 Your.bash_profile is a shell script # set up personal bin directories PATH=$HOME/bin:$PATH: EDITOR=emacs export PATH TERM EDITOR DEFTERM=vt100 ASKTERM=false ~/. bash_profile is used by each user to enter information that is specific to his or her own use. It is executed only one when users log in. However, ~/.bashrc contains information that is specific to your bash shells. It is executed each time you open a new bash shell. 20 P51UST: Unix and Software Tools


Download ppt "1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University."

Similar presentations


Ads by Google