0

I'm trying to seperate the ip and port using a bash script. Below is what I have

my proxies.txt file

48.54.87.45:1000
48.54.87.45:1001
48.54.87.45:1002

My bash script

#!/bin/sh
input="proxies.txt"
while IFS= read -r var
do
  echo "$var"
  IFS=':' read proxy port <<< "$var"
  echo "$proxy"
  echo "$port"
  echo "---------------"
done < "$input"

But I get the following error

Syntax error: redirection unexpected

I have tested the loop and it does read the file one by one. It's the IFS line which is giving the error.

What have I done wrong?

david
  • 997
  • 3
  • 14
  • 34
  • Make sure your shell is bash, not `/bin/sh`. – Charles Duffy Apr 13 '18 at 19:36
  • And btw, make it `while IFS=: read -r proxy port _` and you'll only need the one `read`. Or if you *are* going to do a full-line `read`, it's cheaper in terms of performance to do `proxy=${var%%:*}` and `port=${port#*:}` – Charles Duffy Apr 13 '18 at 19:36

1 Answers1

1

<<< is bash-only syntax. If you try to use it in a script run with /bin/sh (with either a #!/bin/sh shebang or run with sh scriptname), it will cause this error.

Start your script with #!/usr/bin/env bash, or another shebang that actually invokes bash.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441