Tutorial 0A Programming on the command line
Operating systems User Software Program 1 Program 2 Program n Operating System Hardware CPU Memory Disk Screen Keyboard Mouse 2
Operating systems Microsoft Apple Unix Windows 98 Mac OS X Leopard BSD 4.3 Windows XP Windows Vista Windows 8 Windows 10 Mac OS X Snow Leopard Mac OS X Mountain Lion Mac OS X Mavericks Mac OS X Yosemite HP-UX Linux AT&T Unix Solaris 3
Operating systems Microsoft Apple Unix Windows 98 Mac OS X Leopard BSD 4.3 Windows XP Windows Vista Windows 8 Windows 10 Mac OS X Snow Leopard Mac OS X Mountain Lion Mac OS X Mavericks Mac OS X Yosemite HP-UX Linux AT&T Unix Solaris 4
Operating systems Microsoft Apple Unix Windows 98 Windows XP Windows Vista Windows 8 Mac OS X Leopard BSD 4.3 Mac OS X Snow Leopard HP-UX Different from the others: the GUI (graphical user interface) Mac OS is X independent Mountain Lion of the OS Linux (operating system). There Mac OS are X many Mavericks choices. AT&T Unix Windows 10 Mac OS X Yosemite Solaris 5
The terminal & shell Shell Terminal Display manager Linux Hardware 6
Files In UNIX there are three basic types of files 1.Ordinary Files: An ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, we look at working with ordinary files. 2.Directories: Directories store special and ordinary files, and other directories. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders. In particular, directories are organized in a hierarchy, starting at the root directory. 3.Special Files: Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names. 7
Shell commands Command ls ls l ls a ls *.java ls ~ pwd cd Progs cd.. cd ~ Description List the files in the current directory Give a long listing of the files in the current directory List all the files in the current directory (including hidden files = filenames that start with a. ) Use * as a wildcard = list all files that end in.java List all files in the user s home directory Display the current working directory Change current working directory to Progs ( go into ) Change current working directory to parent directory Change current working directory to user s home directory cd /users/jaco/progs 8
Shell commands Command Description mkdir Tut0A Create a new directory rmdir Tut0A Remove a directory (only if it is empty) cd - Change back to the previous working directory echo abc Display a string on the screen cat Greet.java Display the contents of a file cp A.java B.java Make a copy of a file mv B.java C.java Change the name of a file mv A.txt ~ Move one file to another directory mv B.txt ~/C.txt Move one file to another directory and name 9
Shell commands View the manual page for some command: man ls More commands: cat Display File Contents cd Changes Directory to dirname chgrp Change file group chmod Changing Permissions cp Copy source file into destination file Determine file type find Find files grep Search files for regular expressions head Display first few lines of a file ln Create softlink on oldname ls Display information about file type mkdir Create a new directory more Display data in paginated form mv Move/rename a file pwd Print current working directory rm Remove/delete filename rmdir Delete an existing directory if empty tail Prints last few lines in a file touch Update access and modification time of a file 10
More commands! awk Pattern processing language cmp Compare contents of two files comm Compare sorted data cut Cut out selected fields of file diff Differential file comparator expand Expand tabs to spaces join Join files on some common field perl Data manipulation language sed Stream text editor sort Sort file data split Split file into smaller files tr Translate characters uniq Report repeated lines in a file wc Count words/lines/characters vi Visual text editor vim vi improved fmt Simple text formatter spell Spell checker ispell Interactive spell checker ex Simple line editor emacs GNU project Emacs compress Compress files gzip GNU file compression tar Archiver man Manual pages apropos Keyword lookup of commands 11
Redirection and pipes The shell is very, very powerful. One important feature is redirection : Read input from a file $ sort < mydata.txt Write output to a file $ sort mydata.txt > sorted.txt Append output to a file $ sort moredata.txt >> sorted.txt Combine the above $ sort < somedata.txt >> sorted.txt Another feature is pipes : Write output to another program $ find. name *.java sort 12
Java compiler and execution javac Greet.java Greet.java Java compiler Greet.class java Greet Hello, world! Greet.class Java virtual machine 13
Java compilation & execution step-by-step Make a new directory for your programs $ mkdir RW114 $ cd RW114 Use an editor (such as gedit) to write the following program public class Greet { public static void main(string[] args) { System.out.println("Hello, world!"); } } Save it in a file called Greet.java 14
Java compilation & execution step-by-step Compile the program $ javac Greet.java $ ls Greet.java Greet.class Check the java version (just for interest s sake) $ java version java version "1.7.0_45 Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08) Execute our program $ java Greet Hello, world! 15
Java compilation & execution step-by-step Change the work public to pubic $ javac Greet.java Greet.java:1: error: class, interface, or enum expected pubic class Greet { ^ 1 error Switch on line numbers to find the errors Correct the previous mistake. Now delete the last } in the program. $ javac Greet.java Greet.java:4: error: reached end of file while parsing } ^ 1 error 16
Java compilation & execution step-by-step Correct the previous mistake. Replace Hello, world! with 2/0. $ javac Greet.java $ java Greet Exception in thread "main java.lang.arithmeticexception: at Greet.main(Greet.java:3) / by zero The first kind of error is a compile-time error. Java does not even produce.class file. The second kind of error (above) is a run-time error. These are more difficult to fix. Always read the error messages carefully. 17
Which computer must I buy? It does not really matter! If possible, wait and determine your needs. How are other students using their computers? Does it work for them? Do they bring it to class, or mostly leave it at home? The CPU and harddrive specs are not too important. 500 GB harddrive space is enough for most people. The most important spec is memory! Buy as much as possible. At the very least 4GB, but aim for 8GB. http://www.sun.ac.za/infoteg IT Service Catalogue (left menu under IT LINKS) Purchasing Hardware Desktops & laptops 18
How do I install Ubuntu In this course, we are using Ubuntu Ubuntu = Linux + other bundled software You can buy an installation CD from Ed Burger (NARGA A) for R20 (?) Installation should be easy, but if you have trouble, bring your PC to the second tutorial and ask a demi to help you. 19