"syntax error near expected token" using process substition - bash

It's a bit of embarrassing question but i can not found the error. I am trying to do process substitution. Here is my code
while read compareFile1 <&3 && read compareFile2 <&4; do
echo compareFile1
echo compareFile2
done 3< <(tail -n+4 test2.txt) 4< <(tail -n+4 test2.txt)
but the error is,
sh.sh: line 7: syntax error near unexpected token `<'
sh.sh: line 7: `done 3< <(tail -n+4 test2.txt) 4< <(tail -n+4 test2.txt)
Any can help?

Process substitution is not an available feature in POSIX sh (#!/bin/sh, also invoked with sh yourscript); despite tagging this question "bash", you're clearly executing your script with a non-bash shell (or are otherwise entering portability mode, as with set -o posix).
Use bash instead; thus, putting #!/bin/bash at the beginning of your script, or invoking it with bash yourscript if specifying an interpreter manually.

Related

Shell read from files with tail

I am currently trying to read from files with shell. However, I met one sytax issue. My code is below:
while read -r line;do
echo $line
done < <(tail -n +2 /pathToTheFile | cut -f5,6,7,8 | sort | uniq )
However, it returns me error syntax error near unexpected token('`
I tried with following How to use while read line with tail -n but still cannot see the error.
The tail command works properly.
Any help will be apprepricated.
process substitution isn't support by the posix shell /bin/sh. It is a feature specific to bash (and other non posix shells). Are you running this in /bin/bash?
Anyhow, the process substitution isn't needed here, you could simple use a pipe, like this:
tail -n +2 /pathToTheFile | cut -f5,6,7,8 | sort -u | while read -r line ; do
echo "${line}"
done
Your interpreter must be #!/bin/bash not #!/bin/sh and/or you must run the script with bash scriptname instead of sh scriptname.
Why?
POSIX shell doesn't provide process-substitution. Process substitution (e.g. < <(...)) is a bashism and not available in POSIX shell. So the error:
syntax error near unexpected token('
Is telling you that once the script gets to your done statement and attempts to find the file being redirected to the loop it finds '(' and chokes. (that also tells us you are invoking your script with POSIX shell instead of bash -- and now you know why)

Using while loop without process substitution, while allowing global variable value modification inside the loop

I have a script that when I run from a file manager (Filza), it return an error saying
command substitution: syntax error near unexpected token `('.
line 56: `paste -d'\n' <(echo "$var1") <(echo "$var2"))'
But when I run it from a terminal (./myscript.sh), it ran with no error. Here's the code that gave the error:
#!/bin/bash
var1="A
B
C"
var2="1
2
3"
globalvar=0
while read v1 && read v2; do
globalvar=$(echo $v1 $v2)
done<<<$(paste -d'\n' <(echo "$var1") <(echo "$var2"))
As commented below, it's probably some shell doesn't allow process substitution, thus why it failed. This command is running on iOS environment (jailbroken). Is there alternative way to implement this? Thanks in advance!
Try using 'here document' (<<) instead of 'here string' (<<<). It is supported by most shells.
while read v1 && read v2; do
globalvar=$(echo $v1 $v2)
done <<__END__
$(paste -d'\n' <(echo "$var1") <(echo "$var2"))
__END__
The other option is create a shell wrapper that will force bash (from the question, looks like bash is installed and working). Rename original script to script-run, and modify the shell script to call the script-run
#! /bin/sh
exec /bin/bash ${0%/*}/script-run "$#"
Or other equivalent.

Syntax error in shell script saying unexpected token `(

I have written the following shell script
while :; do
status=$($EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --list | grep "CopyLogs" | awk '{print $1}')
[[ $status == +( *RUNNING*|*PENDING*|*WAITING* ) ]] || break
sleep 60
done
Its giving me an error in line 3 saying syntax error in conditional expression: unexpected token('' . I tried giving whitespaces between my braces, but its not working.
Can anyone help me out.
Looks like you are trying to use extended globbing. Make sure you have shopt -s extglob somewhere earlier in your script, or rewrite to use standard globbing.
#!/bin/sh
while :; do
case $($EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --list | awk '/CopyLogs/{print $1}') in
*RUNNING*|*PENDING*|*WAITING* ) sleep 60;;
*) break;;
esac
done
Since there are no remaining Bashisms, this script is now POSIX sh compatible. (Personally, I also think it is more readable this way.)
(Note also the fix for the useless grep | awk.)

Syntax Error in bash while loop

What is the error in that?
j=0
filenames=("")
filedates=("")
while read line
do
filenames[$j]="$line"
filedates[$j]=$(stat -c %y ${filenames[$j]} | cut -d ' ' -f1)
(( j++ ))
done < <(ls -t *.gz)
Out:
script.sh: line 9: syntax error near unexpected token `<'
script.sh: line 9: `done < <(ls -t *.gz)'
Really i don't know the error in that while loop, i tested it on several machine but same problem
The problem is that you're using bash specific <(process substition), but running your script with /bin/sh.
Use bash script.sh instead of sh script.sh, and make sure the shebang uses bash and not sh.
use a for loop
for file in *t.gz
do
...
done

Redirector "<<<" in Ubuntu?

I'm getting this error
Syntax error: redirection unexpected
in the line:
if grep -q "^127.0.0." <<< "$RESULT"
How I can run this in Ubuntu?
<<< is a bash-specific redirection operator (so it's not specific to Ubuntu). The documentation refers to it as a "Here String", a variant of the "Here Document".
3.6.7 Here Strings
A variant of here documents, the format is:
<<< word
The word is expanded and supplied to the command on its
standard input.
A simple example:
$ cat <<< hello
hello
If you're getting an error, it's likely that you're executing the command using a shell other than bash. If you have #!/bin/sh at the top of your script, try changing it to #!/bin/bash.
If you try to use it with /bin/sh, it probably assumes the << refers to a "here document", and then sees an unexpected < after that, resulting in the "Syntax error: redirection unexpected" message that you're seeing.
zsh and ksh also support the <<< syntax.
if grep -q "^127.0.0." <<< "$RESULT"
then
echo IF-THEN
fi
is a Bash-specific thing. If you are using a different bourne-compatable shell, try:
if echo "$RESULT" | grep -q "^127.0.0."
then
echo IF-THEN
fi
It works for me on Ubuntu, if I complete you IF block:
if grep -q "^127.0.0." <<< "$RESULT"; then echo ""; fi

Resources