《Beiginning Linux programming || Linux 程序设计》读书笔记(三)
来源:互联网 发布:广州手趣网络 招聘 编辑:程序博客网 时间:2024/06/04 18:31
(8) Shell Function
function_name () {
statements
}
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, are replaced 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 make functions return strings is for the function to store the string in a variable, which can then be used after the function finishes. Alternatively, you can echo a string and catch the result, like this:
foo () { echo JAY;}
(9) break
foo () { echo JAY;}
...
result=”$(foo)“
You can declare local variables within shell functions by using the local keyword. The variable is then only in scope within the function.
(10) The : Command
The colon command is an alias for true.
The : construct is also useful in the conditional setting of variables. For example,
(11) Continue
: ${var:=value}
Without the :, the shell would try to evaluate $var as a command.
(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. eval is 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 export command 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 shift command moves all the parameter variables down by one, so that $2 becomes $1, $3 becomes $2, and so on. The previous value of $1 is discarded, while $0 remains 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.
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 export command 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 shift command moves all the parameter variables down by one, so that $2 becomes $1, $3 becomes $2, and so on. The previous value of $1 is discarded, while $0 remains 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:
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 trap command before the part of the script you wish to protect.
- 《Beiginning Linux programming || Linux 程序设计》读书笔记(三)
- 《Beiginning Linux programming || Linux 程序设计》读书笔记(四)
- 《Beginning Linux Programming》读书笔记(三)
- Linux程序设计(Beginning Linux Programming 3rd Edition)读书笔记
- Linux程序设计读书笔记------入门
- Linux程序设计-读书笔记1
- Linux程序设计-读书笔记2
- 【读书笔记0103】Beginning linux programming-shell programming
- 读书笔记之:Advanced Linux Programming
- 《Linux程序设计》读书笔记(一)
- LINUX汇编(汇编语言程序设计读书笔记)
- Linux程序设计读书笔记:第二章 shell程序设计
- linux 读书笔记(三)
- 《Beginning Linux Programming》读书笔记(二)
- 《Advanced Linux Programming》读书笔记(1)
- 《Beginning Linux Programming》读书笔记(四)
- 《Beginning Linux Programming》读书笔记(二)
- 《Beginning Linux Programming》读书笔记(一)
- delphi实现一个类继承抽象类并实现接口
- 高洛德着色
- Web应用程序的部署与安装(1)
- 设置与删除cookie时一定要注意路径问题(另读取cookie中firfox与ie的区别)
- 多普达S600短信不直接显示在屏幕上的设置方法
- 《Beiginning Linux programming || Linux 程序设计》读书笔记(三)
- 我的电脑怎么啦
- 从插件/RCP中取得文件路径的方法
- 使用Run-Time Library将wchar_t转换为char
- Image类的扩展,可编辑GIF动画图片,需要Magick支持.
- ibatis的入门例子
- strace命令详解
- Eclipse 3.4 (Ganymede) 开发调试PHP全攻略
- 清除远程桌面信息方法