首页 > 代码库 > Linux shell实战(ipcs工具)

Linux shell实战(ipcs工具)

#!/bin/bash

WHOAMI=`whoami`


if [ $# -lt "1" ] 
then
    echo [程序名] [操作对象]
    exit 0
fi

#需要两个参数,参数一:起始行,参数二:结束行,参数三:操作对象
function seddata
{
    for numx in `ipcs | sed -n ${1},${2}p | sed -n /${WHOAMI}/p | awk {print $2}`
    do
        ipcrm ${3} ${numx}
    done
}

function startp
{
    case $1 in
    "shm")
            START=`ipcs | sed -n /shmid/=`
            START=`expr $START + 1`
            END=`ipcs | sed -n /Semaphore/=`
            END=`expr $END - 2`
            #实际上,直接匹配空行会更简单,这里是为了学习目的
            #END="/^$/"
            echo "start=$START;end=$END"
            seddata $START $END shm
        ;;
    "sem")
        START=`ipcs |sed -n /semid/=`
        START=`expr $START + 1`
        END=`ipcs | sed -n /Message/=`
        END=`expr $END - 2`
        seddata $START $END sem 
    ;;
    "msq")
        START=`ipcs | sed -n /msqid/=`
        START=`expr $START + 1`
        END="/^$/"
        seddata $START $END msq
    ;;
    *)
            echo "错误的参数!"
        ;;
    esac
}

if [ "$1" = "all" ]
then
    startp shm
    startp sem
    start msq
else
    startp $1
fi



echo "脚本执行成功!"

 

Linux shell实战(ipcs工具)