Storing all the PID's in a variable.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Storing all the PID's in a variable.
# 1  
Old 12-24-2011
Storing all the PID's in a variable.

Hi,

Code:
ps -ef|awk '{print $2}'

i want to store the result of the above command in a variable.
I never worked with arrays in shell scripting.
i tried the below code:
Code:
set a=`ps -ef|awk '{print $2}'`

But
Code:
echo $a

returns null.
I want to store the content in a variable and retrieve it line by line.
Please help me.
Thanks
# 2  
Old 12-24-2011
Code:
#!/bin/ksh
set -A arr `ps -ef|awk '{printf("%d ", $2)}'`
i=0
while [ $i -lt ${#arr[*]}
do
    echo ${arr[i]}
    i=$(( $i +  1))
done

See Korn Shell Arrays - Assigning and Accessing Values - Part I
for other ways to do this

Last edited by jim mcnamara; 12-24-2011 at 01:49 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-24-2011
Thanks! I will go through that link and post if i have any doubts.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Storing a variable using sed

I would like to use sed to store a variable. The code is : echo $HEADERREC CUTVAR=$(echo "$HEADERREC"|sed 's/SDV/STR') echo $CUTVAR Output I am getting now: 0012PVGRSCDVSDV 005 00000000000000000000 2014 0.00 sed: Function s/SDV/STR cannot be parsed. Desired... (5 Replies)
Discussion started by: MIA651
5 Replies

3. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

4. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

5. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

6. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

7. Shell Programming and Scripting

Help Storing PID

I have to write a csh script to achieve the following: Run a command and store its PID as a two char variable then issue the ps command. If the ps command is successful output a string if it fails output a separate string and include an if statement and use the test command. Help would be greatly... (1 Reply)
Discussion started by: Porthos
1 Replies

8. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question