Using Bash script to run multiple files through java code - java

I have some code that is used to analyse files, the code is setup to analyse 1 file at a time using the following command line input in the /home/john/Dropbox/PhD/MultiFOLDIA/ directory:
java MultiFOLDIA_IMODE1 complex.1.pdb /home/john/Dropbox/PhD/MultiFOLDIA/Poses/ T0868_T0869 /home/john/Dropbox/PhD/MultiFOLDIA/T0868_T0869_complex.1.pdb_IMODE1.txt > /home/john/Dropbox/PhD/MultiFOLDIA/MultiFOLDIA_IMODE1.log
I would like to run the command on every file in the /home/john/Dropbox/PhD/MultiFOLDIA/Poses/ directory and have tried using the following script:
#!/bin/bash
poses=(~/home/john/Dropbox/PhD/MultiFOLDIA/Poses/*)
for f in "${poses[#]}"; do
java MultiFOLDIA_IMODE1 "$f" /home/john/Dropbox/PhD/MultiFOLDIA/Poses/ T0868_T0869 /home/john/Dropbox/PhD/MultiFOLDIA/T0868_T0869_"$f"_IMODE1.txt > /home/john/Dropbox/PhD/MultiFOLDIA/MultiFOLDIA_IMODE1.log
done
It doesn't work and I think I am not understanding how to pull filenames from arrays and utilise them in this way.

~/ is already /home/john.
So ~/home/john probably doesn't exist.
This should bring you closer to your goal :
cd /home/john/Dropbox/PhD/MultiFOLDIA/Poses/
for pdb in *.pdb
do
echo "Processing $pdb"
java MultiFOLDIA_IMODE1 "$pdb" ./ T0868_T0869 ../T0868_T0869_"$pdb"_IMODE1.txt >> ../MultiFOLDIA_IMODE1.log
done

This should work.
find /home/john/Dropbox/PhD/MultiFOLDIA/Poses/ -maxdepth 1 -type f -exec java MultiFOLDIA_IMODE1 '{}' /home/john/Dropbox/PhD/MultiFOLDIA/Poses/ T0868_T0869 /home/john/Dropbox/PhD/MultiFOLDIA/T0868_T0869_'{}'_IMODE1.txt >> /home/john/Dropbox/PhD/MultiFOLDIA/MultiFOLDIA_IMODE1.log \;
Also, when redirecting output use >> instead of >. > truncate file and in the end you will have only logs from last execution
eg:
$ echo a > test.txt
$ echo a > test.txt
$ cat test.txt
a
$ echo a >> test.txt
$ echo a >> test.txt
$ cat test.txt
a
a

Related

can't make a shell script to make a jar file

I try to make a simple shell script to make a jar file. The jar command combined with -C does not work with wildcards. Therefor I use a wildcard to find the files I want. Write them to a file, and loop over them.
It looks something like this:
the_classes=''
cd "$bin_folder"
tmp_dir=$(mktemp -d -t java_sucks)
find "imui/core/" -type f -name "IMUI_Widget_Agent*.class" >"$tmp_dir/classes.txt"
while IFS="" read -r p || [ -n "$p" ]
do
the_classes="${the_classes} -C '$bin_folder' '$p'"
done < "$tmp_dir/classes.txt"
Using the above I complete the command:
cmd='jar cfm build/IMUI_Widget_Agent.jar'
cmd="${cmd} \"$bin_folder/imui/core/IMUI_Widget_Agent_MANIFEST.MF\" $the_classes"
printf "\n\n\ncmd\n\n\n"
echo $cmd
Now if I copy and paste this command to execute it works!
But I want to avoid the manual labour of doing the copy and paste by hand every time.
Now I have:
eval "$("$cmd")"
But I get an error File name too long. No matter what I try, every fix I do creates a new problem. I have been working 6 hours now to make this script.
What would be a good step forward?
Since you cd "$bin_folder" you don't actually need -C "$bin_folder":
#!/bin/bash
shopt -s globstar
cd "$bin_folder"
jar cfm build/IMUI_Widget_Agent.jar \
imui/core/IMUI_Widget_Agent_MANIFEST.MF \
imui/core/**/IMUI_Widget_Agent*.class
However, if you still want to add them as part of a larger script, you can easily and robustly build your command in an array:
#!/bin/bash
shopt -s globstar
cmd=(jar cfm build/IMUI_Widget_Agent.jar imui/core/IMUI_Widget_Agent_MANIFEST.MF)
cd "$bin_folder"
for file in imui/core/**/IMUI_Widget_Agent*.class
do
cmd+=(-C "$bin_folder" "$file")
done
echo "About to execute: "
printf "%q " "${cmd[#]}"
echo
"${cmd[#]}"
Alternatively, you can simply do eval "$cmd" with your code, which is equivalent to echo and copy-pasting. However, be aware that this is fragile and error prone because it requires careful escaping of the filenames which you're not currently doing.

gettext under windows 7 for java project returns 'Bad file descriptor'

I'm trying to use gettext in java project to provide translations for different languages.
My workstation runs Windows 7 and so I'd like to be able to go through whole build process on it (including keys generation and translations update - all from gradle script).
However msgfmt tool returns an error: 'Bad file descriptor' without any additional information...
I was not able to find any helpful information over the Internet.
Someone had similar problem and guy named Daiki Ueno proposed a patch but I guess it didn't go to official gettext versions since the issue is still present...
See: https://lists.gnu.org/archive/html/bug-gnulib/2013-09/msg00049.html
Steps
create list of java files in the project
dir *.java /s/b > files.txt
create translations directory
mkdir po
creates a pot file that contains all strings in the native language
xgettext -ktrc -ktr -kmarktr -ktrn:1,2 --from-code=utf-8 -o po\keys.pot -f files.txt
replace CHARSET in keys.pot file to correct encoding name 'utf-8'
...manual step...
create translations file with content description
type nul >> po\pl.po & copy po\pl.po +,,
echo msgid "" >> po\pl.po
echo msgstr "" >> po\pl.po
echo "Content-Type: text/plain; charset=UTF-8\n\" >> po\pl.po
merge keys into localized po file
msgmerge -U po\pl.po po\keys.pot
create default ResourceBundle class file << fails with error (see below)
set JAVAC=c:\Java\jdk1.8.0_40\bin\javac.exe
set TMPDIR=c:\temp
msgfmt --verbose -java2 -d src\main\java -r com.haso.Messages po\keys.pot
I've tried following implementations of gettext:
GetGnuWin32-0.6.3
MinGW-0.18.3
gettext-tool-windows-0.18.3
gettext-tool-windows-0.19.4
gettext-iconv-windows-0.19.4
first one crashes in Windows 7 64bit
msgfmt from the rest give the following output:
>msgfmt: c:\Java\jdk1.8.0_40\bin\javac.exe subprocess failed: Bad file descriptor
Could anyone help with getting it work?
I was able to find some workaround to my problem since msgfmt downloaded from here generated java sources...
This solution requires following environmental variables set correctly:
JAVA_HOME
CYGWIN_HOME
GETTEXT_HOME
I created following script run_gettext.sh in subdirectory named gettext in my project root dir:
#!/bin/bash
cd `$CYGWIN_HOME/bin/pwd.exe`/gettext
set CYGWIN_HOME=%CYGWIN_HOME%
set GETTEXT_HOME=%GETTEXT_HOME%
$CYGWIN_HOME/bin/rm.exe -fv files.txt
$CYGWIN_HOME/bin/find.exe ../src/main/java -name *.java > files.txt
$CYGWIN_HOME/bin/mkdir.exe -pv po
$GETTEXT_HOME/xgettext.exe -ktrc -ktr -kmrktr -ktrn:1,2 --from-code=UTF-8 -o po/keys.pot -f files.txt
$CYGWIN_HOME/bin/sed.exe -i 's/CHARSET/UTF-8/g' po/keys.pot
if [ ! -f po/pl.po ]; then
$CYGWIN_HOME/bin/cp.exe -v template.po po/pl.po
fi
$GETTEXT_HOME/msgmerge.exe -U po/pl.po po/keys.pot
export JAVAC=`$CYGWIN_HOME/bin/pwd.exe`/javac.sh
$CYGWIN_HOME/bin/rm.exe -rfv tmp
$CYGWIN_HOME/bin/mkdir.exe -pv tmp
export TMPDIR=`$CYGWIN_HOME/bin/pwd.exe`/tmp/
$GETTEXT_HOME/msgfmt.exe --verbose --java2 -d tmp -r com.haso.i18n.Messages po/keys.pot
$GETTEXT_HOME/msgfmt.exe --verbose --java2 -d tmp -r com.haso.i18n.Messages -l pl po/pl.po
$CYGWIN_HOME/bin/rm.exe -rfv ../src/main/java/com/haso/i18n
$CYGWIN_HOME/bin/mkdir.exe -pv ../src/main/java/com/haso/i18n
$CYGWIN_HOME/bin/find.exe tmp -name "Messages*.java" | $CYGWIN_HOME/bin/xargs.exe -n 1 $CYGWIN_HOME/bin/cp.exe -vt ../src/main/java/com/haso/i18n/
and executed it from my project root directory with command:
%CYGWIN_HOME%\bin\bash gettext/run_gettext.sh
where javac.sh is:
#!/bin/bash
#
# A wrapper for calling Javac from Cygwin
# Original Author: Igor Pechtchanski <pechtcha#cs.nyu.edu
# Modified by Florian Gattung <fgattung#gmail.com> for msgfmt compatibility
#
ME="`basename $0`"
JAVAC_EXEC="/cygdrive/%JAVA_HOME%/bin/javac.exe"
ARGS=""
#
while [ -n "$1" ]; do
arg="$1"
shift
if [ -d $arg ]; then
arg="`cygpath -p -w "$arg"`"
fi
if [ -f $arg ]; then
arg="`cygpath -p -w "$arg"`"
fi
ARGS="$ARGS '$arg'"
done
eval "set -- $ARGS"
exec "$JAVAC_EXEC" "$#"
Created *.java files are then compiled by gradle on CI together with project source code.
File template.po contains:
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"

Setting Java CLASSPATH doesn't work but -cp does

So, here's the problem I encountered. I wrote a simple .bat file to run weka on some data sets I had but Java recently updated itself and it stopped working. My old code was this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
This worked before and it did the job I asked of it. However, after the java update, I kept getting the error "Could not find or load main class weka.classifiers.meta.FilteredClassifier". I couldn't figure it out because the directory names and class names were exactly correct. So, I changed the code to this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java -cp "C:\Program Files (x86)\Weka-3-6\weka.jar" weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
And it worked again. Can anyone tell me why this happened? The only thing I can think of is that the Java update isn't playing nice with itself somehow. Any insight would be appreciated thanks.
SET WEKA_HOME=c:\Program Files (x86)\Weka-3-6
SET CLASSPATH=%CLASPATH%;%WEKA_HOME%\weka.jar
bash learn.sh

Bash multi input Java tester

I am currently trying to make a bash testing script that will...
1) Go into many peoples folders
2) Compile their two java files
3) Run two quick tests for the the compiled results and send the output to a file to be saved in their folder
4) Take the results of those four result files, and dump them into one result file with a template at the top for me to input the results
... and I currently have most of this done. My only issue is that their program asks for a couple lines of input, for example...
Input num 1:
Input num 2:
Input num 3:
... and so on, and I am not sure how to get it to continue putting input into their program. Do I need an EOF after my hard coded input in my bash file?? Here is what I have so far...
#! /bin/bash
for i in $(find . -maxdepth 1 -type d)
do
pwd
pushd "$i"
pwd
if [ -f "First.java" ];
then
javac -cp . First.java
echo easyFirst.txt | java -cp . First - > easyFirstResult
echo hardFirst.txt | java -cp . First - > hardFirstResult
fi
if [ -f "Second.java" ];
then
javac -cp . Second.java
echo easySecond | java -cp . Second - > easySecondResult
echo hardSecond | java -cp . Second - > hardSecondResult
fi
printf "easyFirstResult\t: \hardFirstResult\t: \easySecondResult\t: \hardSecondResult\t: " > lab5grade.txt
popd
done
P.S. Everything is working besides the multi-line input, and I have two text files with my hard coded input to test the code.
Thanks!
I see commands like
echo easyFirst.txt | java -cp . First - > easyFirstResult
apparently supplying a line of input to the java programs; but echo commands like that don't transfer file contents, they merely copy text like "easyFirst.txt" to stdout. To pipe the contents of file easyFirst.txt into First, use a command like
java -cp . First - < easyFirst.txt > easyFirstResult
(Note, the above supposes classpath is ., class is First, and - is an unexplained command line argument to First.)

Ubuntu script to compile multiple java files?

I understand the command would be javac file_name.java but how would I put together a shell script which could compile several java files?
I was also thinking about copying the files, which I presume I just use cp and absolute file path referencing.
Create a .sh file and add the following contents. Make the file as executable and run it.
(Specify the complete path along with the file name)
#! /bin/sh
javac sample.java
Try this script: compile_java_files.sh
#!/bin/sh
typeset -r JAVA_FILES_DIR=$(cd full_path_to_java_files 2>/dev/null ; pwd) # JAVA FILES DIRECTORY
LOG_DIR="/tmp/java_compilation/logs" # Create this dir or use another one
for java_file in `ls $JAVA_FILES_DIR`;
do
javac $java_file
return_status=`echo $?`
if [ $return_status -ne 0 ]
then
echo "Failed to compile $java_file" >> $LOG_DIR/$java_file.ERR
exit 1
fi
done
Then run your script(don't forget to specify the path to the directory that contain java files):
chmod +x compile_java_files.sh
./compile_java_files.sh

Categories

Resources