我真的想使用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