Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to avoid arguments inside Nawk command? Post 303004624 by mohtashims on Thursday 5th of October 2017 09:22:06 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy