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

sh/bash/dash/ksh/zsh等Shell脚本
回复
chai21cn
帖子: 8
注册时间: 2008-01-07 0:55

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

#1

帖子 chai21cn » 2008-01-20 17:27

第11章,第1个例子
代码如下

代码: 全选

#!/bin/bash
# spawn.sh


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


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

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

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

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

#  Is having a script spawn multiple instances of itself
#+ an advisable scripting technique.
#  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
指点指点 :roll:
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#2

帖子 BigSnake.NET » 2008-01-20 17:32

你打漏了点东西
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
chai21cn
帖子: 8
注册时间: 2008-01-07 0:55

#3

帖子 chai21cn » 2008-01-20 17:35

最开始我是自己打的,出现这个问题后
我就直接把原版的脚本文件copy出来执行的也是这样的
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2008-01-20 17:44

P_array=( $PIDS )
这是什么意思。
改改吧。
● 鸣学
chai21cn
帖子: 8
注册时间: 2008-01-07 0:55

#5

帖子 chai21cn » 2008-01-20 17:51

eexpress 写了:P_array=( $PIDS )
这是什么意思。
改改吧。
这是给数组赋值
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#6

帖子 BigSnake.NET » 2008-01-20 18:00

chai21cn 写了:最开始我是自己打的,出现这个问题后
我就直接把原版的脚本文件copy出来执行的也是这样的
我 copy 你贴出来的东西
正常...
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
chai21cn
帖子: 8
注册时间: 2008-01-07 0:55

#7

帖子 chai21cn » 2008-01-20 18:08

怪事,我也copy了贴出来的还是这个问题
:em23
chai21cn
帖子: 8
注册时间: 2008-01-07 0:55

#8

帖子 chai21cn » 2008-01-20 18:13

有可能是我的本地环境有问题
回复