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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing a list of dynamic names to a "PS" command in shell script?
# 1  
Old 11-21-2007
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  
Old 11-21-2007
No replies... Pls. do help me in this. It is really urgent.
# 3  
Old 11-21-2007
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  
Old 11-21-2007
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

The "read" command misinterprets file names containing spaces

The "read" command, which is built into bash, takes words from the standard input. However, "read" is not good at taking file names if the file names contain spaces. I would like my bash script to ask the user to enter file names, which may contain spaces. Can you think about any technique for... (14 Replies)
Discussion started by: LessNux
14 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

"sed" command is not working in shell script

Hi All, I am not much strong in shell scripting... I am using sed command in my script to find and replace a string....... This is how script looks : ############# #!/usr/bin/ksh CONFIG_FILE=iom_test.txt FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"` REPLACE=`echo... (2 Replies)
Discussion started by: askumarece
2 Replies

10. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies
Login or Register to Ask a Question