How to avoid arguments inside Nawk command?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to avoid arguments inside Nawk command?
# 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:
Mod Comment PLEASE PLEASE PLEASE USE CODE TAGS FOR DATA AS WELL !!!

Last edited by Scrutinizer; 10-05-2017 at 01: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.
This User Gave Thanks to Scott For This Post:
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid error with ln command?

mkdir logs mkdir: Failed to make directory "logs"; File existsTo avoid this error i use the -p argument so it creates a folder only if it is does not exists like you see below. mkdir -p logs In the similar manner i wish to avoid this error with ln command ln -s /tmp/myfolder var ln: cannot... (4 Replies)
Discussion started by: mohtashims
4 Replies

2. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

3. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

4. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

5. Shell Programming and Scripting

Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments. Example: prargv "-e" "--examples" Inside prargv, I want to print all the arguments using echo echo "$@" This returns --examples rather than -e --examples" This problem can be fixed... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

7. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

8. Shell Programming and Scripting

using cp command inside nawk

Hello I have facing issue while using cp command inside nawk block. #!/bin/ksh my_name=$1 nawk -v my_name1=$my_name 'BEGIN { n = split(my_name1,names,":"); for (i=1;i<=n;i++) { print names; cpcmd = "cp " /tmp/test.txt" " ./sample system(cpcmd) } exit }' exit 0 i'am getting... (1 Reply)
Discussion started by: piscean_n
1 Replies

9. Shell Programming and Scripting

case command inside awk/nawk

well I found lot of topics about awk..about if command in awk.. but I had to implement this: nawk -F"|" ' $47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )} $47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )} $47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )} $47 ~ /0R0018/ { print >... (9 Replies)
Discussion started by: abdulaziz
9 Replies

10. Shell Programming and Scripting

two arguments with nawk program

Can someone help me to understand this part of code? /bin/nawk -f awkfile file1 file2 I know awkfile is the one with awk script. file1 is source file that needs to be processed. What is file2 two? Thanks for your help! (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question