The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Oracle] "Dynamic" sql / Shell script alan Shell Programming and Scripting 4 03-13-2009 11:07 AM
Q: Recording shell script screen output using "script" command ? lalfonso.gomez Shell Programming and Scripting 4 01-18-2007 09:31 PM
How to get Find command work with a variable passing "*" value? unxuser Shell Programming and Scripting 5 08-16-2006 10:47 AM
Passing argument to "at" command nibl Shell Programming and Scripting 2 07-22-2006 11:36 PM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-21-2007
sachin.tendulka sachin.tendulka is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 32
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
  #2 (permalink)  
Old 11-21-2007
sachin.tendulka sachin.tendulka is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 32
No replies... Pls. do help me in this. It is really urgent.
  #3 (permalink)  
Old 11-21-2007
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
What shell / OS?

If you're using bash, you should be able to get away with something like:
Code:
#! /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:
Code:
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.
  #4 (permalink)  
Old 11-21-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
U can try (in ksh):
Code:
awk -F\: '/^ram?*/ {print $1}' /etc/passwd|xargs -i ksh -c 'ps --o user,fname |grep {}'
Regards

Last edited by Klashxx; 11-21-2007 at 04:27 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0