What `<` is for in bash - bash

So I'm taking a course at a university and the professor tells us to run our program like this java -cp classes cst420.Waypoint < samples.txt. What exactly is the < doing in this command? I know it's taking the samples.txt as input but I'm curious on exactly what's going on here.

It grabs input from the standard input, which becomes samples.txt, on most systems. If you're on unix, you can also do cat samples.txt | "java -cp classes cst420.Waypoint - to achieve a similar effect.
The difference being you can't seek on piped input and can on feeding it into the JVM (h/t #CharlesDuffy).

Related

When should I use file descriptors in bash scripting?

i know what they are, but i dunno when i should use them. Are they useful? I think yes, but I want you to tell me in which situations a file descriptor could be useful. Thanks :D
The most obvious case which springs to mind is:
myProgram >myProgram.output_and_error 2>&1
which sends both standard output and error to the same file.
I've also used:
myProgram 2>&1 | less
which will allow me to page through the output and error in sequence (rather than having error got to the terminal in "arbitrary" places in the output).
Basically, any time when you need to get at an already existing file descriptor, you'll find yourself using this.

Passing content of bash script to be executed in same shell given by binary file execution

Time ago i had this answer but i cannot recall how exactly was the syntax.
I have two files.
One is binary file called "changeuid" and second is "do.sh".
I need to run changeuid which will change my uid and after execution, content of do.sh, which consist of list of commands available only for uid given by changeuid script, to be executed.
What i dont remember is how this was done. Ive been testing few variants none worked or maybe tasks inside them which werent similar to what original ones do were wrong.
So .. might be:
./changeuid < sh do.sh
./changeuid < . do.sh
./changeuid < source do.sh
source do.sh < ./changeuid
sh do.sh < ./changeuid
Solution?
Thx in advance.
After some real test figured out... correct solution was:
./changeuid < ./script.sh
I didnt figured cuz something else was missing.
It rather depends on exactly what the changeuid program is doing.
I suspect that it's designed to change your uid to a given value, then run another program, and then exit (by which point you'll be back to your normal user). If that's right, then it'll need your other program to be passed to it as an argument:
./changeuid do.sh
You'll also need both changeuid and do.sh to be executable.
I'm not really sure why you'd need a separate changeuid program to do this, though. There are three ways of doing it with standard Linux commands: use su, or sudo, or runuser. Which one you choose will depend on whether you're running this as root already.
For more info, try man su, etc.
If changeuid takes commands to run on stdin, then it would be
./changeuid < do.sh

How to start writing script for multiple commands output in a single script

I want to writing a script in shell for solaris paltform regarding some below commands and their output should written in excel file or text file as a output.
How many servers are deployed under global
zone --- zoneadm list -cv
H/w architecture
showrev -a
OS Related Information
uname -a
RAM and its Utilization
echo "::memstat" | mdb -k
Internal Disk
echo | format
Which service is running on which node
hares -state |grep "ONLINE"
Solaris is a not something I am familar with. I think I have logged in once or twice in my life.
However, unix/linux I have some experience with.
You simple need to put your commands in a script file often ends with *sh (myscriptfile.sh). then you simply run the script.
That being said. You need to read up on
output redirect (that is out you write the output to file).
And learn more about your 'shell'. I would further recommend using bash (I believe out-of-the-box solaris uses something different). Bash scripting is far more popular therefore you will find greater support.

Unix cat and tail for output of java program

I have a java program that takes time to execute and I have to make 10 runs of it and I am interested in only last 5 lines of the output, the actual output runs in hundreds of lines. Since I want to average the output I want tail -5 for run into a file. Also one of the parameters (--random) in my arguments keep changing in each run.
I am doing the following:
for i in {1..10} ; do cat output| tail -5 | java -cp src.Tagger.java --random $1; done
Sorry I am really bad at bash.
You should make sure you can execute your java program at all first. You can't execute a .java file directly, it has to be compiled.
If you have the file src/Tagger.java, you can try to compile it with
javac -cp src src/Tagger.java
but if it requires other libraries or build systems, it might be completely different. If you downloaded this app, see the project documentation.
This should silently produce a src/Tagger.class. Once you have this, you can try to run it with
java -cp src Tagger --random 1234
though again, if it has dependencies on libraries, it'll be different.
If that works, you can finally start trying to run it in a loop:
for i in {1..10}
do
cat output| tail -5 | java -cp src Tagger --random 1234
done
You want the output of your java program to go to output first, then you need to tail the file. It looks like you are currently feeding output into your java program as input. I don't think that is what you want. Try this instead:
for i in {1..10}
do
java -cp src.Tagger.java --random $1 > output;
tail -5 output;
done
I also have my doubts that you have the java command correct. You shouldn't specify .java for the file name when running the java file. It needs to run from the compiled .class file and the java command assumes .class, so it isn't needed on the command line. You also are using -cp (classpath) but don't appear to be giving it an argument. I'd expect the java command to be more like:
java -cp classesdir com.mydomain.myapp.Tagger

How do I write a shell script that repeats a java program a specific number of times?

Essentially I am looking to write a shell script, likely using a for loop, that would allow me to repeat a program call multiple times without having to do it by hand (I don't know exactly how to explain this, but i want to perform the java TestFile.java command in the cmd window multiple times without doing it by hand).
I am trying to write it for the UNIX shell in bash, if that helps at all.
My program outputs a set of numbers that I want to look at to analyze end behavior, so I need to perform many tests for many different inputs and I want to streamline the process. I have a pretty basic understanding of shell scripting - i tried to teach myself today but I couldn't really understand the syntax of the for loop or the syntax of how to write a .java file call, but I would be able to write them in shell script with a little help.
This will do:
#!/bin/bash
javac Testfile.java # compile the program
for((i=1;i<=50;i++))
do
echo "Output of Iteration $i" >> outfile
java Testfile >> outfile
done
This will compile your java program and run it for 50 times and store the output in a file named outfile. Likewise, you can change the 50 for the number of iterations you want.
#!/bin/bash
for i in {1..10}
do
#insert file run command here
done
#!/bin/bash
LOOPS=50
for i IN {1 .. LOOPS}
do
java TestFile >> out.log
done

Resources