Chinaunix

标题: 请教shell问题:为什么脚本运行时会将当前启动的shell进程打出来 [打印本页]

作者: qinghan1125    时间: 2010-01-29 18:14
标题: 请教shell问题:为什么脚本运行时会将当前启动的shell进程打出来
脚本想实现的功能是杀掉$1参数代表的进程,我现在输入一个不存在的进程abc,ps -ef|grep abc确认无该进程,但是脚本执行时却将当前shell打印出来了
sbmp:/tmp/ou # ./pskill.sh abc
5232 /bin/sh
Are these processes to be killed?[y..n]n
sbmp:/tmp/ou #
sbmp:/tmp/ou # ls hold1*
hold1.5232
sbmp:/tmp/ou #
sbmp:/tmp/ou # cat hold1.5232
5232    /bin/sh
sbmp:/tmp/ou #
sbmp:/tmp/ou #
sbmp:/tmp/ou # ps -ef|grep abc|grep -v "grep"|awk '{print $2"\t"$8}'
//没有结果输出

具体内容如下:
#!/bin/sh
HOLD1=/tmp/ou/hold1.$$
PROCESS=$1

usage()
{
        echo "Usage: `basename $0` processname"
        exit 1
}

if [ $# -ne 1 ]
then
        usage
fi

case $1 in
        *)
        ps -ef|grep $PROCESS|grep -v "grep"|awk '{print $2"\t"$8}' >$HOLD1
        ;;
esac

if [ ! -s $HOLD1 ]
then
        echo "No process found"
        exit 1
fi

while read LOOP1 LOOP2
do
        echo $LOOP1 $LOOP2
done <$HOLD1

echo -n "Are these processes to be killed?[y..n]"
read ANS

case $ANS in
        y|Y)
               while read LOOP1 LOOP2
        do
                echo $LOOP1
                kill -9 $LOOP1
        done <$HOLD1
        rm /tmp/*.$$
        ;;
        n|N)
        ;;
esac
作者: CU_Solaris    时间: 2010-01-29 18:32
echo -n "Are these processes to be killed?[y..n]"


你看一下这句的位置是否正确?

不管如何这句echo都始终显示的
作者: qinghan1125    时间: 2010-01-29 18:38
但是从执行结果看的确将当前shell的进程号写进去了,这个怎么解决?
sbmp:/tmp/ou # ./pskill.sh abc
5232 /bin/sh


sbmp:/tmp/ou # cat hold1.5232
5232    /bin/sh


至于你说的echo位置不对问题,前面有个检查,如果文件为空则退出脚本了
if [ ! -s $HOLD1 ]
then
        echo "No process found"
        exit 1
fi
作者: CU_Solaris    时间: 2010-01-29 18:39
#!/bin/sh
HOLD1=/tmp/ou/hold1.$$
PROCESS=$1

usage()
{
        echo "Usage: `basename $0` processname"
        exit 1
}

if [ $# -ne 1 ]
then
        usage
fi

case $1 in
        *)
        ps -ef|grep $PROCESS|grep -v "grep"|awk '{print $2"\t"$8}' >$HOLD1
        ;;
esac

if [ ! -s $HOLD1 ]
then
        echo "No process found"
        exit 1
else#改

while read LOOP1 LOOP2
do
        echo $LOOP1 $LOOP2
done <$HOLD1

echo -n "Are these processes to be killed?[y..n]"
read ANS

case $ANS in
        y|Y)
               while read LOOP1 LOOP2
        do
                echo $LOOP1
                kill -9 $LOOP1
        done <$HOLD1
        rm /tmp/*.$$
        ;;
        n|N)
        ;;
esac

fi#改




判断是否存在进程不存在显示No process found  else 就显示是否kill

[ 本帖最后由 CU_Solaris 于 2010-1-29 18:40 编辑 ]
作者: qinghan1125    时间: 2010-01-29 18:54
标题: 回复 #4 CU_Solaris 的帖子
改了,结果一样,主要是$HOLD1文件中有一行当前shell的进程号,所以无论如何都判断有进程存在

sbmp:/tmp/ou # ./pskill.sh abc
5430 /bin/sh
Are these processes to be killed?[y..n]n
sbmp:/tmp/ou #
sbmp:/tmp/ou # ls hold*
hold1.5430
sbmp:/tmp/ou # cat hold1.5430
5430    /bin/sh
作者: qinghan1125    时间: 2010-01-30 08:51
继续求助
作者: qinghan1125    时间: 2010-01-30 08:54
输入一个有的进程临时文件中还是会打印当前shell,见如下最后一行
sbmp:/tmp/ou # ./pskill.sh ora_
20695 ora_pmon_ora11g
20697 ora_vktm_ora11g
20701 ora_diag_ora11g
20703 ora_dbrm_ora11g
20705 ora_psp0_ora11g
……………………………………
……………………………………
20727 ora_s000_ora11g
7729 ora_fbda_ora11g
7734 ora_qmnc_ora11g
7748 ora_q000_ora11g
7750 ora_q001_ora11g
7825 ora_smco_ora11g
8376 ora_w000_ora11g
8550 /bin/sh
作者: nhw_cs    时间: 2010-01-30 09:39
这种现象不是很正常么?

试验:

$ cat test.sh
#!/bin/sh
ps -ef|grep $1|grep -v grep

$ ./test.sh dummy   
    hper 16449 16431   0 09:34:32 pts/44      0:00 /bin/sh ./test.sh dummy
$




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2