编写脚本以查找用户过程的所有PID



我真的想使用ps -aux来获取此功能。所以我写了脚本:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read uid
else
        uid=$1
fi
procesy=`ps -aux | awk '{if ($1=="$uid") print$2}'`
echo $procesy

为什么它不起作用?当我writinig ./script root时,我除了空白行以外什么都没有。

这将完成您需要的事情:

pgrep -U $1

在您的脚本中,您使用awk内的shell变量遇到了问题。使用awk的正确方法应该是:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read user
else
        user=$1
fi
procesy=`ps aux | awk -v usr=$user '{if ($1==usr) print$2}'`
echo $procesy

最新更新