Tuesday, August 14, 2007

For Every User, Do ...

If you want to run certain command for every user (including system users), you can do this:
for i in `cut -d: -f1 /etc/passwd`
    /run/this/command $i

If you want to do it in AWK way to loop through every user, you need to construct the command before giving it to 'system' within AWK. It is definitely a lot more involve than using the 'for' loop within the shell. Anyway, this is the AWK way:

awk -F":" '
    cmd=sprintf("/run/this/command %s",$1)
}' /etc/passwd
Remember if you are running the above on Solaris, change awk to nawk because the awk does not support 'system' function.

How about if you want to run the command for non-system users, then AWK has a niche over shell. In most of UNIX systems, system users are normally having user id small than 100. You can modify the above AWK command to achieve that

awk -F":" '
$3 >= 100 {
    cmd=sprintf("/run/this/command %s",$1)
}' /etc/passwd/etc/passwd

IMHO, awk is definitely worth your time to learn.

Labels: ,


Blogger Unknown said...

Nice :)

9:50 AM  

Post a Comment

<< Home