0

I am creating a script where I am redirecting the output of an awk command into a loop, but it's giving me this error:

sh: -c: line 3: syntax error near unexpected token `<'
sh: -c: line 3: `done < <(awk "{print}" inputfile.txt)'

I was able to run it against a different remote host, but this host is giving me an error. Does anyone know if some versions of sh/bash don't support that syntax, or know any alternatives to that syntax, or maybe spot a bug that I haven't been able to see? (I'm new to bash scripting, so even a point in the right direction would be helpful!)

Here is a pared-down version of my script that I was able to reproduce the issue on:

#!/usr/bin/env bash

host=$1

ssh $host 'while read line
do
echo "hi";
done < <(awk "{print}" inputfile.txt)'
Kaitlyn
  • 201
  • 3
  • 15
  • Are you running the script with `sh scriptname`? – Benjamin W. Oct 19 '20 at 23:00
  • I'm running it with ```bash scriptname``` Although I get the error when running it with either bash or sh – Kaitlyn Oct 19 '20 at 23:02
  • @Kaitlyn : From the error message, you can see that it is not bash which is producing the error message, because with bash, the message would be **bash : _bla bla_**., not **sh : ...**. – user1934428 Oct 20 '20 at 05:33

1 Answers1

1

It looks like the remote user on that host uses a shell that's not Bash to run the command, see this Q&A; a way around that is to either avoid Bashisms:

ssh "$host" 'awk "{print}" inputfile.txt \
    | while IFS= read -r line; do
        echo "$line"
    done'

which comes with its own pitfalls, see A variable modified inside a while loop is not remembered – alternatively, you could specify that the remote host should run Bash:

ssh "$host" <<'EOF'
bash -c 'while IFS= read -r line; do
    echo "$line"
done < <(awk "{print}" inputfile.txt)'
EOF
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Ok, that explanation makes sense, thank you. The second snippet specifying bash on the remote host didn't seem to work unfortunately. It's weird because I can see that bash is installed on the remote server, but it won't use bash I guess? I'm going to try reworking the "bashisms" out like your first example. – Kaitlyn Oct 19 '20 at 23:32
  • 1
    Piping my awk results to the while loop at the beginning worked. Thank you for your help! – Kaitlyn Oct 19 '20 at 23:52