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.)
Related
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.
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
I have a jar file which is a program which accept user input and processes it. I am running this jar file using the below shell script:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
The problem I am facing with this is, I want to restrict the user from exiting the application using any of the combinations of the below commands.
For example:
Ctrl + z
Ctrl + c
Ctrl + break
Please help me.
I recommend to you use simple start and stop script for your program;
1) create sh script with name start_myprogram.sh and put into the file ;
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
nohup java -DMY_PROG -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
2) create sh script with name stop_myprogram.sh and put into the file ;
#!/usr/bin/sh
USER=`whoami`
PID=`ps -xfu $USER| grep java | grep MY_PROG | grep -v grep | awk '{ print $2 }'`
if [ -n "$PID" ]
then
kill $PID
else
echo MY_PROG is not running.
fi
3) start your program ./start_myprogram.sh &
4) anytime whenever you want stop your program ./stop_myprogram.sh
*This is maybe not answer of your question but at least you dont need to implement anything more.
I would suggest the following change in the script to get to the desired requirement.
It seems that you need some kind of function which will catch these commands and not let the commands get executed. Shell script can have this kind of functionality by implementing the use of trap.
You can make change in your script like this:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
#Add these two lines in the code for catching exit commands
trap '' 20
trap ' ' INT
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
Its very simple to use traps in shell scripts. Hope this works for you.
My worries about System.out.println used in java code, all java files are not available for me. We have only .class in Production. There are thousand files having System.out.println entry.
In order to clean this string. How can I find all culprit class files which has this string.
I know javap, which disassemble the .class file. But don't know if javap can be used for my purpose.
Any Unix command or java programme or awk script or known UI tool will work for me.
I just want to get rid of System.out.println
I used below command in cygwin but no success !
find . -iname '*.class' -printf "%p | grep -q 'System.out.println' && echo %p\n" | sh
I am using Gnu grep version 2.10.
Then given a sample class file wich contatin System.out.println i get
bash$ grep 'System.out.println' sample.class
Binary file sample.class matches
so in order to extract the files with matches you could use something like:
#! /bin/bash
shopt -s globstar nocaseglob
files=(**/*.class)
for (( i=0; i<${#files[#]}; i++ )) ; do
file="${files[$i]}"
res=$(grep 'System.out.println' $file)
[[ $? == 0 ]] && awk '{print $3}' <<< "$res"
done
I have created some application, which is reading from System.in using the following method:
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String line = input.nextLine();
}
Input data is being passed with linux command:
cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main
The crontab entry looks like:
MAILTO=someuser
CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"
0,10,20,30,40,50 * * * * cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main >/home/someuser/output.txt
The permissions for the files shows the following:
-rw-r--r-- 1 someuser serhiy 8385601 2011-02-07 10:57 /home/someuser/somefile.txt
Everything is working fine on my machine(Ubuntu 9), but after installation on another machine Ubuntu 8, I figured out that program starts but seems not to be reading anything. I have triple checked all configurations and all permissions and the result still the same. When I run command manually everything is working, when it's ran by crontab it seems not reading input. Anyone experienced this issues before?
Thanks for any help
Serhiy.
Are you defining the variables in crontab ? That does not seem right.
1) Move the command to a shell script and invoke the shell command from cron, eg
*/10 * * * * /home/someuser/some_script.sh >/home/someuser/some_script.cronoutput 2>&1
2) Contents of some_script.sh ; make sure the execute bit is set
#!/bin/sh
export MAILTO=someuser
export CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"
cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main >/home/someuser/output.txt