Accessing multiple outputs of a Jar in a shell script - java

I am invoking a Jar(say abc.jar) which returns 3 outputs(which I have given as print statements in jar itself,say outputs from jar are
Emp name,Emp id and Email
and I was able to get these 3 outputs in the script in a single Variable which is not much of use.
I want to capture these outputs in 3 different variables to use them further.
Is there a way possible to store the different outputs of a jar into different variables in .sh?
details=$(java -jar /mydata/abc.jar)
echo $details
Output
Kevin 1234 abc#gmail.com

Yes, it's possible, you just need to evaluate the result and assign each value a variable.
$ details=$(java -jar /mydata/abc.jar)
$ r=`echo $details |gawk '{print "a="$1,"b="$2,"c="$3}'`
$ eval $r
# variable $a $b $c will store your value respectively
$ echo $a $b $c
Kevin 1234 abc#gmail.com

Related

What will be the Regular Expression to parse give output

I want to extract the code count for the below command output. In the below example expected output is 286. What will be the regular expression to extract the code count?
Want to parse the below string in windows:
1 text file.
1 unique file.
0 files ignored.
http://cloc.sourceforge.net v 1.64 T=0.01 s (86.1 files/s, 1119.4 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C++ 1 0 0 13
-------------------------------------------------------------------------------
You can grep the last digits of a line by
grep -P "\d+$"
I can parse it in Linux using below:
grep -Eo '[0-9]+$'

MongoExport a collection

I have a collection called User(with userId as primary key) and am trying to export certain documents based on the Query attribute supported by MonogExport.
My MongoExport command looks like :
mongoexport -h localhost -d mydb -o C:\account\user.json --collection user--query '{userId: {$in :[1233,1234,1235,1236]}}'
Based on the above, mongo creates a json file for us. But my major concern is, how to process this command, if length of this command increases to more than 8192 chracters(Message Displayed : the process command is too long)
This is not the problem of mongo. Windows command line only takes 8191 characters per line.
Refer here for the solution

Sort records but keeping header at the top in Linux and Java

I need to sort records from a file by first column numerically but I need the header to stay at the top of the file.
I am using Java's Process Builder but I am not familiar with Linux commands so I am doing something wrong. This is what I need help with:
Process sort = new ProcessBuilder("/bin/bash", "-c", "((head -n -1 " +main_file+ " | tail -n -1) | sort -n) >> " + main_file).start();
You dont need java to execute a shell and perform the task, directly run the commands on shell .
See below
$ cat delitLater.txt
A 1
B 2
C 3
A 4
B 5
C 6
$ awk 'NR==1; NR > 1 {print $0 | "sort -n -k 1,1"}' delitLater.txt
A 1
A 4
B 2
B 5
C 3
C 6
$ awk 'NR==1; NR > 1 {print $0 | "sort -n -k 2,2"}' delitLater.txt
A 1
B 2
C 3
A 4
B 5
C 6
Using awk , you print the first line as is. This is done using NR(Rownumber) == 1 . For all other rows , you use the sort command and specify which columns to use . Sort key is defined by "-k" option . "-n" means numeric sort , but you might or might not need it depending upon the contents of your file.

How to pass all kind of SQL-Queries via Command Line/Batch Args[] into Java?

I want to write a little .jar which is used as a "translator" for SQL-Queries directed to a z/OS-DB2-Database.
My goal is that the application accepts SQL-Queries as Command Line Arguments manually or via shell script/cron, next to other parameters like IP, Port, User etc.
Is there a way to leave those arguments unaffected while passing them to the jar?
Example:
java -jar db2sql.jar SQL=={SELECT * FROM TABLE1 TAB1, TABLE2 TAB2 WHERE TAB1.XYZ = TAB2.ZYX AND TAB2.ABC LIKE 'blabla' AND TAB1.DATE >= '01.01.2015'} IP=={192.168.0.1} User=={Santa} Password=={CLAUS}
(please ignore that this statement is senseless, but i hope you get the trick)
My Problem is reading out that Command Line parameters, mostly special characters like * , " ' etc.
Questions:
Is there a list of all possible SQL-Parameters which must be escaped?
Is there a special character which can be used as delimiter that will never occur in an SQL-Query?
Is it possible to pass all kind of SQL Statments as ONE argument?
Is it possible to leave special characters unhandled, e.g. Argument "" = String "", and not .classpath etc. ?
Kind Regards
Although I wouldn't recommend what you're trying to do for several reasons, at least in a *NIX environment you could just use the standard way.
java -jar foo.jar -s "SELECT * FROM SOMETHING WHERE foo = 2" -u username -h hostname
You can use additional libraries to parse the parameters, but this way you would use -s to specify the SQL query, and wrap the param value in " to make it a single argument with automatic escape.
In your main method you can then get the full query with (simplified)
if(args[0].equals("-s"))
sqlString = args[1];

Getting Java Println Outputs in PHP

I'm trying to get the output from a java application that would use the below code to output values
System.out.println(object.getNumber(3));
System.out.println(object.getNumber(4));
I'm using exec("somejavapath javaName", $output) and print_r($output) to get that output array to print.
I know it will get the values but I wanted to get into a certain format
So instead of
Array
(
[0] => 34
)
I want something like this
Array
(
[0] => 3
[1] => 4
)
Does anyone know what I could do to get this format?
Thanks
If you are not expecting comas in the output from java application , include a coma between the two values :
System.out.println(object.getNumber(3));
System.out.println(","); // Print a coma in between
System.out.println(object.getNumber(4));
Then
$values_array = explode( ',', $output );
You can use explode to break the content up using a delimiter, in this case, a line-break (\n).
$var = explode('\n', $output); // split output by line breaks

Categories