×
UNIX.COM Login
Username:
Password:  
Show Password





×
UNIX.COM Login
Hello Unregistered
Welcome from United States
You are somewhere around Ashburn
Last Visit: 1 Minute Ago @ 03:21 PM
Bits: 0
UNIX Time: 1531682483

👤


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?

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-05-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 13 July 2018, 12:41 AM EDT
Posts: 810
Thanks: 216
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 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: 8 July 2018, 4:41 PM EDT
Posts: 9,062
Thanks: 402
Thanked 1,279 Times in 1,082 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 10-05-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 13 July 2018, 12:41 AM EDT
Posts: 810
Thanks: 216
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 12:58 PM..
    #4  
Old 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: 8 July 2018, 4:41 PM EDT
Posts: 9,062
Thanks: 402
Thanked 1,279 Times in 1,082 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 10-05-2017   -   Original Discussion by mohtashims
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 15 July 2018, 1:04 PM EDT
Location: Simplicity
Posts: 4,151
Thanks: 365
Thanked 1,417 Times in 1,273 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 10-05-2017   -   Original Discussion by mohtashims
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 3 July 2018, 11:43 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,786
Thanks: 112
Thanked 1,698 Times in 1,248 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 10-06-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 13 July 2018, 12:41 AM EDT
Posts: 810
Thanks: 216
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
👤 Login to reply

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
passing arguments to unix command or script inside tclsh Blue_shadow Shell Programming and Scripting 1 01-10-2012 04:15 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:21 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.