0% found this document useful (0 votes)
8 views42 pages

ch10shell编程

Uploaded by

yueli98354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views42 pages

ch10shell编程

Uploaded by

yueli98354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

10 Sh Programming

10.1 sh Scripts
A sh script is a text file containing sh statements for the command
interpreter sh to execute.
We may create a text file, mysh, containing
#! /bin/bash
# comment line
echo hello
Use chmod +x mysh to make it executable. Then run mysh.
The first line of a sh script usually begins with the combination of #!,
which is commonly called a shebang.
When the main sh sees the shebang, it reads the program name for
which the script is intended and invokes that program.
There are many different versions of sh, e.g. bash of Linux, csh of BSD
Unix and ksh of IBM AIX, etc.
The shebang allows the main sh to invoke the proper version of sh to
execute the script.
If the shebang is not specified, it runs the default sh, which is /bin/bash
in Linux.
10.2 sh Scripts vs. C Programs

sh is an interpreter, which reads a sh script file line by line and executes the
lines directly.
If a line is an executable command, sh executes the command directly if it’s a
built-in command.
Otherwise, it forks a child process to execute the command and waits for the
child to terminate before continuing, exactly as it does a single command line.
In sh scripts, everything is string, there is only one type, namely strings.
10.3 Command-line parameters
A sh script can be invoked with parameters exactly as running a sh
command, as in
mysh one two three
Inside the sh script, the command line parameters can be accessed by
the position parameters $0, $1, $2, etc.
The first 10 command line parameters can be accessed as $0 to $9.
Other parameters must be referenced as ${10} to ${n} for n>=10.
In sh the built-in variables $# and $* can be used to count and display
the command-line parameters
$# = the number of command-line parameters $1 to $n
$* = ALL command-line parameters
In addition, sh also has the following built-in variables related to
command executions.
$$ = PID of the process executing the sh
$? = last command execution exit status (0 if success, nonzero
otherwise)
Example Assume the following mysh script is run as
#! /bin/bash
echo \$# = $# # $# = 12
echo \$* = $* # $* = abc D E F G H I J K L M N
echo $1 $9 $10 # abc K abc0 (note: $10 becomes abc0)
echo $1 $9 ${10} # abc K L (note: ${10} is L)
shift # replace $1, $2 .. with $2, $3,...
echo $1 $9 ${10} #DLM
10.4 Sh Variables
Sh has many built-in variables, such as PATH, HOME, TERM, etc.
In addition to built-in variables, the user may use any symbol as sh
variable.
No declaration is necessary. All sh variable values are strings.
An unassigned sh variable is the NULL string. A sh variable can be set
or assigned a value by
variable=string # NOTE: no white spaces allowed between tokens
If A is a variable, then $A is its value.
10.5 Quotes in sh
Sh has many special chars, such as $, /, *, >, <, etc. To use them as
ordinary chars, use \ or single quotes to quote them.

In general, \ is used to quote single chars. Single quotes are used to


quote long strings.
Substitution will occur within double quotes.
10.6 sh Statements
sh statements include all Unix/Linux commands, with possible I/O
redirections.

The sh programming language also supports statements for testing conditions,


loops and cases, etc. which controls the executions of sh programs.
10.7 sh Commands
10.7.1 Built-in Commands
sh has many built-in commands, which are executed by sh without
forking a new process.
The read Command: When sh executes the read command, it waits for
an input line from stdin.
It divides the input line into tokens, which are assigned to the listed
variables.
The commonly used built-in sh commands.
10.7.2 Linux Commands
some commands have become almost an integral part of sh because they
are used extensively in sh scripts.
The echo Command: echo simply echoes the parameter strings as lines
to stdout. It usually condenses adjacent white spaces into a single space
unless quoted.
The expr Command: Since all sh variables are strings, we can not
change them as numerical value directly. For example,
I=123 # I assigned the string “123”
I=I + 1 # I assigned the string “I + 1”
Changing the (numerical) values of sh variables can be done indirectly
by the expr command.
expr string1 OP string2 # OP = any binary operator on numbers

I=123
I=$(expr $I + 1)
changes I from “123” to “124”.
The pipe Command: Pipes are used very often in sh scripts to act as
filters.
ps –ax | grep httpd
cat file | grep word
Utility Commands: sh also uses many other utility programs as
commands. These include
10.8 Command Substitution
In sh, $A is substituted with the value of A.
When sh sees `cmd` (in grave quotes) or $(cmd),
it executes the cmd first and substitutes $(cmd) with the result string of
the execution.
echo $(date) # display the result string of date command
echo $(ls dir) # display the result string of ls dir command

Command substitution is a very powerful mechanism.


10.9 Sh Control Statements
10.9.1 if-else-fi statement
if [ condition ] # NOTE: must have white space between tokens
then
statements
else # as usual, the else part is optional
statements
fi # each if must end with a matching fi
Each statement must be on a separate line.
However, sh allows multiple statements on the same line if they are
separated by semi-colon ;.
if [ condition ]; then
statements
else
statements
fi
By default, all values in sh are strings, so they can be compared as strings by
if [ s1 = s2 ] # NOTE: white spaces needed between tokens
if [ s1 != s2 ]
if [ s1 \< s2 ] # \< because < is a special char
if [ s1 \> s2 ] etc. # \> because > is a special char
The left bracket symbol [ is actually a test program, which is executed
as
test string1 COMP string2 OR [ string1 COMP string2 ]
It is important to note that in sh 0 is TRUE and nonzero is FALSE,
which are exactly opposite of C.
The user may also use the sh built-in variable $? to test the exit status of
the last command execution.
The operators -eq, -ne, -lt, -gt , etc. compare parameters as integer
numbers.
if [ "123" = "0123" ] is false since they differ as strings
if [ "123" -eq "0123" ] is true since they have the same numerical value
the TEST program can also test file types and file attributes, which are
often needed in file operations.
if [ -e name ] # test whether file name exists
if [ -f name ] # test whether name is a (REG) file
if [ -d name ] # test whether name is a DIR
if [ -r name ] # test whether name is readable; similarly for -w,
-x, etc.
if [ f1 -ef f2 ] # test whether f1, f2 are the SAME file

Compound if-elif-else-fi statement: This is similar to if-else if-else in C


except sh uses elif instead of else if. The syntax of compound if-elif-
else-fi statement is
Compound Conditions: As in C, sh allows using && (AND) and || (OR)
in compound conditions, but the syntax is more rigid than C.
The conditions must be enclosed in a pair of matched double
brackets, [[ and ]].
if [[ condition1 && condition2 ]]; then
if [[ condition1 && condition2 || condition3 ]]; then
As in C, compound conditions can be grouped by ( ) to enforce their
evaluation orders.
if [[ expression1 && (expression2 || expression3) ]]; then
10.9.2 for Statement
The for statement in sh behaves as for loop in C.
for VARIABLE in string1 string2 .... stringn
do
commands
done
On each iteration, the VARIABLE takes on a parameter string value and
execute the commands between the keywords do and done.
10.9.3 while Statement
The sh while statement is similar to while loop in C
while [ condition ]
do
commands
done
10.9.4 until-do Statement
This is similar to the do-until statement in C.
10.9.5 case Statement
This is similar to the case statement in C also, but it is rarely used in sh
programming.
10.9.6 continue and break Statements
As in C, continue restarts the next iteration of the nearest loop and break
exits the nearest loop.
They work exactly the same as in C.
10.10 I/O Redirection
When entering a sh command, we may instruct sh to redirect I/O to files
other than the default stdin, stdout, sterr.
I/O redirections have the following form and meaning:
> file stdout goes to file, which will be created if non-existing.
>> file stdout append to file
< file use file as stdin; file must exist and have r permission.
<< word
take inputs from "here" file until a line containing only “word"
10.11 Here Documents
command << token
string
token
Is equivalent to
echo string | command

These are commonly known as here documents.


10.12 sh Functions
sh functions are defined as
func()
{
# function code
}
all functions in a sh script must be defined before any executable
statements.
The sh statement
func s1 s2 ... sn
calls the sh func, passing as parameters (strings) s1 to sn.
Inside the called function, the parameters are referenced as $0, $1 to $n.
As usual, $0 is the function name, and $1 to $n are position parameters
corresponding to the command-line parameters.
When a function execution finishes, $? is its exit status, which is 0 for
success and nonzero otherwise.
The value of $? can be changed by the explicit return value of a
function.
Examples of sh functions
10.13 Wild Cards in sh
Star Wildcard: The most useful wildcard in sh is the *, which expands to
all files in the current directory.
file * : list information of all files in the current directory.
ls *.c : list all files ends with .c in the current directory.
? Wildcard: inquires chars in a file name.
file ??? : all files names with exactly 3 chars
ls *.?? : all file names with 2 chars following a dot.
[ ] Wildcard : Inquire about chars within a pair of [ ] in filenames.
file *[ab]* : all file names containing the chars a or b
ls *[xyz]* : list all file names containing x or y or z
ls *[a-m]* : list all file names containing chars in the range of a to m
10.14 Command Grouping
In sh scripts, commands can be grouped together by either { } or ( ).
{ ls; mkdir abc; ls; } : execute the list of commands in { } by the current
sh.
The only usefulness of command grouping by { } is to execute them in
the same environment, e.g. to redirect I/O for all the commands in the
group.
(cd newdir; ls; A=value; mkdir $A): execute commands in ( ) by a subsh
process.
The subsh process may change its working directory without affecting the
parent sh.
Any assigned variables in the subsh will have no effect when the subsh
process terminates.
10.15 eval Statement
eval [arg1 arg1 .. argn]
eval is a built-in command of sh. It is executed by sh itself without
forking a new process.
It concatenates the input argument strings into a single string, evaluates
it once,
i.e. perform variable and command substitutions, and present the
resulting string for sh to execute.
Assume
A=’$B’; B=’abc*’; C=newdir; CWD=/root; /root/newdir is a DIR
For the command line
cp $A ‘pwd‘/$C # grave quoted command pwd
sh evaluates the command line in the following steps before executing
it.
(1). Parameter substation: Scan the command line, substitute any $x
with its value but do it only once, i.e. no substitutions again for any
resulting $ symbols. The line becomes
cp $B ‘pwd‘/newdir
(2). Command substitution: perform `pwd` with substitution. Sh will
execute the resulting line
cp $B /root/newdir
This will result in an error if the current directory does not have any file
named $B. However, if we change the original command line to
eval cp $A $(pwd)/$C
sh will evaluate the command line first before executing it. After eval,
the line becomes
cp abc* /root/newdir
(3). Wildcard expansion: When sh executes the line, it expands abc* to
file names that begin with abc.
This will copy all file names beginning with abc to the target directory.

Using eval saves a few substitution statements but it may also makes the
code hard to understand. Therefore, any unnecessary use of eval should
be avoided.
10.16 Debugging sh Scripts
A sh script can be run by a sub sh with the –x option for debugging, as
in
bash –x mysh
The sub sh will show each sh command to be executed, including
variable and command substitutions, before executing the command.
It allows the user to trace command executions.
10.17 Applications of sh scripts
sh scripts are most often used to perform routine work that involves
lengthy sequence of commands.
Example 3: Instead of using Makefiles, simple compile-link tasks can
be done by sh scripts containing the compile and link commands.
The following shows a sh script which generates a MTX operating
system kernel image and user mode command programs on a virtual
disk named VFD
Example 4: Create user accounts for a CS class on a Linux machine.
(1). The Linux command
sudo useradd -m -k DIR -p PASSWORD -s /bin/bash LOGIN
creates a new user account with login name LOGIN and password
PASSWORD.
It creates a user home directory /home/LOGIN, which is populated with
files in the -k DIR directory.
It creates a line in /etc/passwd for the new user account and adds a line
in /etc/shadow for the encrypted PASSWORD.
(2). Assume that roster.txt is a class roster file containing lines of
students ID and name
ID name # name string in lowercase
(3). Run the following sh script mkuser < roster.txt

You might also like