0

I'm using here document of sh to run some commands. now I want to parse the output of those commands using awk. However, everytime I execute it, I get the output of the command append with something like this "% No such child process"

This is how my script looks like.

#!/bin/sh
com = "sudo -u username /path/of/file -l"
$com <<EOF | awk '{print $0}'
Commands.
.
.
.
EOF

How am I going to use heredoc and pipeline without appending that unwanted string? Thanks

user3714598
  • 1,733
  • 5
  • 28
  • 45

2 Answers2

0

There's no problem, check :

$ cat <<EOF | awk '{print $1}'
a b c
1 2 3
EOF
a
1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • it's weird. because if you print $0, it gives you the output plus something like this at the end "% No such child process 4330" – user3714598 Dec 13 '14 at 05:38
0

Your variable assignment is wrong in a couple of ways. First, you aren't actually assigning a variable; you're trying to run a command named com whose arguments are = and a string "sudo ...". Spaces must not be used on either side of the =:

com="sudo ..."

Second, command lines should not be stored in a variable; the shell's parser can only make that work they way you intend for very simple commands. Type the command out in full, or use a shell function.

com () {
    sudo -u username /path/to/file -l
}

com <<EOF | awk '{print $0}'
...
EOF
chepner
  • 497,756
  • 71
  • 530
  • 681