Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: [ / test do not support expressions specified with escaped parentheses #1036

Closed
dtrudg opened this issue Oct 12, 2023 · 1 comment
Closed

Comments

@dtrudg
Copy link
Contributor

dtrudg commented Oct 12, 2023

Version 3.7.0

Running the following excerpt, taken from the start of a script that NVIDIA inserts into their container images, through gosh results in the error 13:4: not a valid test operator: (

#!/bin/sh

set -x 

NV_DRIVER_VERS=$(sed -n 's/^NVRM.*Kernel Module *\([^() ]*\).*$/\1/p' /proc/driver/nvidia/version 2>/dev/null | sed 's/^$/unknown/')

export _CUDA_COMPAT_PATH=${_CUDA_COMPAT_PATH:-/usr/local/cuda/compat}

# Set paths to use
_CUDA_COMPAT_CHECKFILE="${_CUDA_COMPAT_PATH}/.${NV_DRIVER_VERS}.$(hostname).checked"

# If the CUDA driver was detected and the compat check hasn't been flagged as done yet, proceed
if [ \( \( -n "${NV_DRIVER_VERS}" -a -e /dev/nvidiactl \) -o -e /dev/nvgpu \) -a ! -e "${_CUDA_COMPAT_CHECKFILE}" ]; then
  echo
fi

It appears from a simpler example, that mvdan.cc/sh / gosh doesn't support parentheses in test / [ expressions, as below... where they are written \( to avoid subshell evaluation.

$ cat paren-test.sh 
#!/bin/sh

if [ \( "bob" == "bob" \) ]; then
  echo "hello ["
fi

if test \( "bob" == "bob" \); then
  echo "hello test"
fi

$ /bin/sh paren-test.sh 
hello [
hello test

$ gosh paren-test.sh 
3:4: not a valid test operator: bob
3:4: not a valid test operator: bob
7:4: not a valid test operator: bob
7:4: not a valid test operator: bob

Is support for parentheses in test / [ expressions something that would be in scope for this package, or does it fall under the other caveats around ambiguity etc?

@mvdan
Copy link
Owner

mvdan commented Oct 14, 2023

Thanks for spotting this and filing an issue. We have supported parentheses in the [[ bash test syntax for years, but we didn't yet support them with classic test commands, surprisingly enough!

mvdan added a commit that referenced this issue Oct 14, 2023
Spotted this parser issue when investigating #1036.
Note that parsing of [[ happens in the syntax parser,
but the classic [ test is done in the interpreter.
Both treat both binary operators as having the same precedence.
@mvdan mvdan closed this as completed in 8b67386 Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants