Maven script shows that Java command is not executable - bash

After installation on Ubuntu, mvn -version prints:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
I detected that the problem is in
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly" >&2
echo "This environment variable is needed to run this program" >&2
echo "NB: JAVA_HOME should point to a JDK not a JRE" >&2
exit 1
fi
When I remove quotes from "$JAVACMD" it works perfectly
I saw that other scripts use commands with in quotes and I doubt that Maven released this script with errors. So, what seems to be the problem? Why my script won't work in original version? Type of script is #!/bin/sh but I tested this condition separately in new file with bash script. The result is the same. When I ask with quotes is command executable, result is false. When I ask without quotes is true
Edit:
I put directly that case in new bash file. The result is interesting
#!/bin/bash
JAVACMD1="$JAVA_HOME/bin/java"
JAVACMD2="/usr/lib/jvm/java-8-openjdk-amd64/bin/java"
echo $JAVACMD1
echo $JAVACMD2
if [ ! -x "$JAVACMD1" ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x $JAVACMD1 ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x "$JAVACMD2" ]
then
echo "NOT"
else
echo "YES"
fi
Result:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
NOT
YES
YES

The problem was in env variable JAVA_HOME. It started with one blank, but that I didn't see in echo command (I suppose it trims the string). In command prompt I also used echo, but when I listed all env variables I saw it

Related

A way to ignore exit status in gitlab job pipeline [duplicate]

In our project we have a shell script which is to be sourced to set up environment variables for the subsequent build process or to run the built applications.
It contains a block which checks the already set variables and does some adjustment.
# part of setup.sh
for LIBRARY in "${LIBRARIES_WE_NEED[#]}"
do
echo $LD_LIBRARY_PATH | \grep $LIBRARY > /dev/null
if [ $? -ne 0 ]
then
echo Adding $LIBRARY
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBRARY
else
echo Not adding $LIBRARY
fi
done
i.e. it checks if a path to a library is already in $LD_LIBRARY_PATH and if not, adds it.
(To be fair, this could be written differently (like here), but assume the script is supposed to achieve something which is very hard to do without calling a program, checking $? and then either doing one thing or doing another thing).
The .gitlab-ci.yml then contains
before_script:
- yum install -y <various packages>
- source setup.sh
but the runner decides to stop the before script the very moment $? is non-zero, i.e. when the if-statement decides to add a path to $LD_LIBRARY_PATH.
Now it is nice that the gitlab runner checks $? after each line of my script, but here it'd be great if the lines in .gitlab-ci.yml were considered atomic.
Is there a way to avoid the intermediate checks of $? in a script that's sourced in .gitlab-ci.yml?
Use command_that_might_fail || true to mask the exit status of said command.
Also note that you can use grep -q to prevent output:
echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY" || true
This will however also mask $? which you might not want. If you want to check if the command exits correct you might use:
if echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY"; then
echo "Adding $LIBRARY"
else
...
fi
I suspect that gitlab-ci sets -e which you can disabled with set +e:
set +e # Disable exit on error
for library in "${LIBRARIES_WE_NEED[#]}"; do
...
done
set -e # Enable exit on error
Future reading: Why double quotes matter and Pitfalls with set -e
Another trick that I am using is a special kind of "|| true", combined with having access to previous exit code.
- exit_code=0
- ./myScript.sh || exit_code=$?
- if [ ${exit_code} -ne 0 ]; then echo "It failed!" ; else echo "It worked!"; fi
The $exit_code=$? always evaluates to "true" so you get a non failing command but you also receive exit_code and you can do whatever you want with it.
Note please, that you shouldn't skip the first line or exit_code will be uninitialized (since on successful run of script, the or'ed part is never executed and the if ends up being)
if [ -ne 0 ];
instead of
if [ 0 -ne 0 ];
Which causes syntax error.

Missing environment parameter in shell script

I have problem to launch the following shell script. I
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Missing environment parameter"
exit 1
fi
environment=$1
npm=/opt/plesk/node/8/bin/npm
gulp=/opt/plesk/node/8/bin/gulp
grunt=/opt/plesk/node/8/bin/grunt
composer=/usr/bin/composer
home=$(pwd)
logs=${home}/logs
echo "home dir: ${home}"
echo "logs dir: ${logs}"
echo "environment: ${environment}"
...more code follow
From terminal I got the following error:
Missing environment parameter
I launch the following command from terminal.
./build.sh
Which parameter should I enter in my command?

Check if all "CMD_xxx" are executable using loop

How do I check if all variables "CMD_xxx" are executable in bash script?
I can check one by one with [ or test command, but I'd like to know how I check them with loop.
[Question] How do I check all variables which start "CMD_" with loop command?
This is my image. (Does not work)
for v in ${!CMD_#}
do
if [ ! -x ${$v}} ]; then
echo ${${v}} does not executable.
exit 1
fi
done
My environment is CentOS6.6 bash 4.1.2
That's not how you perform indirection.
if [ ! -x "${!v}" ]

Adding key to ssh agent when shell is opened

Why isn't this code working in my .bash_profile?
echo $SSH_AUTH_SOCK
if [ -z "$SSH_AUTH_SOCK" ]; then
ssh-add
fi
My identity is not getting added when shell is opened. The variable $SSH_AUTH_SOCK is being echoed when the shell opens:
/private/tmp/com.apple.launchd.UKHCb6uhHB/Listeners
So if the variable is defined, why isn't the ssh-add statement being evaluated?
If I run ssh-add manually from the command line, or place it on it's own separate line of my .bash_profile, it successfully runs and adds my identity to the ssh agent.
Additionally, making thing simpler and just trying to echo a value doesn't work, for example, I do not see test being echoed after placing this in the file:
echo $SSH_AUTH_SOCK
if [ -z "$SSH_AUTH_SOCK" ]; then
echo test
fi
Whoops, I was using the wrong operator. Should be -n, not -z.

Printing different messages by command exit code

So I have this in my .sh file:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
make OS=APPLE -k
if [$? -eq 0]
then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi
The problem is, bash prints this message I don't even understand:
./build-mac.sh: line 7: [0: command not found
Any help or advice is appreciated!
I'm running on a Mac with Bash.
You need to add additional spaces on the if line around the square brackets. Change your code to:
if [ $? -eq 0 ]
The reason this is required is that [ is a command itself (a synonym on *nix for test) and you need to execute the [ command not the (non-existing) [$? command.
Might as well not use $? at all:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
if make OS=APPLE -k ; then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi

Resources