0

See solution at end of post.
I'm running into a frustrating problem. Thanks for any help you can give. What I'm trying to do is get all occurrences of certain values in an error file. I'm using process substitution to parse error output files. I created a test script with the following code and it works perfectly.

#--------------------
# Get all values of affiliate
echo "Building array of affiliates with errors"
while read -r line; do
    AffiliateName_array+=($line)
    i=$(( ${#AffiliateName_array[@]} - 1 ))
    echo "  ScriptName_array | index --> $i | $line"
done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')

The echo output looks like this:

Building array of affiliates with errors
index --> 0 | affiliate1
index --> 1 | affiliate2

Then I cut and pasted that exact code into the final script and it throws the error:

path/AR_exit_process.sh: line 166: syntax error near unexpected token `<'
path/Affiliate_Remits/AR_exit_process.sh: line 166: ` done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')'

Here's the code from my final script (exactly the same as the test script since it was cut and pasted):

#--------------------
# Get all values of affiliate
echo "Building array of affiliates with errors"
while read -r line; do
    AffiliateName_array+=($line)
    i=$(( ${#AffiliateName_array[@]} - 1 ))
    echo "  ScriptName_array | index --> $i | $line"
done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')

NOTES:
All scripts have the #!/bin/bash bang line
The scripts run on the same server
The test script is called directly from the command line but
the final script is 2 layers deep. All calling scripts use #!/bin/bash

My main reference: https://wiki.bash-hackers.org/syntax/expansion/proc_subst

SOLUTION: As Charles suggested I added a line to both the test and final scripts with the command: set -o I found that when running the test script, I got the value posix off. However, when I ran the final script with set -o I got posix on. What this means is that the test script was running under bash while the final script was running under sh. The solution is to add the line set +o posix to turn posix off before executing the code. What I'm still not clear about is how the posix setting is different on the same server. More research ...

BradD
  • 35
  • 5
  • 1
    This implies you're running your script with `sh`. Process substitution is a `bash`-only feature; it's not available in `sh`. Make sure there's a proper shebang (`#!/usr/bin/env bash` or similar), and don't ever use `sh yourscript` for _bash_ scripts. – Charles Duffy Oct 12 '22 at 22:58
  • (if you content that this was closed incorrectly, and that you're getting this issue even when the software really is running under bash, please provide a [MRE] -- the shortest possible code we can copy and paste without changes to see the problem on our own systems) – Charles Duffy Oct 13 '22 at 11:45
  • (you also might edit the script to `echo "$BASH_VERSION` before it reaches the failure point -- if that's nonempty it tells us the shell really is bash; if it is bash, check if the output of `set -o`, again when run from the same script emitting the bug, says that the `posix` compatibility mode is enabled) – Charles Duffy Oct 13 '22 at 11:48
  • Thanks Charles. As I noted in my question, all scripts have #!/bin/bash. I read the two links listed as duplicates before posting this question. What I was trying to express is that the code snippet executes correctly from my test script but when I cut and paste it into the final script, it fails due to a syntax errors. – BradD Oct 13 '22 at 22:22
  • So I know the issue is not syntax, it's environmental. Your suggestions actually led me to the problem. echo $BASH_VERSION --> 4.2.46(2)-release : all good here. set -o for the test script --> 'posix off' set -o for the final script --> 'posix on' Why are the posix settings different on the same server? And how do I change that so that the final script completes successfully? – BradD Oct 13 '22 at 22:23
  • Answer found. I added `set +o posix` to my script just before the process substitution and the my script worked. Please open this issue up again in case others run into the same issue. It's not a duplicate of the ones questioning syntax. I don't know how posix can be on at one point and off at another unless there is some set statement somewhere else. – BradD Oct 13 '22 at 22:40
  • bash turns on posix mode when it's started under the `sh` name. – Charles Duffy Oct 14 '22 at 14:37
  • There _are_ other, less-likely ways it can be enabled; there's a compile-time flag to turn it on by default, or the `BASHOPTS` environment variable can be exported by the parent process, or there could be a `BASH_ENV` or `ENV` file that runs `set -o posix`; but by far the most likely is the shell interpreter being started as `sh`, as the linked duplicate covers. – Charles Duffy Oct 14 '22 at 14:40
  • (you did indeed specify `#!/bin/bash` shebangs, but if someone runs `sh scriptname` the shebang is ignored; so it's relevant information, but not sufficient on its own) – Charles Duffy Oct 14 '22 at 14:42

0 Answers0