Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass contexts of a file to a command as a parameter 1

Status
Not open for further replies.

terrassa5

IS-IT--Management
Feb 22, 2005
40
0
0
GB
I have a text file that only has login of users, one login per row.

Example of file:
pei
xav
snc
bel

I want to send a message using my program bshcmd6.1 to all of these users when they are connected to the system, and to do this I need to get process number. I'm thinking on something like that:

for i in `ps -ef | grep 'login_of_user' | awk '{print $2}'`
do
bshcmd6.1 -M "Hello" -u1 -w1 $i
done


How can I get logins of my file to put them on the grep of ps command?

Thank you very much
 
A starting point:
ps -fu $(</path/to/users.txt)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV

Thanks for your reply, but with this solution ps only get processes of first user in the file, it seems it only reads first line of file, and I need processes of all users in the file.
 
Another way:
while read user
do
ps -fu $user
done < /path/to/users.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Another starting point:
Code:
users | awk -v txt="Hello there.." '{
  if (NR == FNR) User[$1]
  else for (i=1; i<=NF && ! Seen[$i]++; i++)
      if ($i in User) print txt | "write " $i
}' path/to/users -

Some more extended but incomplete version:
Code:
awk -v txt="Hello there..." 'file != FILENAME { isfile++; file = FILENAME }
{ if (isfile == 1) User[$1]
  else if (isfile == 2) Written[$1]
  else for (i=1; i<=NF; i++) {
    Seen[$i]
    if ($i in User && ! ($i in Written) {
      cmd = "write " $i; print txt | cmd; close(cmd)
    }
  }
}

END { for (i in Seen) if (i in Written) print i >2
  # other stuff
}

users | awk '..' users 2 -

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
else for (i=1; i<=NF && ! Seen[$i]++; i++)
if ($i in User) print txt | "write " $i


Should be:

else for (i=1; i<=NF; i++)
if ($i in User && ! Seen[$i]++) print txt | "write " $i

or..

if ($i in User && ! ($i in Seen)) { Seen[$i]; print txt | "write " $i }

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
I did not try it.
it should work fast.

ps -ef |awk '{print $1,$2}'|while read ii
do
set $ii
grep -q $1 login_of_user && bshcmd6.1 -M "Hello" -u1 -w1 $2
done

# grep -q --> Quit grep with no output
# returns 0 only on succes

Gregor.Weertman@lycos.com
 
Mine did get created especially for speed. Neither mass grep, nor ps, nor anything spawning multiple times is required.

gregor, read i ii -> grep -qw $i logins && cmd

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Hello xmb,

I see your point. Your line is faster.

Don’t you need ps to know which users are on-line en which pid they have?
Can the bshcmd6.1 program handle multiple pid arguments?

Regards Gregor


Gregor.Weertman@gmail.com
 
For XMB and Gregor.

XMB script seems impressive, but I don't know about scripting and I don't understand what it does exactly.

I need to use ps, because each people connected to my system has three processes, and I don't want bshcmd6.1 to send three messages to each of them. So all users have a process called bshell6.1, and with ps I can do a grep to find pid only for bshell6.1 processes, and give this pid to bshcmd6.1.

Because of that now I was using PHV solutions and now I will try Gregor solution.

Speed is not very important in this case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top