Linux shell实例精讲
1.shopt bash2.0以上新的命令,功能和set类似。
给变量赋值时等号两边不可留空格。
环境变量一般使用大写。
2.
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的进程ID号
$@ 传递到脚本的参数列表,并在引号中返回每个参数
$- 显示shell使用的当前选项,与set命令功能相同
$? 显示最后命令的退出状态,0表示没有错误,其他表示有错误
3.
echo 命令中使用" "无法直接识别转义字符,必须使用选项 -e,但可以使用$和`引用变量和命令替换。
$[] 告诉shell对方括号中表达式求值 $,如$
4.
tee
把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中
tee -a file -----a表示在文件末尾追加,不要-a则覆盖
该命令一般与管道合用
5.
command>filename>&1 ---把标准输出和标准错误重定向
command /dev/tty
done >temp$$
mv temp$$ $1
$ sh loopred testmv
Processing file testmv.....
Process finish,total line number is 19
$ vi testmv
1 /u01/test
2 /u01/test/1
3 /u01/test/1/11
4 /u01/test/1/forlist.sh
5 /u01/test/1/optgets.sh
6 /u01/test/1/whiletest.sh
7 /u01/test/1/func.sh
8 /u01/test/1/helloworld.sh
9 /u01/test/1/df.out
10 /u01/test/1/nullfile.txt
11 /u01/test/1/iftest.sh
12 /u01/test/1/myfile
13 /u01/test/1/opt2.sh
14 /u01/test/1/0
15 /u01/test/1/case.sh
16 /u01/test/1/nohup.out
17 /u01/test/1/hellfun.sh
18 /u01/test/1/parm.sh
19 /u01/test/1/test
15。在done后面加&使循环在后台运行,程序继续执行。
16.在函数内可以使用local定义本地变量,local variable。
17.陷阱信号 trap --当一个信号发出传递给进程时,进程进行相关操作,信号包括中断等
trap ‘command;command’ signal-num #trap设置时执行命令
trap “command;command” signal-num #信号到达时执行命令
eg:
# trap "echo -e 'hello world\n';ls -lh" 2
# hello world ---ctrl+c
total 100K
-rw-r--r-- 1 root root 1.4K Nov 14 16:53 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.0K Nov 23 13:11 Desktop
-rw-r--r-- 1 root root 53K Nov 14 16:53 install.log
-rw-r--r-- 1 root root 4.9K Nov 14 16:53 install.log.syslog
drwxr-xr-x 2 root root 4.0K Nov 22 13:03 vmware
页:
[1]