在这有两处可以借鉴:
(1)set `/sbin/runlevel` (后面有示例)
(2)
#! /bin/bash
#
# rc This file is responsible for starting/stopping
# services when the runlevel changes.
#
# Original Author:
# Miquel van Smoorenburg, <
miquels@drinkel.nl.mugnet.org>
#
# check a file to be a correct runlevel script
check_runlevel () #检测在脚本启动目录下的相关脚本,并且返回相应的值0|1
{
# Check if the file exists at all.
[ -x "$1" ] || return 1
# Reject backup files and files generated by rpm.
case "$1" in
*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
return 1
;;
esac
return 0
}
# Now find out what the current and what the previous runlevel are.
argv1="$1" #读取位置参数1
set `/sbin/runlevel` #创建位置参数
runlevel=$2
previous=$1
export runlevel previous #创建全局环境变量
. /etc/init.d/functions #载入函数
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
if [ -f /var/run/confirm ]; then
echo $"Entering interactive startup"
else
echo $"Entering non-interactive startup"
fi
fi
# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"#如果第一个参数不为空,那么就将其argv1这个变量赋值给变量
#runlevel
# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0 #检测运行级别是否是否目录
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??} #这句话很有用,它是截取的作用,将符合#后面的文字都
#将其除去
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue #判定$sybsys与$subsys.init两个文件是否存在,若存在,那么什
#么都不做,若不存在,那么不执行下面的语句
# Bring the subsystem down.
if egrep -q "(killproc |action )" $i ; then
$i stop
else
action $"Stopping $subsys: " $i stop
fi
done
# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/S??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
&& continue
# If we're in confirmation mode, get user confirmation
if [ -f /var/run/confirm ]; then
confirm $subsys
test $? = 1 && continue
fi
update_boot_stage "$subsys"
# Bring the subsystem up.
if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
export LC_ALL=C
exec $i start
fi
if egrep -q "(daemon |action |success |failure )" $i 2>/dev/null \
|| [ "$subsys" = "single" -o "$subsys" = "local" ]; then
$i start
else
action $"Starting $subsys: " $i start
fi
done
rm -f /var/run/confirm
if [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then
/usr/bin/rhgb-client --quit
fi
===================
参考代码:
[No.501 19:04:23 ~]$ set var1 var2 var3
[No.502 19:04:37 ~]$ while (( $#>0 ));do
> echo $1
> shift
> done
var1
var2
var3
[No.503 19:05:07 ~]$ echo $#
0
[No.504 19:05:26 ~]$ set var1 var2 var3
[No.505 19:05:41 ~]$ echo $#
3
[No.506 19:05:44 ~]$ echo "$@"
var1 var2 var3
[No.507 19:05:54 ~]$ set --
[No.508 19:06:31 ~]$ echo $#
0
[No.509 19:06:35 ~]$ echo "$@"
[No.510 19:06:39 ~]$