KSH starts Java with arguments in a variable

I need to run Java from a KSH script after getting a list of files in a directory as arguments to run this Java class.

cd /batch/App/ find /batch/files/ -type f -print -name "*.xls" >> $list_of_files /usr/java14/bin/java ConvertApp $list_of_files 

in which I think it will function as /usr/java14/bin/java ConvertApp test1.xls test2.xls test3.xls

But it seems that the argument passed to Java was not successful. Does anyone have an idea? Thanks.

+4
source share
4 answers

You can see xargs .

try it

 find /batch/files/ -type f -print -name "*.xls" | xargs /usr/java14/bin/java ConvertApp 
+1
source

find ... -print0 | xargs -0 ... find ... -print0 | xargs -0 ... is the way to go.

Answers recommending cat , or the variable will not work if there are spaces in the file names. If you do not want to use xargs , you can get a better chance of success if you use an array:

 saveIFS=$IFS IFS=$'\n' files=($(find...)) IFS=$saveIFS java ... "${files[@]}" 
+1
source

I think you have two problems.

  • find outputs match one in a line, you need to convert this so that they are limited to a space, possibly with sed or awk.

  • You should probably put the output in a variable using

    list_of_files = 'find ........'

then call java with

 java ConvertApp ${list_of_files} 
0
source

You add the .xls files found by the find to the $list_of_files file.

Next, you want to send the contents of this file as a command line argument to your java program. To do this, you can use command substitution like:

 /usr/java14/bin/java ConvertApp "$(cat $list_of_files)" 

or

 /usr/java14/bin/java ConvertApp "`cat $list_of_files`" 
0
source

All Articles