Bash script: passing a "string with empty space" onto a command - bash

I have following bash script:
set -o xtrace
TEST_CASE=#Login
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
When i run the script the mvn command become:
mvn clean install '-Dcucumber.options=--tags #Login'
Note that the single quote is wrapping the whole -D argument, and the original double quote has disappear. But what i want to see is below:
mvn clean install -Dcucumber.options="--tags #Login"
What is wrong with my original script? how should I update to make it right?

The quotes are not part of the argument; they are there solely to prevent the shell from breaking the string beginning with --D into two arguments to be passed to mvn. The following are all equivalent, creating three arguments:
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
mvn clean install "--Dcucumber.options=--tags ${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags" ${TEST_CASE}"
The following creates 4 arguments, since the whitespace between tags and the parameter is not quoted:
mvn clean install --Dcucumber.options=--tags "${TEST_CASE}"
You can quote the space individually in a variety of ways to preserve the 3-argument command. (Remember, adjacent strings are automatically concatenated):
mvn clean install --Dcucumber.options=--tags" ""${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags' '"${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags\ "${TEST_CASE}"
For consistency, set -x always shows a string that would need to be quoted using '...' and quoting the entire string, not just a portion of it.

Is the bash script not working? I'd say use single quotes for literal strings and double quotes for strings that are going to be expanded.
TEST_CASE='#Login'
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
What are the error messages?
I think problem is that the quotes aren't actually being included int he cucumber options. So Try this as well:
TEST_CASE='#Login'
CUCE_OPTS='"--tags '${TEST_CASE}'"'
mvn clean install --Dcucumber.options=${CUCE_OPTS}

Related

How to handle space in argument in MAVEN_OPTS from the command line?

I have some issues passing arguments to my mvn command through MAVEN_OPTS when there's a space, and specifically a javaagent parameters. I have also followed this recommendation to double quote the part after the semicolon but with no luck.
The command I am using is this:
$ export MAVEN_OPTS='-javaagent:"/Users/teras/Library/Application Support/CrossMobile/Plugins/cmxray.jar"'
$ echo $MAVEN_OPTS
-javaagent:"/Users/teras/Library/Application Support/CrossMobile/Plugins/cmxray.jar"
$ mvn -P desktop,run install -B -e
Error opening zip file or JAR manifest missing : "/Users/teras/Library/Application
Error occurred during initialization of VM
agent library failed to init: instrument
Unfortunately this doesn't seem to work. I am on OSX, using bash, if this helps.
Any idea what I am missing?
I'm afraid, you can't do that easily. If you look at the mvn script you can see it does not put $MAVEN_OPTS in quotes.
So unless there is a trick I'm not aware of, there is no way to escape the space.
You can fix that locally by editing your mvn script. If you used homebrew to install Maven you can find it in /usr/local/Cellar/maven/<VERSION>/libexec/bin/mvn. Make a copy of the script (so you can restore it if something goes wrong) and then find those lines at the end of the file (may be a bit different depending on the version):
exec "$JAVACMD" \
$MAVEN_OPTS \
$MAVEN_DEBUG_OPTS \
-classpath "${CLASSWORLDS_JAR}" \
"-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
"-Dmaven.home=${MAVEN_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$#"
and add quotes around the variables so it looks like this:
exec "$JAVACMD" \
"$MAVEN_OPTS" \
"$MAVEN_DEBUG_OPTS" \
-classpath "${CLASSWORLDS_JAR}" \
"-Dclassworlds.conf=${MAVEN_HOME}/bin/m2.conf" \
"-Dmaven.home=${MAVEN_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${CLASSWORLDS_LAUNCHER} "$#"
I am not sure if this is a Maven bug or it's this way on purpose. It looks like the issue was reported already but there seems to be some pushback from the maven team.
UPDATE
After reading through the comments of the above issue, I found this one which explains why this may not be a good solution after all:
If you quote $MAVEN_OPTS when used here, the java executable will see it as 1 parameter, not multiple ones, so if you have MAVEN_OPTS="-Xmx256m -Dparam=\"with space\"", then java will understand a Xmx value of 256m -Dparam="with space"...
If you don't quote it, then every space separated token (even if it was escaped when declaring MAVEN_OPTS) will be considered a separate argument, so -Dparam="with space" will be understood as trying to launch the space" main class with the -Dparam="with system property...

How to escape pipe character from PowerShell command line to pass into non-PowerShell command

I'm in a PowerShell console session, trying to run a Gradle script that exists in the cwd from the PowerShell command line.
The Gradle command accepts an argument which includes quotes that can contain one or more pipe characters, and I can't for the life of me figure out how to get PowerShell to escape it using just one line (or any number of lines, actually - but I'm not interested in multi-line solutions).
Here's the command:
./gradlew theTask -PmyProperty="thisThing|thatThing|theOtherThing"
...which produces this error:
'thatThing' is not recognized as an internal or external command, operable program or batch file.
I've tried all of these variants, none of which work. Any idea?
./gradlew theTask -PmyProperty="thisThing`|thatThing`|theOtherThing"
./gradlew theTask -PmyProperty=#"thisThing|thatThing|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\|thatThing\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\\|thatThing\\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Well, I have found that:
First call your script like this as I first suggested:
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Then modify your gradlew.bat script by adding quotes:
set CMD_LINE_ARGS="%*"
The problem is: now CMD_LINE_ARGS must be called within quotes or the same error will occur.
So I assume that your command line arguments cannot be something else and I'm handling each parameter one by one
rem now remove the quotes or there will be too much quotes
set ARG1="%1"
rem protect or the pipe situation will occur
set ARG1=%ARG1:""=%
set ARG2="%2"
set ARG2=%ARG2:""=%
set ARG3="%3"
set ARG3=%ARG3:""=%
echo %ARG1% %ARG2% %ARG3%
The output is (for my mockup command):
theTask -PmyProperty "thisThing|thatThing|theOtherThing"
The "=" has gone, because it has separated parameters from PowerShell. I suppose this won't be an issue if your command has standard argument parsing.
Add as many arguments as you want, but limit it to 9 anyway.
Found another possibility here. One can use single quotes and double quotes like this. Not sure if that helps to solve the original problem but it helped to solve mine.
'"thisThing|thatThing|theOtherThing"'

Cannot access mvn command from bash script

I want to run the following script as:
bash ./scripts/startapp.sh
Here is the script:
#!/bin/bash
str=$HOSTNAME
PREV_IFS=$IFS
IFS=. components=(${str})
ORIGINAL_DIRECTORY=`pwd`
REP_DIRECTORY=$ORIGINAL_DIRECTORY/src/main/resources
for part in "${components[#]}"
do
PATH=$REP_DIRECTORY/"$part"
REP_DIRECTORY=$PATH
done
IFS=$PREV_IFS
CONFIG_PATH=$REP_DIRECTORY/application.yaml
# Below is the final command I want to run from the terminal
`SPRING_CONFIG_LOCATION=$CONFIG_PATH mvn spring-boot:run`
I am getting
mvn: command not found
Without starting the script, I can use mvn spring-boot:run without any problem.
Make sure you have defined:
M2_HOME pointing to the base directory of your Maven installation
PATH must include $M2_HOME/bin
In your script you're overwriting the value of PATH on every iteration. You should change it to:
PATH=$PATH:$REP_DIRECTORY/"$part"

Escaping double quotes in exec java in Maven while executing through command line

I am trying to execute a Maven exec command as follows:
mvn exec:java -Dexec.args="{\"parameters\":[{\"name\":\"parameter1\",\"value\":\"1\"}]}"
The argument is a JSON string which I need to process in my Java program. But when the string is read by the Java class, the double quotes are removed and it reads as:
{parameters:[{name:parameter1,value:1}]}
I have tried different escape sequences like \\ and \\\, but it doesn't work. I need to preserve the double quotes in the string.
Could someone please help me resolve this?
Try this instead:
mvn exec:java -Dexec.args="'{\"parameters\":[{\"name\":\"parameter1\",\"value\":\"1\"}]}'"
try to use " instead " (double quotes)

Preserving argument spacing, etc when passing into mvn exec:java

I have a shell script that launches a Maven exec:java process -
exec mvn exec:java -Dexec.mainClass=... -Dexec.args="$*"
Now sadly if I run
./myMagicShellScript arg1 "arg 2"
the single string arg 2 doesn't make it through as a single argument as I'd like.
Any thoughts as to how to escape / pass things through properly (perferably in a clean way)?
I took a look at the mvn script and did some testing. This is what I came up with:
Try changing your script to look like this:
args=(${#// /\\ })
exec mvn exec:java -Dexec.mainClass=... -Dexec.args="${args[*]}"
That changes all spaces which are within each array element to be escaped with a backslash.

Resources