passing a list of dynamic names to a "PS" command in shell script?

Hi,

I am new to shell script. This is my first post .I have written a small script which returns list of names starts with "ram" in /etc/passwd .Here is that:-

#!/bin/ksh

NAME_LIST="name_list.txt"
cat /dev/null > $NAME_LIST

evalcmd="cat /etc/passwd | grep "^ram?*" | cut -d: -f1"
eval $evalcmd > $NAME_LIST 2>&1

echo $?

if [[ $? -ne 0 ]] then
echo "Failed to create list of names";
else
echo "List of names are created successfully";
fi

The thing is that i need to pass these dynamic names from /etc/passwd to a "ps" command like:-

ps -o user,fname -U ram,ramdev1,ramdev2,ramdev3

Since i cannot hardcode the names like ram,ramdev1,ramdev2,etc i need to pass these names in a single
command. something like:-

cat /etc/passwd | grep "^ram?*" | cut -d: -f1| ps --o user,fname -U <dynamic variable which fetches the whole name in /etc/passwd>

Since i am pretty much new to shellscript.Please do help me on this. This is really urgent to be delivered. Hence pls. do the needful.

Thanx,
Sachin

No replies... Pls. do help me in this. It is really urgent.

What shell / OS?

If you're using bash, you should be able to get away with something like:

#! /bin/bash

typeset -i n=0
typeset -a names

oldifs="$IFS"
IFS=:
 while read passwdname _; do
  [[ $passwdname == *a* ]] && names[n++]=$passwdname
 done </etc/passwd
IFS="$oldifs"

names=${names[*]}
ps --o user,fname -U ${names// /,}

Or:

ps --o user,fname | gawk '/^[^ ]*ram/{print $1}'

I'm sure there's a dozen ways of doing this better, but those are the first two things I thought of.

U can try (in ksh):

awk -F\: '/^ram?*/ {print $1}' /etc/passwd|xargs -i ksh -c 'ps --o user,fname |grep {}'

Regards