jeet jeet - 5 months ago 21
Bash Question

Run Jar Using Shell Script

So I have a Java file that needs this following string to run.

java --classpath=$CLASSPATH:/path/to/jradius-client.jar net.sourceforge.jradiusclient.TestRadiusClient hostname authport acctport shared-secret username password


What I need to do is to write a script that uses space separated words from a certain
textFile.txt
and use each of the words in place of
shared-secret
. If it's a success, print the word.

What I have till now is:

#!/bin/bash

java --classpath=$CLASSPATH:/path/to/jradius-client.jar net.sourceforge.jradiusclient.TestRadiusClient "val" 1812 1813 shared-secret username password

exit $?


What I want is something like this (in Java like pseudo-code, I have no bash experience :( ):

String s = Words From Text File As String
String[] words = s.split("\\W+");
for(word : words){
try and run The JAva .jar File With Params X, Y, word, Z
success? print word
}


How can I achieve this?
Thanks.

Answer

Idea for the loop taken from http://stackoverflow.com/a/7721320/2193968

for word in $(<textFile.txt)
do
    java --classpath=$CLASSPATH:/path/to/jradius-client.jar net.sourceforge.jradiusclient.TestRadiusClient hostname authport acctport $word username password
done

This will read every word from every line in textfile.txt and put it into word and then run the command substituting $word with the value read from the file.