看看代码:
#!/bin/bash
read -p "Eneter 1 for UID and 2 for LOGNAME" choice
if [ $choice -eq 1 ]
then
read -p "Enter UID: " uid
logname=`cat /etc/passwd | grep $uid | cut -f1 -d:`
else
read -p "Enter Logname: " logname
fi
not=`ps -au$logname | grep -c bash`
echo "The number of terminals opened by $logname are $not"
Run Code Online (Sandbox Code Playgroud)
此代码用于查找用户在同一台 PC 上打开的终端数量。现在有两个用户登录,比如 x 和 y。我目前以 y 身份登录,用户 x 中打开了 3 个终端。如果我使用上面提到的不同方式在 y 中执行此代码,结果是:
$ ./file.sh
The number of terminals opened …Run Code Online (Sandbox Code Playgroud)