0

I'm trying to find what sort of value in ${RESULT} as used in this script;

#!/bin/ksh
...
echo ${RESULT}|awk '{if ($0 ~ /RUNNING mode/) {print "STATUS=0"; print "PID="'${PID}'; print "INFO="$0; print '$PID' > "'${INSTALLDIR}/domain'/sample_pid.txt"} else {print "STATUS=1"; print "PID=-1"; print "INFO="$0} }'

Could cause this output:

awk: cmd. line:1: {if ($0 ~ /RUNNING mode/) {print "STATUS=0"; print "PID="27708
awk: cmd. line:1:                                                              ^ unexpected newline or end of string

I've tried throwing many combinations of special characters into ${RESULT} and it never has issues. I'll probably rewrite the command anyway but I am curious about what ${RESULT} could be both as an educational point and also to help me reproduce the error that was reported to me from a test system, which I haven't otherwise been able to reproduce.

Thanks!

Compo
  • 36,585
  • 5
  • 27
  • 39
John Kelty
  • 107
  • 8
  • You can easily debug it. Use the standard way to pass shell variables to awk, not like this. See: https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script `-v pid="$PID"` etc – thanasisp Oct 15 '20 at 20:10

1 Answers1

4

Your code has issues with how to call shell variables in awk + redundant use of functions (e.g. print). Keeping awk code like awk '.........' Input_file is what is recommended. You could take the following as a start, but since no samples were given, I have not tested it at all; it's completely based on the attempt by OP.

Also whenever you are writing output to a output file it's better to place output file name in (output file name)

echo "$RESULT" | 
awk -v installdir="${INSTALLDIR}/domain" -v pid="$PID" -v outfile="sample_pid.txt" '
{
  if ($0 ~ /RUNNING mode/){
     print "STATUS=0\nPID=" pid "\nINFO="$0 >  (installdir"/"pid"/"outfile)
  }
  else{
     print "STATUS=1\nPID=-1\nINFO="$0
  }
}'
tripleee
  • 175,061
  • 34
  • 275
  • 318
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93