Question

Can anyone tell me how to write the outputs of an application to mysql using bash script? for example a java application with the following code

class HelloWorld {
public static void main(String[] args) {
    System.out.println("Hello World!");
    System.out.println("Message 1!");
    System.out.println("Message 2!");
    System.out.println("Message 3!");
}

}

i want to produce 4 rows in table for every System.out

i tried that script

app_name="HelloWorld"
app_output=$(java $app_name);
mysql -u<username> -p'<password>' <db_name> <<_EOF_
INSERT INTO logs_tbl (app_name,text) VALUES ("$app_name","$app_output");
_EOF_

but the problem is that it produce one row when the app exit with value: Hello World!Message 1!Message 2!Message 3!

Was it helpful?

Solution

Try the following:

#!/bin/bash

app_name="HelloWorld"
java $app_name | while read line
do
        echo "INSERT INTO logs_tbl (app_name,text) VALUES ('$app_name','$line');" | mysql -u <username> -p '<password>' <db_name>
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top