Syntax error near unexpected token `|'

Hi All;

I try to write a bash code and I am using command substitution. My code is like:

#!/bin/bash

IP="10.0.0.1 10.0.0.2"
PORT="22 80"
USERNAME="admin"
SCRIPT_HOST="adminHost"
HOME_DIR=/home/admin
SCRIPT_DIR=$HOME_DIR/scripts
script="sudo /my_remote_script.sh"
SSH="/usr/bin/ssh -t -o ConnectTimeout=10 -l ${USERNAME} ${SCRIPT_HOST}"

declare -a array1
declare -a array2
declare -a array3


for ip in ${IP}" ; do
    for port in ${PORT} ; do

        rmt_command1=" $script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' "

        rmt_command2=" $script -i $ip -p $port  | awk -v IGNORECASE=1 '/pattern2/ {print \$5}' "

        rmt_command3=" $script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern3/ {print \$5}' "

        array1[$ip,$port]=$($SSH "$rmt_command1")
        array2[$ip,$port]=$($SSH "$rmt_command2")
        array3[$ip,$port]=$($SSH "$rmt_command3")

    done
done

for ip in ${IP}
    
    for port in {PORT}
        printf   ${array1[$ip,$port]}
        printf   ${array2[$ip,$port]}
        printf   ${array3[$ip,$port]}

    done

done


And I receive an error message like:

./test_sh: line 20: syntax error near unexpected token `|'
./test_sh: line 20: `        rmt_command1=" $script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' "'

What could be wrong?

At first sight I already see a few problems:

for ip in "${IP}" ; do
for port in ${PORT} ; do
rmt_command1=" $script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' "

Where is the $script variable set?

Sorry I have 2 copies of same file $script was actually $remote_script. Neither worked. Corrected.

I guess this must be your problem
change this

$ rmt_command1=" $script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' "
$ echo $rmt_command1
-i -p | awk -v IGNORECASE=1 '/pattern1/ {print $4}'

to

$ rmt_command1=" \$script -i \$ip -p \$port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' "
$ echo $rmt_command1
$script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print $4}'

Try and let me know..

use backticks instead of double quotes if you want your script to be executed here:

`$script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}' `

instead of

$script -i $ip -p $port | awk -v IGNORECASE=1 '/pattern1/ {print \$4}'

same for all those 3 lines.

i didnt see the error after the change.

I don't think he wants the code executed there; it has to be run on the server which he makes an SSH connection to... Therefore, don't change the quotes.

1 Like

[quote=subbeh;302876685]
At first sight I already see a few problems:

for ip in "${IP}" ; do
Do we need the quotes here? This will make the the value of ip as  10.0.0.1 10.0.0.2 instead of taking them one by one.

This would be better
for ip in ${IP} ; do

Even after escaping dollars, I had same error:

./test.sh 
./test.sh: line 20: syntax error near unexpected token `|'
./test.sh: line 20: `		rmt_command1=" \$script -i \$ip -p \$port | awk -v IGNORECASE=1 '/ipv4 count/ {print \$4}' "' 

---------- Post updated at 12:50 PM ---------- Previous update was at 12:23 PM ----------

I executed all line in the script one by one. Actually I found it's an unnecessary double quote issue in the original code. After I removed that, error resolved. Thanks all to who replied to this post.