Tutorials¶
Linux Intro¶
There are many Linux command line tutorials online, so this will be a brief one. Search linux command line tutorial for a bunch of options.
The basic idea behind Linux, from the user’s perspective, is that there are a lot of small programs that can form powerful combinations and do complex things. The first step is to get to a linux command line. We will assume you are connecting to a remote linux host. You can do this through a variety of programs:
Programs to connect to a remote Linux host.
Mac: use the terminal program. This is actually a linux command line
Windows: the easiest thing to use the putty application. It is a free download. Enter the web address of the host you want to connect to, and then start a session. Note that with putty, you will not be able to use applications that open up new windows. You might want to create a linux live cd, essentially a bootable cd (or usb stick) that will boot your computer into linux, but will not alter anything on your hard drive (leaving your precious Windows untouched). I recommend the Ubuntu live cd.
Linux: use the shell or terminal. There are many options depending on which Linux flavor you have. you might have gterm, kterm, etc. Basically, open a command line
Once you have the appropriate program, connect to a host. home.cs.siue.edu should almost always be available, I will gove you some others in class.
To connect to the remote linux server, you need to tell a secure shell program to connect the appropriate host. If you are using a terminal, you invoke the ssh program from the command line. If you are using putty, simply put the server address in the dialogue box and click the connect button. Putty will ask for your username and passwor. To invoke the ssh program by hand in a terminal, type the following. The -X -Y options will allow you to use programs that create new windows.:
ssh home.cs.siue.edu -l [your username] -X -Y
Once connected, you should be at a simple command line in a shell. The shell is actually an interpreter for a programming language, the default language is usually bash. In its simplest form, bash takes a line that you enter, and executes it. Typically, the lines you enter will call other programs, but you can enter if statements, loops, etc in the bash syntax.
Once connected, you can use some commands. The following is a brief list of commands. Note that every command should have a man page that describes what it does and how to use it. If you want to learn moore about a command, you simply type man [the command].
Some basic linux commands
ls - list the contents of a directory
mkdir - make a directory
cd - change to another directory
touch - update the timestamp of a file, or create the file if it does not exist
nano - a simple text editing program
cat - show the contents of a file (dump it to the screen)
less - browse the contents of a file (hit q to quit)
gcc - the c compiler
g++ - the c++ compiler
The following is a script showing some of the commands being used. Note that the prompt tells you what directory you are in. ~ is a shortcut for your home directory. Everything after the $ sign is a command that I have entered. If the command produces output, it is shown on the next line:
marmcke@beet/~$ ssh home.cs.siue.edu -l marmcke -X -Y
Last login: Tue May 13 12:04:33 2014 from beet.cs.siue.edu
If you are using PuTTY to connect to this system, some characters will not show up correctly because PuTTY defaults to the wrong character encoding. To get all the characters to display correctly, you must set the encoding/translation to UTF-8.
If you are getting disconnected from your ssh session every five minutes or so, it is probably because the SIUE firewall is resetting its connection-state tables when no traffic is seen for a few minutes. PuTTY or openssh can be configured to send a NULL character every minute or so to keep the session alive (see http://www.kehlet.cx/articles/129.html for openssh instructions).
If you are denied access to this server due to your having entered too many invalid passwords(3), reset your modem to receive a new ip address or email cs-support@siue.edu to have your ip address unblocked.
marmcke@vm-02/~$ mkdir test340
marmcke@vm-02/~$ cd test340
marmcke@vm-02/~/test340$ ls
marmcke@vm-02/~/test340$ ls -a
. ..
marmcke@vm-02/~/test340$ mkdir example1
marmcke@vm-02/~/test340$ ls
example1
marmcke@vm-02/~/test340$ ls -a
. .. example1
marmcke@vm-02/~/test340$ cd example1/
marmcke@vm-02/~/test340/example1$ ls
marmcke@vm-02/~/test340/example1$ touch ex.cpp
marmcke@vm-02/~/test340/example1$ ls
ex.cpp
marmcke@vm-02/~/test340/example1$ nano ex.cpp
marmcke@vm-02/~/test340/example1$ echo "Once the nano program opened, I typed the following short program:"
Once the nano program opened, I typed the following short program:
marmcke@vm-02/~/test340/example1$ cat ex.cpp
#include <iostream>
int main(){
std::cout << "hiiiiiiiiiii"<<std::endl;
}
marmcke@vm-02/~/test340/example1$ g++ ex.cpp -o ex
marmcke@vm-02/~/test340/example1$ ./ex
hiiiiiiiiiii
marmcke@vm-02/~/test340/example1$ cd ..
marmcke@vm-02/~/test340$ echo "the .. directory is a shortcut for the parent directory of the current directory"
the .. directory is a shortcut for the parent directory of the current directory
marmcke@vm-02/~/test340$ ls
example1
marmcke@vm-02/~/test340$ exit
logout
Connection to home.cs.siue.edu closed.
Makefiles¶
Ususally, the make program is installed on linux servers. To invoke the program, type make. The default behavior is to find a file in the current directory called makefile ( or Makefile), and execute the default target in that file. The default target is named all:. Make can do some very complicated stuff, but the most basic behavoir is that it will simply execute every line after the all: target. So, you just put commands to be exectured. Make sure the commands are indented.
We will use makefiles to compile the programs you submit in this class. That way, you can have any crazy compilation technique you want, but I simply type make, and it all compiles. This allows you to use your language of choice. The following code lists the creation and usage of a simple c++ program and a makefile to compile it.
marmcke@beet/~$ ssh home.cs.siue.edu -l marmcke -X -Y
Last login: Tue May 13 14:04:02 2014 from client49-37.wpa.siue.edu
If you are using PuTTY to connect to this system, some characters will not show up correctly because PuTTY defaults to the wrong character encoding. To get all the characters to display correctly, you must set the encoding/translation to UTF-8.
If you are getting disconnected from your ssh session every five minutes or so, it is probably because the SIUE firewall is resetting its connection-state tables when no traffic is seen for a few minutes. PuTTY or openssh can be configured to send a NULL character every minute or so to keep the session alive (see http://www.kehlet.cx/articles/129.html for openssh instructions).
If you are denied access to this server due to your having entered too many invalid passwords(3), reset your modem to receive a new ip address or email cs-support@siue.edu to have your ip address unblocked.
marmcke@vm-02/~$ mkdir test_340Ex2
marmcke@vm-02/~$ cd test_340Ex2/
marmcke@vm-02/~/test_340Ex2$ touch ex.cpp
marmcke@vm-02/~/test_340Ex2$ nano ex.cpp
marmcke@vm-02/~/test_340Ex2$ cat ex.cpp
#include <iostream>
int main(){
std::cout<< "hihihi"<<std::endl;
}
marmcke@vm-02/~/test_340Ex2$ touch Makefile
marmcke@vm-02/~/test_340Ex2$ nano Makefile
marmcke@vm-02/~/test_340Ex2$ cat Makefile
all:
g++ ex.cpp
echo "hi"
marmcke@vm-02/~/test_340Ex2$ make
make: Warning: File `Makefile' has modification time 572 s in the future
g++ ex.cpp
echo "hi"
hi
make: warning: Clock skew detected. Your build may be incomplete.
marmcke@vm-02/~/test_340Ex2$ ls
a.out ex.cpp Makefile
marmcke@vm-02/~/test_340Ex2$ ./a.out
hihihi
marmcke@vm-02/~/test_340Ex2$
Makefiles using Java¶
Using a makefile for Java is no different that for C++, or any other language, for that matter. The main problem people have is that often I will require your program to have a specific name for its executable. This can be a little troublesome if you use a package or class name that is not the name of the executable I specified. This part shows how to create an executable jar file with whatever name you choose, regardless of what you named your packages or class files.
Lets say you have an assignment and the executable name must be exampleProgram. However, your solution looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 | // this is in OtherClass.java
package example;
class OtherClass{
public OtherClass(){
// nothing to see here
}
public void theFunction( String x ){
System.out.println( x );
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 | // this is in Main.java
package example;
class Main{
public static void main(String[] args){
OtherClass oc = new OtherClass();
if( args.length > 0 )
oc.theFunction( args[0] );
if( args.length > 1 )
oc.theFunction( args[1] );
}
}
|
Those files are in a example directory, consistent with the package name. For instance:
~$ pwd
~
~$ ls
example
~$ cd example/
~/example$ ls
Main.java OtherClass.java
~/example$
Warning
We assume you are in the TOP LEVEL DIRECTORY, not in the examples directory.
To compile you program, you would type:
~$ javac example/*.java
So, we would just put that in a Makefile
To create an executable jar, we use the jar command. The jar command requires the name of the desired jarfile, the directory containing the package, and a manifest file. The manifest file simply indicates which class contains the public static void main(String[] args) function. The following sequence shows the contents of a manifest file, and the correct jar command. Note the that jar options are for the following:
- c:
- create a jar file
- m:
- the next argument will be the name of the manifest file
- f:
- the argument after the manifest file will be the directory holding the package to jar (the compiled files are there)
Here is it how it looks:
~$ ls
example Makefile manifest.mf
~$ cat manifest.mf
manifest-Version: 1.0
Main-Class: example.Main
~$ jar cmf manifest.mf exampleProgram.jar example
~$
Finally, putting all that in a Makefile looks like this:
~$ cat Makefile
all:
javac example/*.java
jar cmf manifest.mf exampleProgram.jar example
@echo "+ to run type: java -jar exampleProgram.jar"
@echo "+ to run with command line arguments, simply append them to the line above"
~$
So, the final directory structure with the Makefile looks like the following:
~$ ls example/
Main.java OtherClass.java
~$ ls .
example Makefile manifest.mf
~$ $ make
javac example/*.java
jar cmf manifest.mf exampleProgram.jar example
+ to run type: java -jar exampleProgram.jar
+ to run with command line arguments, simply append them to the line above
~$ ls example
Main.class Main.java OtherClass.class OtherClass.java
~$ ls .
example exampleProgram.jar Makefile manifest.mf
To run the program, you simply use the java command, but give it the -jar option:
~$ java -jar exampleProgram.jar
~$ java -jar exampleProgram.jar 1
1
~$java -jar exampleProgram.jar 1 42
1
42
So, we were able to name the jar file whatever we wanted.
Excercise
This example also utilized command line arguments. The final code block shows the program being run with 0, 1 and 2 command line arguments. Command line args are super useful, you should walk through the program to see how they work. The basic idea is that the static public void main(String[] args) function gets an array of strings from the command line when the program runs. The operating system takes the arguments from the command line, and gives them to the main function when it sets up the program’s memory.
Modify the program so that it will print out up to 3 arguments.
Modify the program so that it will print out ALL arguments given.
Modify the program so the the 1st argument is an integer indicating how many of the following command line arguments should be printed.
Python¶
There are two trunks of python that are actively developed: the 2.x and 3.x trunks. We will use the 2.x trunk in this class. So download the appropriate one (python 2.7 is current).
Ptyhon is a scripting language, and happens to be an interpreted language. As a scripting language, it is meant to easily handle text processing, be oriented to writing code quickly, and is easily integrated with other executables and libaries. Furethermore, python has rules to enforce the readability of code, and lots of tools/libraries to make coding easier. One of the guiding philosophies to coding in python is to concentrate on the algorithm, not the code. In fact, the speed of your code should generally not concern you when coding in python, except for egregious mistakes like implementing an
algorithm when a constant time option is available and easy to implement.
In python, white space matters. There are no curly braces for code blocks; code blocks are identified by indentation. This forces nice readable code:
a = 3
if a < 2:
print 'a is less than 2'
a = 4
print 'not any more'
else:
print 'a is >= 2'
a = 1
print 'not any more'
In python, types are not declared (unless you want to). Basically, type information is figured out as the program progresses. for example:
a = 4
a = 4+7 # a is an integer
a = a + 6.2 # a is now a double
b = 'hi there' # b is a string
b = 4 # b is now an integer
Python is an interpreted language, meaning the source code is run through an interpreter (think virtual machine, like Java). Therefore, errors can occur on the fly and exceptions can/will be thrown.
The following is a simple script illustrating some basic python stuff. Get an interpreter going and try it out. You can type directly into an interpreter, or use the interpreter to execute the entire program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | a = 4
print a
a = a + 7.2
print a
a = 'hi'
print a
# tuples are like arrays, but are IMMUTABLE, you cannot change them once they are made
a = (1 ,2,3,4,'hi')
print a
#to update the tuple, make a new one and copy what you need from the old one
a = (2,3,4,a[3], a[4])
print a
# lists. Lists are like linked lists, but can be accessed with brackets like
# arrays. From a usage standpoint, they are most like dynamic arrays.
a = [] #empty list
a = [2,5,6,'hi']
print a
print a[2]
print a[3]
a.append( 'more!' )
print a
# loops. Python typically does not do all the manual loop incrementing that
# C++ does. Instead, it provides an IN keyword that will get 1 item at a time
# out of a container (a tuple, list, etc)
for item in a:
print item
# to loop a specific number of times, or to generate a counting variable to
# be used in the loop, the range function will create a list of integers in a sequence.
#range( startInt (optional), stopInt (non-inclusive), incrementStep (optional)
for i in range(10):
print i
for i in range(2, 10, 3):
print i
|
The output of the above script is shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 4
11.2
hi
(1, 2, 3, 4, 'hi')
(2, 3, 4, 4, 'hi')
[2, 5, 6, 'hi']
6
hi
[2, 5, 6, 'hi', 'more!']
2
5
6
hi
more!
0
1
2
3
4
5
6
7
8
9
2
5
8
|
The final program uses command line arguments. To include a library, python uses import statements. To use command line args, we need to import sys. There is a try catch block since command line args come in as strings, and we need to convert it to a number (there is no guarantee the user entered a number and not text).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import sys
print sys.argv
if len( sys.argv ) < 2:
print 'usage: python', sys.argv[0], '[a number]'
print 'this program computes the sum of 1 to the number you enter'
exit()
# a try catch block for the number conversion exception
try:
stopInt = int( sys.argv[1] ) # args will come in as strings, convert to int
except ValueError:
print 'You must enter an integer'
exit()
sumNum = 0
for i in range( stopInt+1 ):
sumNum += i
print sumNum
|
At this point you should have a feel for the basics. I would suggest finding a tutorial online and checking it out.
There are lots of online resources for python. In general, just search for whatever you need and the answer is usally in the top 2 or 3 search results.
Java¶
When submitting projects using Java, you must ensure that they are able to be compiled and run on one of the department linux servers (specifically, the home server). If you use a program with multiple source files, you must create an executable jar that can be run from the command line. The following example shows two source files, a manifest file, and a Makefile. If you put all of these files in the same directory, you should be able to compile them and execute program from the command line. The manifest file simply tells the jar where the static main function resides in the java files.
Source file: HelloWorld.java
1 2 3 4 5 6 7 8 | class HelloWorld
{
public static void main( String[] args )
{
OtherClass OC = new OtherClass();
OC.printSomething( "hi there, human!" );
}
}
|
Source file: OtherClass.java
1 2 3 4 5 6 7 | class OtherClass
{
public void printSomething( String thing )
{
System.out.println( thing );
}
}
|
Manifest file: MANIFEST.MA
1 | Main-Class: HelloWorld
|
Makefile:
1 2 3 4 | all:
javac -d . HelloWorld.java OtherClass.java
jar cvmf manifest.mf myJar.jar HelloWorld.class OtherClass.class
@echo "To run the file, type: 'java -jar myJar.jar'"
|
To compile and execute, put all three files in the same directory and type the following:
1 2 3 4 5 6 7 8 9 10 | $ make
javac -d . HelloWorld.java OtherClass.java
jar cvmf manifest.mf myJar.jar HelloWorld.class OtherClass.class
added manifest
adding: HelloWorld.class(in = 381) (out= 277)(deflated 27%)
adding: OtherClass.class(in = 392) (out= 272)(deflated 30%)
To run the file, type: 'java -jar myJar.jar'
$ java -jar myJar.jar
hi there, human!
$
|
The contents if the directory after all of this will be:
$ ls
HelloWorld.class HelloWorld.java MANIFEST.MF Makefile OtherClass.class OtherClass.java myJar.jar
$