LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 956|回复: 1

<高级Bash脚本编程指南>遇到的一例子的问题

[复制链接]
发表于 2008-1-20 17:38:41 | 显示全部楼层 |阅读模式
第11章,第1个例子
代码如下
  1. #!/bin/bash
  2. # spawn.sh


  3. PIDS=$(pidof sh $0)  # Process IDs of the various instances of this script.
  4. P_array=( $PIDS )    # Put them in an array (why?).
  5. echo $PIDS           # Show process IDs of parent and child processes.
  6. let "instances = ${#P_array[*]} - 1"  # Count elements, less 1.
  7.                                       # Why subtract 1?
  8. echo "$instances instance(s) of this script running."
  9. echo "[Hit Ctl-C to exit.]"; echo


  10. sleep 1              # Wait.
  11. sh $0                # Play it again, Sam.

  12. exit 0               # Not necessary; script will never get to here.
  13.                      # Why not?

  14. #  After exiting with a Ctl-C,
  15. #+ do all the spawned instances of the script die?
  16. #  If so, why?

  17. # Note:
  18. # ----
  19. # Be careful not to run this script too long.
  20. # It will eventually eat up too many system resources.

  21. #  Is having a script spawn multiple instances of itself
  22. #+ an advisable scripting technique.
  23. #  Why or why not?
复制代码


问题:
按这个例子的意思是不按Ctl-C这个脚本是不会停止的

我运行出错:
6713 6701 6476 5561
3 instance(s) of this script running.
[Hit Ctl-C to exit.]

./spawn.sh: 6: Syntax error: "(" unexpected
发表于 2008-1-20 18:48:17 | 显示全部楼层
在我的机器上正常.
1690 1582
1 instance(s) of this script running.
[Hit Ctl-C to exit.]

3676 1690 1582
2 instance(s) of this script running.
[Hit Ctl-C to exit.]

3679 3676 1690 1582
3 instance(s) of this script running.
[Hit Ctl-C to exit.]

3682 3679 3676 1690 1582
4 instance(s) of this script running.
[Hit Ctl-C to exit.]

3685 3682 3679 3676 1690 1582
5 instance(s) of this script running.
[Hit Ctl-C to exit.]

3688 3685 3682 3679 3676 1690 1582
6 instance(s) of this script running.
[Hit Ctl-C to exit.]

3691 3688 3685 3682 3679 3676 1690 1582
7 instance(s) of this script running.
[Hit Ctl-C to exit.]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表