Bash script pass arguments to maven - bash

I have a bash script called runMain that is shown below:
#!/bin/bash
mvn exec:java -Dexec.mainClass=org.fjx.main.Main -f fjxRepo/pom.xml -Dexec.args="$#"
So I want to pass the arguments of the bash script to the maven exec. Even though this works when there is only one argument, it does not work for more than one argument e.g. 2 arguments.
Please help me fix this.

If you want to pass the whole argument string and none of the arguments have whitespace in them, you can use $* instead of $#.

Related

How to pass a parameter which includes a single quote in it from Jenkins to PowerShell? Unterminated quoted string

In one of the stages in a Jenkins pipeline, I need to pass the values from parameters to a script. Sometimes these values may contain a single quote sign which disrupts the run and doesn't come through to the script.
sh "pwsh -file script.ps1 '${params.NAME}' '${params.DESCRIPTION}' '${params.SOMETHING}'"
Ie when the script is called in the stage, the passed parameters may look like this:
'Name' 'Somebody's description' 'Something'
As you can see it messes with the arguments being passed on. It produces the error of an unterminated quoted string. I need to pass only 3 args to the script as the params suggest.
Any ways to solve this? Should I add another stage before calling the script that formats the values of the params in a way that is acceptable to pass to the PowerShell script? If so, how should I format it? Is there any other way to solve it?
bash's builtin printf has a %q format string that will insert quotes for you. You might try something like:
sh -c "$(printf "%q " pwsh -file script.ps1 "${NAME}" "${DESCRIPTION}" "${SOMETHING}")"
but I'm not sure at which point the params gets expanded. This sort of thing is inherently fragile, though, and you should find a better way.

Convert a list of bash arguments to a quoted single string

I'm trying to create a simple bash script to run a mvn command with some specific arguments and "sub" arguments, using the exec plugin. Seems simple, but I'm having problems to achieve the desired effect:
here is the command to run:
mvn exec:java -Dexec.args="-a val1 -b val2 -c val3"
it works fine when run like this, but I want to wrap it in a bash script, easy to run for everyone:
myapp -a val1 -b val2 -c val3
The script would be as easy as:
mvn exec:java -Dexec.args="$#"
But "$#" doesn't make it a single quoted value, so I tried to quote it:
mvn exec:java -Dexec.args=\""$#"\"
I also tried a second script I called quoter:
./myapp \""$#"\"
This doesn't work either, after a good combination playing time around, it feels like no matter how many times I quote it, it still treats it as a list of arguments, being the first element "-a, then the second element val1, then third -b, and so on.
curiously, if I don't quote the arguments in the quoter script, and run it like this:
./quoter "-a val1 -b val2 -c val3"
It works fine, it is passed as a single parameter to the second script and then to the first script executing the maven command.
It seems like, "$#" is not quite a string made of the stringficated concatenation of all the parameters (which is cool if you think about it), nor echoing it seems to convert it into one (not cool). Is there a way to make this possible?
PS: yes I'm trying to pass options to the main program, which is a heavily dependent spring standalone application.
man bash...
Special Parameters
The shell treats several parameters specially. These parameters may only be
referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When the expansion
occurs within double quotes, it expands to a single word with the value of
each parameter separated by the first character of the IFS special variable.
That is, "$*" is equivalent to "$1c$2c...", where c is the first character
of the value of the IFS variable. If IFS is unset, the parameters are
separated by spaces. If IFS is null, the parameters are joined without
intervening separators.
Ok, after some more hours I found a solution, here it is. I created whole new script so I'll start from the scratch.
Create a simple script that the final user will execute and pass the parameters as a normal command-line app:
$./myapp -a val1 -b val2 -c val3
which will contain the following:
mvn exec:java -Dexec.args="$*"
The difference between $* and $#, is that $# will pass the arguments as a list and will also do some formatting to those values, like removing quotes and so. In the other hand, $* will treat all the arguments as a single one, grouping them and letting you pass them altogether as a string value to the inner command.

Run a bash including a variable in the "bash XXX.sh" command

I am completely new to "programming" in Linux, and I wonder if it is possible to include the definition of a variable when I run a bash file.
My bash file needs the variable in order to go from one or another path, so I would like to be able to include it when running the script.
Something like this:
bash MYFILE.sh -VARIABLE
So the -VARIABLE would be used in the script.
Thank you!
You can take advantage of shell parameter expansion to smoothly read variables from the environment of the parent process, if it's that what you want to achieve.
Look at the following script named test.sh:
#!/bin/bash
VARIABLE=${VARIABLE:="default value"}
echo $VARIABLE
If you start it with the line
$ ./test.sh
it outputs
default value
But if you invoke test.sh with the line
$ VARIABLE="custom Value" ./test.sh
it outputs
custom value
But make sure that the variable assignment is at the beginning of the line. Otherwise it is passed to test.sh as command line argument.
The used form of parameter expansion ${parameter:=word} is described in the bash reference manual as:
If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

passing command line arguments to a shell script doesn't work

I want to write a script that will change to different directories depending on my input. something like this:
test.sh:
#!/bin/bash
ssh machine001 '(chdir ~/dev$1; pwd)'
But as I run ./test.sh 2 it still goes to ~/dev. It seems that my argument gets ignored. Am I doing anything very stupid here?
Bash ignores any variable syntax inside the single-quoted(') strings. You need double quotes(") in order to make a substitution:
#!/bin/bash
ssh machine001 "(chdir ~/dev$1; pwd)"
The parameter is enclosed in single quotes, so it isn't expanded on the local side. Use double-quotes instead.
#!/bin/bash
ssh machine001 "chdir ~/dev$1; pwd"
There's no need for the (...), since you are only running the pair of commands then exiting.

bash shell script conditional assignment

I'm writing a bash shell script. There's a required first argument and I want to have an optional second argument.
If the second argument is omitted I want it to use the value of the first argument.
Currently I have:
SOMEVAR=${2:-Untitled}
How can I use something like basename $1 instead of Untitled?
You can just do something like SOMEVAR=${2:-$(basename "$1")}. You can do any shell or variable in the optional part.
Just use command substitution: $(basename $1), literally instead of Untitled.
However, bash also has the ability to do this without an external process: ${1##*/}
SOMEVAR=${2:-${1##*/}}

Resources