0

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.

Jishan
  • 1,654
  • 4
  • 28
  • 62
  • 1
    The frist result from https://www.google.co.nz/search?q=bash+read+words+from+text+file is http://stackoverflow.com/questions/10931915/how-can-i-read-words-instead-of-lines-from-a-file and the second is http://stackoverflow.com/questions/7719628/how-to-read-word-by-word-from-a-file-in-a-for-loop Did neither of them help you? – Jerry Jeremiah Jun 20 '16 at 22:21
  • @JerryJeremiah This is my first bash code kind of, I will try working on them. Sorry if the question is very naive. – Jishan Jun 20 '16 at 22:23
  • I didn't meant that. I just meant that if they didn't work if would be helpful to know why so that my answer wouldn't tell you something you had already tried. – Jerry Jeremiah Jun 20 '16 at 22:24
  • @JerryJeremiah I read it, but my low experience really comes in the way of an workable implementation. Thank you so much for an answer. – Jishan Jun 20 '16 at 22:25

1 Answers1

1

Idea for the loop taken from https://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.

Community
  • 1
  • 1
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32