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
  • 235,242
John
  • 221

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
  • 42,502
  • 8
  • 126
  • 159
1

Your awk command does not have a closing '.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
Guru
  • 19