reading vm properties from a shell script XshowSettings - bash

I am trying to capture the output of java -XshowSettings | grep file.encoding but it is not working. I am trying to read a property of java -XshowSettings from a Unix shell script. Normally, it is easy to ready a property by using e.g. printenv | grep JAVA_HOME but in case of java -XshowSetting, grep does not work.
So, I want something like this java -XshowSetting | grep file.encoding, but it does not work. Any idea?

This will solve your problem
java -XshowSettings 2>&1 | grep file.encoding
You need to combine the standard output and standard error to capture the results. Read more here.

Related

How to get version from sbt-dynver on command line?

In order to generate dynamically the version in my sbt project I am using the sbt-dynver plugin. But in order to integrate the build system, I would like to obtain the version string from a bash script, something like:
DYNVER=`sbt dynver`
But the previous command does not return anything.
I managed to get what I wanted by adding the 'show' command to sbt and parsing the output value, as follows:
VERSION=`sbt "show dynver" | grep -oE "\w{7}-\w{8}-\w{4}"`
echo $VERSION
4bbbb2a-20171022-1508
The simplest solution is to ignore other SBT output by taking last line.
Also it's better to print out version instead of dynver as it affects overrides from your version.sbt file.
VERSION=$(sbt 'print version' | tail -n 1)
echo $VERSION
0.1.7-2-6853afe4

How do I get the full list of files bash sourced at startup?

I am trying to figure out where some problematic environment variables are coming from. I suspect that some of the system installed bash initialization scripts have been tampered with.
How can you get the full list of files that are getting sourced by bash as it starts?
I have spent some quality time with stuff like:
$ strace -ofoo bash
$ grep stat foo | grep -v ENOENT | sort | uniq
but that is some last-resort unix hackery, and I still haven't nailed it.
You can put set -x in your startup file where is likely that the variable comes from, for example .profile or .bashrc or a more drastic /etc/profile
This enable a trace of all the action done to the standand error.
Things can get very verbose, so you can redirect stderr to a file with:
exec 2>>/path/file.log

Logcat filter by application commandline w/o tags

I've been searching and never found an answer that works for me.
Specifically, I want to see logcat outputs from a specific application.
I'm not using eclipse, I'm using command line adb logcat.
I'm only given the apk build of the application, so I don't know any tags to use the filter by tags everyone has been talking about. I don't think I can just go in and add tags or what not.
I saw in another page someone suggested
"Use ps/grep/cut to grab the PID, then grep for logcat entries with that PID. Here's the command I use:"
adb logcat | grep `adb shell ps | grep com.example.package | cut -c10-15`
This doesn't work for me either.
Maybe you are using this in windows?

Unix cat and tail for output of java program

I have a java program that takes time to execute and I have to make 10 runs of it and I am interested in only last 5 lines of the output, the actual output runs in hundreds of lines. Since I want to average the output I want tail -5 for run into a file. Also one of the parameters (--random) in my arguments keep changing in each run.
I am doing the following:
for i in {1..10} ; do cat output| tail -5 | java -cp src.Tagger.java --random $1; done
Sorry I am really bad at bash.
You should make sure you can execute your java program at all first. You can't execute a .java file directly, it has to be compiled.
If you have the file src/Tagger.java, you can try to compile it with
javac -cp src src/Tagger.java
but if it requires other libraries or build systems, it might be completely different. If you downloaded this app, see the project documentation.
This should silently produce a src/Tagger.class. Once you have this, you can try to run it with
java -cp src Tagger --random 1234
though again, if it has dependencies on libraries, it'll be different.
If that works, you can finally start trying to run it in a loop:
for i in {1..10}
do
cat output| tail -5 | java -cp src Tagger --random 1234
done
You want the output of your java program to go to output first, then you need to tail the file. It looks like you are currently feeding output into your java program as input. I don't think that is what you want. Try this instead:
for i in {1..10}
do
java -cp src.Tagger.java --random $1 > output;
tail -5 output;
done
I also have my doubts that you have the java command correct. You shouldn't specify .java for the file name when running the java file. It needs to run from the compiled .class file and the java command assumes .class, so it isn't needed on the command line. You also are using -cp (classpath) but don't appear to be giving it an argument. I'd expect the java command to be more like:
java -cp classesdir com.mydomain.myapp.Tagger

Bash script store command output into variable

I have a problem concerning storing the output of a command inside a variable within a bash script.
I know in general there are two ways to do this
either
foo=$(bar)
# or
foo=`bar`
but for the Java version query, this doesn't seem to work.
I did:
version=$(java --version)
This doesn't store the value inside the var. It even still prints it, which really shouldn't be the case.
I also tried redirecting output to a file but this also fails.
version=$(java -version 2>&1)
The version param only takes one dash, and if you redirect stderr, which is, where the message is written to, you'll get the desired result.
As a sidenote, using two dashes is an inofficial standard on Unix like systems, but since Java tries to be almost identical over different platforms, it violates the Unix/Linux-expectations and behaves the same in this regard as on windows, and as I suspect, on Mac OS.
That is because java -version writes to stderr and not stdout. You should use:
version=$(java -version 2>&1)
In order to redirect stderr to stdout.
You can see it by running the following 2 commands:
java -version > /dev/null
java -version 2> /dev/null

Resources