分类: Python/Ruby
2012-03-22 21:55:07
----------------------------------------------------------------------------
0. 关于重定向
# file redir.sh
function fn_main() {
bash redir2.sh
}
fn_main $@ >& stdout_n_stderr
redir2.sh继承了redir.sh 的stdout与stderr,即使redir2.sh在后台运行也是这样。
这时会出现一个问题:fn_main $@ >& stdout_n_stderr | tee -a f_out_n_err
当有redir2.sh &在后台运行时,如果其不退出,redir.sh也不会退出,因为管道'|'
只有当文件被关闭时才会退出。
----------------------------------------------------------------------------
1. 后台执行
Shell fn_bg &
(0) moo:~/sh/feature.test # bash --version
GNU bash, version 3.1.17(1)-release (i586-suse-linux)
Copyright (C) 2005 Free Software Foundation, Inc.
(1) strace -f -t -T -s 1024
(2) clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7d466f8) = 15610 <0.000776>
(3) Conclusion
bg shell 是一个新的进程,改变全局变量,不会在父进程中修改
----------------------------------------------------------------------------
2. "$@" "$*"
1 #! /bin/bash
2
3 fn_chk_position_para() {
4 echo para[1]:$1
5 }
6
7 # $@ is different only when are quoted
8 function fn_main() {
9 arr=(1 2 3 4 5)
10 fn_chk_position_para "$@"
11 fn_chk_position_para "$*"
12
13 fn_chk_position_para "${arr[@]}"
14 fn_chk_position_para "${arr[*]}"
15 }
16
17 fn_main 1 2 3
18 # output
19 #|| para[1]:1
20 #|| para[1]:1 2 3
21 #|| para[1]:1
22 #|| para[1]:1 2 3 4 5
----------------------------------------------------------------------------
3. pkill tree
function fn_pkill_children()
{
local papa=$1
# must declare child as a local
local child
local children
children=`ps -e -opid,ppid | grep "$papa$" | awk '{print $1}'`
for child in $children; do
fn_pkill_children $child
kill -9 $child
done
return 0;
}
function fn_pkill_tree()
{
fn_pkill_children $1
kill -9 $1
}
fn_pkill_tree $@