Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using While and grep combo- "syntax error near unexpected token `<' "

I am trying to direct the stdout of grep to the stdin of read command in the while loop

the below code gives me the syntax error - "syntax error near unexpected token `<' "

while read -r line ; do
 echo "Processing $line "
 IFS=: read var1 var2 var3 <<< $line
 if [ -n "$(expr match "$var3" '.*\(BEGIN\).*')" ]; then
   echo "Found BEGIN"
   (( var2 += 1 ))
   read -r line1
   IFS=: read var4 var5 var6 <<< $line1
   if [ -n "$(expr match "$var6" '.*\(END\).*')" ]; then
     echo "Found END"
     (( var5 -= 1 ))
     sed -i -e "$var2,$var5 s/# //" -e "$var2,$var5 s%/\* %%" -e "$var2,$var5 s% \*/%%" $var1
   fi
 else
   echo "Found NOTHING"
   sed -i -e "$var2 s%// %%" $var1
 fi
done < <(grep -H -r -n Uncomment *)
like image 201
Harika Yasa Avatar asked Jun 11 '26 11:06

Harika Yasa


2 Answers

The < <(...) syntax is only available in shells with ksh extensions, like ksh itself or bash. When I ran your code against sh I get precisely the same error.

Update (or add!) your shebang to be #!/bin/ksh or #!/bin/bash

like image 104
rrauenza Avatar answered Jun 13 '26 02:06

rrauenza


Running with a bash shebang will fix this issue. Place #!/bin/bash (or appropriate bash location #!/usr/bin/env bash) as the first line of your file.

If you need a way to do this in sh as well change your < <() to a <<< "$()" which feeds in the result of the subshell as a string, I just tested this and it works in bash and sh

like image 30
Will Barnwell Avatar answered Jun 13 '26 02:06

Will Barnwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!