【问题标题】:Retrieving active user information script with ksh?使用 ksh 检索活动用户信息脚本?
【发布时间】:2015-01-27 13:53:53
【问题描述】:

我正在使用一个在 AIX 上运行的应用程序,它不提供有关当前正在使用该系统的用户的任何信息。相反,它只有一个实用程序 userInfo,它列出了数据库中的所有用户 ID 和用户名:

>userInfo <path to app> -list
   696969 Doe, John 
   123456 Doe, Jane
   121212 Jones, Mike
   232323 Sanders, Chris
   345678 Smith, Mary

等等……

我检索当前在系统中的用户的方法是首先搜索活动用户会话这将返回一个带有用户 ID(数字)的字符串,格式如下。

>ps –ef | grep <app name> | grep user 
     <process name and path info…> -user 696969 <more process and path info> 
     <process name and path info…> -user 121212 <more process and path info> 

然后使用 grep 运行 userInfo 实用程序

>userInfo <path to app> -list | grep 696969
    696969 Doe, John 

问题在于,这都是手动的,并且必须为 ps –ef 找到的每个单独的用户 ID 重复。当系统中有很多用户时,这需要很长时间,而且我通常被要求在很短的时间内发送一个活跃用户列表。 我想创建一个脚本来自动化这个过程。最好在 KSH 中,但 bash 也可以。我可以运行该脚本,它会为我提供当前用户 ID 和名称的列表:

>script.ksh 
    696969 Doe, John 
    232323 Sanders, Chris
    345678 Smith, Mary

我将不胜感激。 谢谢

【问题讨论】:

    标签: shell ksh aix


    【解决方案1】:

    将第一个输出传递给sed 's/.*-user \([^ \t]*\).*/\1/',然后使用 for 循环对每个用户 ID 进行 grep。祝你好运!

    sed 命令将过滤用户 ID。

    更具体:

    for userid in $(ps –ef | grep <app name> | grep user | sed 's/.*-user \([^ \t]*\).*/\1/')
    do
        userInfo <path to app> -list |grep $userid
    done
    

    更好的版本:

    for userid in $(ps –ef | grep -E "$1.*user|user.*$1" | sed 's/.*-user \([^ \t]*\).*/\1/')
    do
        userInfo $(which $1) -list |grep $userid
    done
    

    其中 $1 是作为参数传递给脚本的应用程序的名称,请注意这是未经测试的(或者更确切地说是在 bash 中测试的)。

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2019-09-19
      相关资源
      最近更新 更多