Variable to command to Variable Question KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable to command to Variable Question KSH
# 1  
Old 04-15-2012
Variable to command to Variable Question KSH

Hello,

First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID, there are no errors.

Code:
ps -ef | awk '{if ($2 == "3026") print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'

If I pass it as a variable that I know contains data, it errors out. Best as I can tell, the data isn't being read.

Code:
ps -ef | awk '{if ($2 == $mFName) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'

The only thing that I can think that is happening is that the command is running outside of the main script and I need to pass the value to it in some way. I just haven't been able to figure out how to do it.

I then want to pass the results to a variable that I can use.

Can anyone point the way or provide an alternate explanation? Any assistance would be greatly appreciated.
# 2  
Old 04-15-2012
To pass a variable, try this..:
Code:
ps -ef | awk '{if ($2 == mfname) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}' mfname="$mFName"

The double quotes are important..

(BTW welcome to these forums and I salute you for correctly using code tags right in your first post Smilie )
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-15-2012
you need to define variable in awk so that it can understand:

Code:
ps -ef | awk  -v name="$nFName" '{if ($2 == name) print $1 " " $2 " " $3 " " $9 " " $10 " " $11 " " $12 " " $13;}'


Last edited by 47shailesh; 04-15-2012 at 12:27 PM.. Reason: adding double quotes thanks Scrutinizer for pointing
This User Gave Thanks to 47shailesh For This Post:
# 4  
Old 04-15-2012
Thank you for the help. I tested and it worked very well. This is definately going on my offices wiki for one to remember.

---------- Post updated at 09:24 AM ---------- Previous update was at 09:21 AM ----------

I may have botched the reply, however, I wanted you both to know that your assistance was very timely and much needed. Both solutions worked as advertised. Again, much thanks. Now I can start hunting why some of my Oracle processes are crashing. I needed that line so that I could give a face to the PID I was using LSoF with.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

[Solved] cp command with dollar variable in ksh

hi, I have been trying to acheive the following task for a while now, but failed.. Need help, experts please help! This is what I am trying to do: - I am writing to a flat file the name of the source to be copied and the destination path as to where it is to be copied to. Sample flat file:... (7 Replies)
Discussion started by: abdulhusein
7 Replies

3. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

4. UNIX for Dummies Questions & Answers

Question on using a variable in KSH

Hi all, The below command tries to copy ".tgz" instead of "hello_test.tgz" -- It seems as if the underscore gets in the way. I tried with different ways of using quotes, with no luck, unfortunately...it's probably very simple, but may I ask how this would be done: How would the below be... (3 Replies)
Discussion started by: chatguy
3 Replies

5. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

6. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. Shell Programming and Scripting

variable as variable name (ksh)

Greetings folks I'm using ksh on aix and have gone around and around trying to use a variable as a variable name. I would like to set a variable like this... ym200702="01 02 05 06 07 08 09 12 13 14 15 16 19 20 21 22 23 26 27 28" and then check to see if the variable has value... if ];... (4 Replies)
Discussion started by: Smoker
4 Replies

8. Shell Programming and Scripting

Question On Command in Variable

I have been working on a script that executes on a number of different operating systems. As a result I was trying to set a variable or perhaps variable array depending on the OS. I tried the following using eval and such but so far have not had any luck. Is there a way to do something like the... (4 Replies)
Discussion started by: scotbuff
4 Replies

9. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question