Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How to avoid arguments inside Nawk command?

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-05-2017
Tools How to avoid arguments inside Nawk command?

Hi,

Here is my command

Quote:
mypid=`/usr/ucb/ps auxwww | grep $i | grep $2 | grep -v grep | nawk 'NR==1{print $2}'`
Code:
print $2

was meant to select the second column however, it is getting substituted with the second argument that was passed to the script.

Can you please tell me how can I resolve this ?
# 2  
Old 10-05-2017
How do you know that the second argument of the script is not the same as the second column returned by ps?

What is the value of $2 (the argument to the script) and what is the output of the ps command?
# 3  
Old 10-05-2017
RedHat

Quote:
Originally Posted by Scott
How do you know that the second argument of the script is not the same as the second column returned by ps?

What is the value of $2 (the argument to the script) and what is the output of the ps command?
My script terminates before I expect it to ... The for loop break in between for some reason.

Here is my script run.sh

Code:
#!/bin/bash
 echo "Stopping Servers"
 for i in $(echo $1 | sed "s/,/ /g")
do
echo "/usr/ucb/ps auxwww | grep $i | grep $2 | grep -v grep | nawk 'NR==1{print $2}'"
mypid=`/usr/ucb/ps auxwww | grep $i | grep $2 | grep -v grep | nawk 'NR==1{print $2}'`
 echo "MYPID is:$mypid"
kill -9 $mypid
done
 echo "Clearing Cache..."

Here is the INCORRECT output:
Code:
bash-3.2$ bash -x /web/run.sh MS01,MS02,MS03,MS04 mydomain01
+ echo 'Stopping Servers'
Stopping Servers
++ echo MS01,MS02,MS03,MS04
++ sed 's/,/ /g'
+ for i in '$(echo $1 | sed "s/,/ /g")'
+ echo '/usr/ucb/ps auxwww | grep MS01 | grep mydomain01 | grep -v grep | nawk '\''NR==1{print mydomain01}'\'''
/usr/ucb/ps auxwww | grep MS01 | grep mydomain01 | grep -v grep | nawk 'NR==1{print mydomain01}'
++ /usr/ucb/ps auxwww
++ grep MS01
++ grep mydomain01
++ grep -v grep
++ nawk 'NR==1{print $2}'
+ mypid=6433
+ echo 'MYPID is:6433'
MYPID is:6433
+ kill -9 6433
+ for i in '$(echo $1 | sed "s/,/ /g")'
+ echo '/usr/ucb/ps auxwww | grep MS02 | grep mydomain01 | grep -v grep | nawk '\''NR==1{print mydomain01}'\'''
/usr/ucb/ps auxwww | grep MS02 | grep mydomain01 | grep -v grep | nawk 'NR==1{print mydomain01}'
++ /usr/ucb/ps auxwww
++ grep MS02
++ grep mydomain01
++ grep -v grep
++ nawk 'NR==1{print $2}'
+ mypid=5370
+ echo 'MYPID is:5370'
MYPID is:5370
+ kill -9 5370
Killed
 bash-3.2$

Can you please suggest what's wrong with my script ? Why MS03 and MS04 is not executed by the for loop ?


Moderator's Comments:
How to avoid arguments inside Nawk command? PLEASE PLEASE PLEASE USE CODE TAGS FOR DATA AS WELL !!!

Last edited by Scrutinizer; 10-05-2017 at 12:58 PM..
# 4  
Old 10-05-2017
Well, it looks like the ps is picking up the process ID ($$) of your script when it gets to 'M02' (whatever that it), and killing it.

Perhaps you could add "| grep -v $$" to your command, to exclude your script from the results, although this approach isn't good at all. It would be better to use positional fields from the output, as grep will pick up any old thing it finds, regardless of where it is in the output, unless it's very finely tuned.
The Following User Says Thank You to Scott For This Useful Post:
mohtashims (10-06-2017)
# 5  
Old 10-05-2017
... because the two greps are so unprecise that they match the own bash process
Code:
bash -x /web/run.sh MS01,MS02,MS03,MS04 mydomain01

The echo output differs because its arguments are in "quotes".
echo "print '$var'" expands differently because the ' are within "quotes" and have no special meaning.
In contrast to echo print '$var' where the ' are active quotes and protect the $var from expansion.

Maybe you can give an example how the processes look that are to be killed?
# 6  
Old 10-05-2017
This line:

Quote:
Originally Posted by mohtashims
Code:
echo "/usr/ucb/ps auxwww | grep $i | grep $2 | grep -v grep | nawk 'NR==1{print $2}'"

Won't work - at least not in the way you perhaps supposed it to work: the shell maintains one (only one!) flag of sorts for being inside or outside of a quotation. That means quotes cannot be nested and this:

Code:
"some-string 'foo bar' other string"

is NOT a single-quoted string inside a double-quoted string but in fact a double-quoted string with just a few single-quote-characters in it. In other words: the single-quotes lose their special meaning inside double-quotes.

This is why "...$2...." and "...'$2'..." are basically the same.

I hope this helps.

bakunin
# 7  
Old 10-06-2017
Quote:
Originally Posted by Scott
Well, it looks like the ps is picking up the process ID ($$) of your script when it gets to 'M02' (whatever that it), and killing it.

Perhaps you could add "| grep -v $$" to your command, to exclude your script from the results, although this approach isn't good at all. It would be better to use positional fields from the output, as grep will pick up any old thing it finds, regardless of where it is in the output, unless it's very finely tuned.
grep -v $$ helped !!. Thank you.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to avoid "Too many arguments" error, when passing a long String literal as input to a command? cool.aquarian Shell Programming and Scripting 4 03-06-2015 06:44 PM
Substitute variable inside nawk mohtashims Shell Programming and Scripting 8 05-28-2014 07:25 AM
Need to pass shell arguments into Nawk/awk Selva_2507 Shell Programming and Scripting 6 04-06-2014 03:33 AM
Using echo to print arguments inside a function kristinu Shell Programming and Scripting 3 01-03-2014 10:10 AM
passing arguments to unix command or script inside tclsh Blue_shadow Shell Programming and Scripting 1 01-10-2012 04:15 PM
Reading input record from inside nawk Shahul Shell Programming and Scripting 2 05-31-2011 09:49 AM
awk command to avoid loops vijayarajvp Shell Programming and Scripting 2 10-12-2009 01:35 PM
using cp command inside nawk piscean_n Shell Programming and Scripting 1 04-08-2009 08:31 AM
case command inside awk/nawk abdulaziz Shell Programming and Scripting 9 03-09-2009 09:15 PM
two arguments with nawk program whatisthis Shell Programming and Scripting 4 02-22-2005 02:45 PM


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

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password