《Beiginning Linux programming || Linux 程序设计》读书笔记(三)

(8) Shell Function
function_name () {
statements
}
You must always define a function before you can invoke it.When a function is invoked, the positional parameters to the script, $*, $@, $#, $1, $2,and so on, arereplaced by the parameters to the function.When the function finishes, they are restored to their previous values.

You can make functions return numeric values using the return command. The usual way to makefunctions return strings is for the function to store the string in a variable, which can then be used afterthe function finishes. Alternatively, you can echo a string and catch the result, like this:
foo () { echo JAY;}
...
result=”$(foo)“
You can declare local variables within shell functions by using the localkeyword. The variable isthen only in scope within the function.

(9) break

(10) The :Command
The colon command isan alias for true.
The : construct is also useful in the conditional setting of variables. For example,
: ${var:=value}
Without the :, the shell would try to evaluate $varas a command.

(11) Continue

(12) The .Command

(13) eval
The eval command enables you to evaluate arguments. It’s built into the shell and doesn’t normally exist as a separate command. evalis a bit like an extra $: It gives you the value of the value of a variable.

(14) exec
The exec command has two different uses. Its typical use is to replace the current shell with a different program.
The second use of exec is to modify the current file descriptors:
exec 3< afile
This causes file descriptor three to be opened for reading from file afile. It’s rarely used.

(15) export
The exportcommand creates an environment variable from its parameter that can be seen by other scripts and programs invoked from the current program.

(16) expr
The expr command evaluates its arguments as an expression. It’s most commonly used for simple arithmetic in the following form:
x=`expr $x + 1`
The `` (backtick) characters make x take the result of executing the command expr $x + 1. You could also write it using the syntax $( ) rather than backticks, like this:
x=$(expr $x + 1)

(17) printf
(18) return
(19) set
The set command sets the parameter variables for the shell. It can be a useful way of using fields in commands that output space-separated values.
#!/bin/sh

echo the date is $(date)
set $(date)
echo The month is $2

exit 0

This program sets the parameter list to the date command’s output and then uses the positional parameter $2 to get at the month.Since the date command is sensitive to the language locale, in reality you would have extracted the name of the month using date +%B.

The most commonly used form of the command is set -x, which makes a script display a trace of its currently executing command.

(20)Shift
The shiftcommand moves all the parameter variables down by one, so that $2 becomes $1, $3 becomes $2, and so on. The previous value of $1is discarded, while $0remains unchanged. If a numerical parameter is specified in the call to shift, the parameters move that many spaces. The other variables, $*, $@, and $#, are also modified in line with the new arrangement of parameter variables.

(21) Trap
The trap command is used to specify the actions to take on receipt of signals.A common use is to tidy up a script when it is interrupted.
The trap command is passed the action to take, followed by the signal name (or names) to trap on:
trap command signal
Remember that the scripts are normally interpreted from top to bottom, so you must specify the trapcommand before the part of the script you wish to protect.

你可能感兴趣的:(programming)