- 论坛徽章:
- 0
|
本帖最后由 rdcwayx 于 2014-03-28 16:22 编辑
脚本的一个功能是:当前用户下运行了n个test进程,通过-k[pid]杀掉指定进程ID为pid的test进程,如果pid为0,则杀掉所有test进程。- #!/bin/sh
- #kill_test.sh
- stop()
- {
- pid=$1
- name="$PWD/$2"
- user=`whoami`
- echo "pid is $pid"
- echo "name is $name"
- echo "user is $user"
- if [ $pid -eq 0 ]
- then
- echo "All the process named by $name will be killed!"
- ps -ef |grep $name|grep -v grep|grep $user|awk '{print $2}'|xargs kill
- fi
- if [ $pid -gt 0 ]
- then
- kill_name=`ps -ef | grep $pid | grep $user | grep -v grep | awk '{print $8}'`
- echo "kill_name is $kill_name"
- if [ "$kill_name" = "$name" ]
- then
- # kill $pid
- echo "kill $pid"
- else
- echo "wrong pid"
- fi
- fi
- }
- PROCESS_NAME=“test”
- while getopts k:b: OPTION
- do
- case $OPTION in
- k)
- KILL_PID=$OPTARG
- stop $KILL_PID $PROCESS_NAME
- ;;
- b)
- ...
- ;;
- ?)
- ...
- esac
- done
复制代码 在命令行中运行该脚本:$./kill_test.sh -k26187- pid is 26187
- name is /home/zj/bin/test
- user is zj
- kill_name is /bin/sh
- /bin/sh
- name is /home/zj/bin/test
- wrong pid
复制代码 结果把脚本中第一行的/bin/sh给打印出来了,导致杀test进程失败。
如果把第一行改成#!/bin/bash,再执行脚本就会打印/bin/bash.
如果去掉第一行的#!/bin/sh,脚本执行正常。
而不采用getopts,也保留第一行的#!/bin/sh,直接用$1 $2获取选项参数,脚本执行也正常。
哪位大侠帮忙解释一下?Linux zj 2.6.18-128.el5,CentOS release 5.3 (Final) |
|