1

I have this command line that I enter into terminal and it works as intended:

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID: / {if ($2 > 500) print name}'

What I want to do is use sh -c "insert command string here" and when I try and use the above statement, it gives me these errors:

awk: syntax error at source line 1
 context is
    /^RecordName:/ >>>  {name=} <<< 
awk: illegal statement at source line 1
awk: illegal statement at source line 1

Any idea how I would correct this? I need it for a program in objective-c.

slhck
  • 223,558
  • 70
  • 607
  • 592
John
  • 191
  • 2
  • 8

2 Answers2

4

Single quotes don't prevent variable expansion inside double quotes:

$ echo "a'$RANDOM'"
a'23976'

You could replace $ with \$ or ' with '\'':

$ sh -c "echo a b | awk '{print \$2}'"
b
$ sh -c 'echo a b | awk '\''{print $2}'\'
b

Or use a heredoc:

sh -s <<'END'
echo a b | awk '{print $2}'
END
Lri
  • 40,894
  • 7
  • 119
  • 157
1

Your awk command does not have a closing '.

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Guru
  • 19
  • 1