1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai...
-
Upload
tyrone-walsh -
Category
Documents
-
view
221 -
download
1
Transcript of 1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai...
1P51UST: 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
Introduction
• More input/output– echo
– printf
• Flow control
• Arithmetic
P51UST: Unix and Software Tools
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
3P51UST: Unix and Software Tools
echo
• If -e option is used, the following formatting characters can be used
4P51UST: Unix and Software Tools
Option Function
\b Backspace
\n New line
\t Horizontal tab
\v Vertical tab
\a Alert (bell)
\c Suppress trailing newline
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]specifieroFlags -: 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
5P51UST: Unix and Software Tools
Printf – specifier table
• Examples:#!/bin/sh
name=“Ruibin”
printf “Hello %-10s, average=%+7d\n” $name 30
6P51UST: Unix and Software Tools
Specifier
Output Example
s String of characters sample1
d or i Signed decimal integer 342
f Decimal floating point 342.65
e (or E) Scientific notation (mantise/exponent) using e (or E) character
3.4265e+23.4265E+2
% A % followed by another % character will write % to stdout
%
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
7P51UST: Unix and Software Tools
Conditionals
if some conditionthencommand1
command2[else
command3...]fi
Example:
if [ $# -gt 1 ]
then
echo “Multi-arguments…”
fi
8P51UST: Unix and Software Tools
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
9P51UST: Unix and Software Tools
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
10P51UST: Unix and Software Tools
Test File Conditions
• Some common tests on files/directories
11P51UST: Unix and Software Tools
Condition Meaning (if True)
-d file File exists and is a directory
-f file File exists and is a regular file.
-r file File exists and is readable
-w file File exists and may be written to
-x file File exists and is executable
-s file File exists and has a size greater than zero.
String Comparisons
• String tests
• N.B: equality test vs assignment – white space before and after “=”
12P51UST: Unix and Software Tools
Condition Meaning (if True)
-n str1 Str1 has a length greater than 0
-z str1 str2 is a length of 0 bytes.
s1 = s2 s1 is equal to s2
s1 != s2 s1 is not equal to s2
Numerical Comparison
• In sh, all mathematical comparisons are integer comparisons.
• sh does not natively deal with floating-point numbers.
13P51UST: Unix and Software Tools
Condition Meaning (if True)
n1 -eq n2 n1 is equal in value to n1
n1 -le n2 Less than or equal
n1 -lt n2 Less than
n1 -gt n2 Greater than
n1 -ge n2 Greater than or equal
n1 -ne n2 Not equal
loops
• for loop
for variable [in list]do command1 command2 ...done
• while loop
while conditiondo
command1 command2 ...done
14P51UST: Unix and Software Tools
Loops - examples
#!/bin/shfor file in *.shdo if test ! -r $file then echo “$file is not
readable.” else echo “$file is
readable” fidone
#!/bin/shvalue=$1while test $value -gt 0do echo “\$value= $value” value=`expr $value - 1`done
15P51UST: Unix and Software Tools
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
expr 10/ 3
expr 10 /3
16P51UST: Unix and Software Tools
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
17P51UST: Unix and Software Tools
Comparisons using expr
• Exampleequal=`expr $1 = “ruibin”`
flag=`expr $1 > 30`
Other operators:
!= not equal
< less
>= greater than or equal to
<= less than or equal to
18P51UST: Unix and Software Tools
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 (+).
19P51UST: Unix and Software Tools
Your .bash_profile is a shell script
# set up personal bin directoriesPATH=$HOME/bin:$PATH:EDITOR=emacsexport PATH TERM EDITORDEFTERM=vt100ASKTERM=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.
20P51UST: Unix and Software Tools