original-mawk icon indicating copy to clipboard operation
original-mawk copied to clipboard

Improper shell quote handling

Open GreenBeard opened this issue 6 years ago • 2 comments

If the title is unsatisfactory feel free to change it (I was not sure of a proper title nor of the root cause of the bug).

The following awk program does not function correctly (run with mawk -f ./filename.awk):

function PrintTestBroken(var)
{
  "printf '" var "'" | getline result
  close("printf '" var "'")
  print result
}

function PrintTestWorking(var)
{
  cmd = "printf '" var "'"
  cmd | getline result
  close(cmd)
  print result
}

function PrintTestWorkingTwo(var)
{
  "printf 'test'" | getline result
  close("printf 'test'")
  print result
}

BEGIN {
  testvar = "test"
  PrintTestWorking(testvar)
  PrintTestWorkingTwo(testvar)
  PrintTestBroken(testvar)
}

It outputs:

test
test
/bin/sh: 1: Syntax error: Unterminated quoted string
test

when it should output:

test
test
test

gawk has the proper output if that helps at all.

GreenBeard avatar Jun 25 '18 09:06 GreenBeard

thanks (I can reproduce this)

ThomasDickey avatar Jun 27 '18 01:06 ThomasDickey

Also I noticed that the following works (notice parentheses):

function PrintTestWorkingThree(var)
{
  ("printf '" var "'") | getline result
  close("printf '" var "'")
  print result
}

GreenBeard avatar Jun 27 '18 08:06 GreenBeard