A shell/awk problem 
Author Message
 A shell/awk problem

 I am trying to solve following problem:

There are several users, each of them can have different process pairs (p1
and p2) running. The process p1 has to be killed if the p2 is not existent
anymore.

I am thinking of an awk or shell script of following content:

- who |grep <user-group>  >users.txt
 shell)
- read field 1 of users.txt (the user will be in this field)      (shell or
awk?)
- begin of a loop (depends on number of uesers)               (shell or
awk?)
- ps -u <actual-user> >proc-table.txt
(shell)
- read proc-table.txt and look at p2, put p1 in a variable (p1 and p2 always
use the same TTY) (awk)
- kill other p1 processes (awk calling kill)

Problem: How can I exchange variable between awk and shell?

Is it possible to write the script entirely in awk?

PS: There is no perl on that machine available, just awk and csh.

thanks in advance for any ideas




Thu, 03 Feb 2000 03:00:00 GMT  
 A shell/awk problem

Quote:

>  I am trying to solve following problem:

> There are several users, each of them can have different process pairs (p1
> and p2) running. The process p1 has to be killed if the p2 is not existent
> anymore.

> I am thinking of an awk or shell script of following content:

> - who |grep <user-group>  >users.txt
>  shell)
> - read field 1 of users.txt (the user will be in this field)      (shell or
> awk?)
> - begin of a loop (depends on number of uesers)               (shell or
> awk?)
> - ps -u <actual-user> >proc-table.txt
> (shell)
> - read proc-table.txt and look at p2, put p1 in a variable (p1 and p2 always
> use the same TTY) (awk)
> - kill other p1 processes (awk calling kill)

> Problem: How can I exchange variable between awk and shell?

> Is it possible to write the script entirely in awk?

> PS: There is no perl on that machine available, just awk and csh.

> thanks in advance for any ideas



I don't have an awk book in front of me, so bear with me on the syntax:

BEGIN   {
                user_string = "who | grep <user-group>"

                while (user_string | getline user)
                {
                        job_string = "ps -u " user

                        while (job_string | getline)
                        {
                                # I'm a little fuzzy on what you're looking for here.
                                # You'll be able to loop through each process looking
                                # for what you want. $0 thru $N will set to the process
                                # information.

                                # To kill a process

                                system("kill -9 " pid)
                        }

                        close(job_string)
                }

                close(user_string)
        }

More details will get you more help. I hope this puts you on the right
track.

To answer your other question: "Problem: How can I exchange variable
between awk and shell?"

awk -v variable=value -f awk_script

Variable is equal to any variable within the awk script.
--

Quote:
>>>---------------------------------<<<

   Joel Friedman (N-Tier Consultant)

- Show quoted text -

Quote:
>>>---------------------------------<<<



Fri, 04 Feb 2000 03:00:00 GMT  
 A shell/awk problem


{*filter*}Space:

Quote:
>There are several users, each of them can have different process pairs (p1
>and p2) running. The process p1 has to be killed if the p2 is not existent
>anymore.
>- who |grep <user-group>  >users.txt [shell)
>- read field 1 of users.txt [the user will be in this field)      (shell or
>awk?)
>- begin of a loop (depends on number of uesers)               (shell or
>awk?)
>- ps -u <actual-user> >proc-table.txt
>(shell)
>- read proc-table.txt and look at p2, put p1 in a variable (p1 and p2 always
>use the same TTY) (awk)
>- kill other p1 processes (awk calling kill)

What about:

#! /bin/ksh
who | egrep USER_GROUP > users.txt
ps -ef | awk '
BEGIN {
        #
        # Load list of targetted users
        #
        while ( (getline < "users.txt") == 1 ) \
        {
                User[$1]=$1
        }

Quote:
}

/p1/ {
        POwner = substr($0,1,8);
        if ( POwner in User ) \
        {
                #
                # If p1 process belongs to targetted user, we store the PID
                # in an array indexed by the user name
                #
                P1[POwner]=substr($0,9,6);
        }
Quote:
}

/p2/ {
        POwner = substr($0,1,8);
        if ( POwner in User ) \
        {
                #
                # If p2 process belongs to targetted user, we store the PID
                # in an array indexed by the user name
                #
                P2[POwner]=substr($0,9,6);
        }
Quote:
}

END {
        for ( i in P1 ) \
        {
                if ( i not in P2 ) \
                {
                        #
                        # for each user in array of p1 process, we print the PID if there's
                        # no associated user in array of p2
                        #
                        # note that the print is pipe'd into xargs & kill to optimize
                        #
                        print P1[i];
                }
        }
Quote:
}

' | xargs -n 200 kill -1

Quote:
>>>>>>>>>>>> Note that I did not test it >>>>>>>>>>>>>>>>>>>

Hope this helps,
Patrice.

----------------------------------------------------------------------

|  #include <disclaimer.h>  | V-mail : (+33) [0]4 92 29 39 49 + 3902 |
----------------------------------------------------------------------



Sat, 05 Feb 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with awk and shell script

2. AWK-Script to set a shell variable (Win32) ?

3. Return value from awk snipet via korn shell subroutine

4. Passing command line variables to AWK in shell script

5. using awk in a shell script...

6. Substituting single quotes from Perl via shell using awk and gsub

7. shell/awk combo puzzler

8. setting shell variables with results from awk

9. trying to call awk from within a shell script

10. A simple question regarding shell scripts and awk

11. shell-parameter for outputfile in awk

12. awk-shell interaction

 

 
Powered by phpBB® Forum Software