Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
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 ?
Sponsored Links
    #2  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 18 November 2017, 4:24 PM EST
Location: Zürich
Posts: 8,821
Thanks: 366
Thanked 1,196 Times in 1,022 Posts
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?
Sponsored Links
    #3  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
RedHat

Quote:
Originally Posted by Scott View Post
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 01:58 PM..
    #4  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 18 November 2017, 4:24 PM EST
Location: Zürich
Posts: 8,821
Thanks: 366
Thanked 1,196 Times in 1,022 Posts
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)
Sponsored Links
    #5  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 18 November 2017, 2:39 PM EST
Location: Simplicity
Posts: 3,822
Thanks: 318
Thanked 1,281 Times in 1,159 Posts
... 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?
Sponsored Links
    #6  
Old Unix and Linux 10-05-2017   -   Original Discussion by mohtashims
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 15 November 2017, 4:57 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,633
Thanks: 107
Thanked 1,603 Times in 1,180 Posts
This line:

Quote:
Originally Posted by mohtashims View Post

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
Sponsored Links
    #7  
Old Unix and Linux 10-06-2017   -   Original Discussion by mohtashims
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
Quote:
Originally Posted by Scott View Post
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 07:44 PM
passing arguments to unix command or script inside tclsh Blue_shadow Shell Programming and Scripting 1 01-10-2012 05:15 PM
using cp command inside nawk piscean_n Shell Programming and Scripting 1 04-08-2009 09:31 AM
case command inside awk/nawk abdulaziz Shell Programming and Scripting 9 03-09-2009 10:15 PM
two arguments with nawk program whatisthis Shell Programming and Scripting 4 02-22-2005 03:45 PM



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