Jar file escapes "--" from substituted variable - Bash - java

Code:
#!/bin/bash
MyVariable="--option arg1,arg2"
echo Variable output : $MyVariable
java -jar HelloInternet.jar "$MyVariable"
Expected results:
The jar file should recognize and use the value stored in variable.
Actual results:
The jar file escapes "--" from "--option arg1,arg2" , and interprets the variable without the "--" .
Include any error messages:
Exception in thread "main" joptsimple.UnrecognizedOptionException: option arg1,arg2 is not a recognized option
Describe what you have tried:
I tried using ' ' instead of " " and vice versa without success.

Use an array when you need an array:
MyVariable=(--option arg1,arg2)
java -jar HelloInternet.jar "${MyVariable[#]}"

Related

How can I use a regular expression to remove the file extension in zhell?

I am trying to simplify my command line java using this zshell script where $1 holds the file name, in this case Hello.java.
# jcr stands for java compile and run
jcr() {
javac $1 # $1 contains the first argument Hello.java
java Hello # I want to replace Hello with a
# regular expression or similar which extracts Hello
}
I need a good reference or cheat sheet for zshell and zhell regular expressions.
You can use a simple string substitution: ${1//.java} This will remove the last occurence of .java from the parameter $1.

springboot cmd args contains special character

java -jar xx.jar --spring.datasource.password=root like this, if it don't contains special character, it is ok.
if java -jar --spring.datasource.password='root!00' , if contains special character !, it didn't work. and I tried \"root!00\" \'root\!00\' \"root\!00\" and other ways , all field.
java -jar --spring.datasource.password='root\!00'

IP as Linux array element throws UnknownHostException but as constant works

I have the following script in the directory /home/test/javacall that parses csv of IP pair , invokes a sh file that calls an executable jar to get output from these IPs.
In the below code ip1=${IPArray[0]} throws UnknownHostException from java.
But If I use the ip directly ip1="10.10.10.10" java code works fine. I did System.out.println from java and I got the same IP displayed in both cases. But in the case of ip1=${IPArray[0]} only, I get the exception.
#!/bin/bash
INPUT="IPPairs.csv"
array=()
while IFS="," read var1 var2 ; do
echo $var1 $var2
pairString="$var1***$var2"
array+=("$pairString")
done < $INPUT
for i in "${array[#]}" ; do
echo $i
IPString=$(echo $i | tr '***' ' ')
read -ra IPArray <<< "$IPString"
ip1=${IPArray[0]}
#ip1="10.10.10.10"
ip2=${IPArray[1]}
source /home/test/javacall/javacmd.sh "$ip1" "/home/test/javacall/out.txt" "show running-config all-properties"
done
Exception:
com.jcraft.jsch.JSchException: java.net.UnknownHostException: 10.10.10.10
at com.jcraft.jsch.Util.createSocket(Util.java:349)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
That string (357\273\277) indicates that your csv file is encoded with a Byte-Order Mark (BOM) at the front of the file. The read command is not interpreting the BOM as having special meaning, just passing on the raw characters, so you see them as part of your output.
Since you didn't indicate how your source file is generated, you may be able to adjust the settings on that end to prevent writing the BOM, which is optional in many cases. Alternatively, you can work around it various ways on the script side. These questions both offer some examples:
How can I remove the BOM from a UTF-8 file?
Cygwin command not found bad characters found in .bashrc 357\273\277
But honestly, if you just follow Charles Duffy's advice and run your file through dos2unix before parsing it, it should clean this up for you automatically. i.e.:
...
array=()
dos2unix $INPUT
while IFS="," read var1 var2 ; do
...
Or, building on Charles' version:
#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac
INPUT="IPPairs.csv"
declare -A pairs=( )
dos2unix $INPUT
while IFS=$',\r' read -r var1 var2 _ ; do
pairs[$var1]=$var2
done <"$INPUT"
for ip1 in "${!pairs[#]}"; do
ip2=${pairs[$ip1]}
# Using printf %q causes nonprintable characters to be visibly shown
printf 'Processing pair: %q and %q\n' "$ip1" "$ip2" >&2
done
Do note that running dos2unix in your script is not necessarily the best approach, as the file only needs to be converted once. Generally speaking, it shouldn't hurt anything, especially with such a small file. Nonetheless, a better approach would be to run dos2unix as part of whatever process pushes your csv to the server, and keep it out of this script.
System.out.println() only shows visible characters.
If your input file contains DOS newlines, System.out.println() won't show them, but they'll still be present in your command line, and parsed as part of the IP address to connect to, causing an UnknownHostException. Converting it to a UNIX text file, as with dos2unix, or using :set fileformat=unix in vim, is typically the quickest way to fix this.
BTW, if you don't need ordering retained, an associative array is typically a more appropriate data structure to use to store pairs:
#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac
declare -A pairs=( )
while IFS=$',\r' read -r var1 var2 _ ; do
pairs[$var1]=$var2
done <"$input"
for ip1 in "${!pairs[#]}"; do
ip2=${pairs[$ip1]}
# Using printf %q causes nonprintable characters to be visibly shown
printf 'Processing pair: %q and %q\n' "$ip1" "$ip2" >&2
done
In the above, using IFS=$',\r' prevents LF characters (from the "CRLF" sequence that makes up a DOS newline) from becoming either part of var1 or var2. (Adding an _ placeholder variable to consume any additional content in a given line of the file adds extra insurance towards this point).

FileWriter issue with \t

I am trying to write a string to a batch file using Java Filewriter as following
fw.write("echo Default is: %SDK_ROOT%\\tools\\android-ndk-r10d\r\n");
It gives me the output as :
echo Default is: %HEXAGON_SDK_ROOT% ools\android-ndk-r10d
Its interpreting \tools as a tab space and ools. how to suppress the actual meaning of \t here ?
My desired output should be
echo Default is: %HEXAGON_SDK_ROOT%\tools\android-ndk-r10d
what can I do here ?
Add \ in front of each of your backslashes. They need to be duplicated inside string literals.

How to pass parameters to a jar file and read the console output in ruby?

I'm trying to execute a .jar file from ruby and read the output. The following solution works when my .jar file does not expect any parameters to be passed. But when I try to do this, I get an error -
pipe = IO.popen( [ '/path/to/java', '-jar', "/path/to/jarfile.jar #{argsA} #{argsB}", {SDTERR=>STDOUT} ]
pipe.close_write
puts pipe.readlines
I need to pass a couple of parameters to the .jar file. How can I acheive this? I've tried several ways to do this but wasn't successful. This is the error I get, where 'Hello' and 'World' are the arguments I'm passing -
Error: Unable to access jarfile jarfile.jar Hello World
Useful links:
How to get java output with ruby
call jar file from ruby class
Put argsA and argsB in their own parameter, otherwise when seeing spaces in the parameters, ruby will surround them with quotes, so they're considered like one.
pipe = IO.popen( [ '/path/to/java', '-jar', '/path/to/jarfile.jar',"#{argsA}", "#{argsB}", {SDTERR=>STDOUT} ]

Categories