ch10shell编程
ch10shell编程
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.
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
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