0

I'm trying to create a shell script that reads and writes to certain text files on two second intervals. When I run the script by itself (without watch), it works perfectly fine. However, when I run it using watch -n 2 ./feedcmd.sh, I'm given this error: ./feedcmd.sh: 2: ./feedcmd.sh: Syntax error: "(" unexpected.

I've tried adding an extra pair of parentheses around that phrase, but still no luck.

Here's my shell script:

$(tmux capture-pane -t "$tes3-server" -pS 32768 > one.txt)
output=$(comm -23 <(sort one.txt) <(sort two.txt))
len=`expr "$output" : '.*'`
if [ "$len" -gt 0 ]; then
       $(echo "$output" > fin.txt)
       $(cat one.txt > two.txt)
       $(echo 0 > done.d)
fi
echo "done"
  • Your script doesn't appear to have a "shebang" - watch is likely executing it using /bin/sh, which doesn't support process substitutions like <(command) – steeldriver Mar 07 '21 at 04:18
  • I see. I think I'll just put the script in a loop and use sleep to run it in intervals. Thank you. Also, what exactly do you mean by a "shebang"? – Michael Kellam Mar 07 '21 at 04:37
  • 1
    https://en.wikipedia.org/wiki/Shebang_(Unix) – A.B Mar 07 '21 at 09:31

1 Answers1

0

Your script doesn't have a shebang. The watch command is likely executing it using /bin/sh, which doesn't support the <(command) process substitution construct1:

$ cat myscript
cat <(echo foo)

$ watch -n 2 ./myscript

==>

Every 2.0s: ./myscript                                                                   t400s: Sun Mar  7 09:09:04 2021
./myscript: 1: ./myscript: Syntax error: "(" unexpected

whereas

$ cat myscript
#!/bin/bash
cat <(echo foo)

$ watch -n 2 ./myscript

==>

Every 2.0s: ./myscript                                                                   t400s: Sun Mar  7 09:07:32 2021
foo

  1. more precisely, it passes the command to sh -c ' ... ' which then follows the rules outlined in Which shell interpreter runs a script with no shebang?
steeldriver
  • 81,074