1

I came across a script that has the line:

java_version=$($JAVA_HOME/bin/java -version 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d '_' -f 1,3)

followed by the test:

if [[ -n "$java_version" ]]; then
    ...

The test when called from the command line succeeds. When called from Java application fails.

Some useful notes:

  • The first line in the script is #!/usr/bin/env bash.
  • The fact that the test succeeds when called from the command line means java_version is not empty. For further verification, I echo'd right before the test the value of java_version into some external file and found the value there.
  • I changed the double brackets to singular and the test succeeded in both calling the script from the command line and the Java application.
  • I'm running GNU/Linux Ubuntu 18.04, bash version 4.4.20, Java 8 (Oracle JDK).

Any ideas on what might cause this change of behavior?

joker
  • 574
  • 6
  • 14
  • I don't actually know, but to guess: `[ ]` is the POSIX test, whereas `[[ ]]` contains additional functionality in `bash`, `zsh`, and `ksh`. What shell does Java use? Perhaps `strace` might reveal how the script is called from Java. – Christopher Oct 21 '19 at 11:37
  • 1
    how does it fail? silently just returning a falsy status, or does it give an error message? And yes, the fact that `[[` isn't standard comes first to mind, but if the script gets run with a shell that doesn't support it, you should get an error – ilkkachu Oct 21 '19 at 11:48
  • @Christopher, I don't know the shell used by Java. But doesn't the first line in the script eliminates questioning this part? – joker Oct 21 '19 at 11:50
  • 2
    @joker, no, if the script gets run with `/bin/sh scriptname.sh` – ilkkachu Oct 21 '19 at 11:50
  • @ilkkachu, By fail I mean giving false status code (executes the `else` part). – joker Oct 21 '19 at 11:50
  • @ilkkachu, unfortunately I don't know. I don't have the source code. I'm just trying to tackle the problem and pinpoint the issue (this is interesting to expand my knowledge and always tease my understanding of things). – joker Oct 21 '19 at 11:52
  • 2
    the hashbang line only applies if the script gets launched through `exec()`, i.e. `./scriptname.sh`, see e.g. [here](https://unix.stackexchange.com/q/87560/170373). You could put something like `echo "bash version = $BASH_VERSION"` in the script to see if it really runs with Bash – ilkkachu Oct 21 '19 at 11:53
  • 1
    Conversely, `[ ]` is sufficient; there's no purpose in using `[[ ]]` for the simple test. – Christopher Oct 21 '19 at 11:57
  • @Christopher, I totally agree. – joker Oct 21 '19 at 11:57
  • 2
    I just found that the running shell is `sh`. I guess this explains why `[[ ]]` doesn't work. – joker Oct 21 '19 at 13:24

1 Answers1

1

I added the following line to the script in order to find the running shell:

ps -p $$ > /path/to/some_file

Found the running shell is /bin/sh which doesn't support the double brackets syntax. Double brackets are only supported in a few shells (Source).

All credits go to the guys helped with their comments under my post.

joker
  • 574
  • 6
  • 14