• No results found

Tutorial 0A Programming on the command line

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 0A Programming on the command line"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

Tutorial 0A

(2)

Operating systems

Software

Operating System

Program 1 Program 2 Program n

User

Hardware

CPU Memory Disk

(3)

Operating systems

Microsoft Apple Unix

Windows 98 Windows XP Windows Vista Windows 8 Windows 10 Mac OS X Leopard BSD 4.3 Mac OS X Snow Leopard

(4)

Operating systems

Microsoft Apple Unix

Windows 98 Windows XP Windows Vista Windows 8 Windows 10 Mac OS X Leopard BSD 4.3 Mac OS X Snow Leopard

(5)

Operating systems

Microsoft Apple Unix

Windows 98 Windows XP Windows Vista Windows 8 Windows 10 Mac OS X Leopard BSD 4.3 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 Different from the others: the

GUI (graphical user interface) is independent of the OS

(6)
(7)

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

(8)

Shell commands

Command Description

ls List the files in the current directory

ls –l Give a long listing of the files in the current directory

ls –a List all the files in the current directory (including hidden

files = filenames that start with a “.”)

ls *.java Use “*” as a wildcard = list all files that end in “.java”

ls ~ List all files in the user’s home directory

pwd Display the current working directory

cd Progs Change current working directory to “Progs” (“go into”)

cd .. Change current working directory to parent directory

cd ~ Change current working directory to user’s home directory

(9)

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

(10)

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

(11)

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

(12)

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

(13)

Java compiler and execution

Greet.java Greet.class

javac Greet.java

Java compiler

java Greet

Java virtual machine

(14)

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

Save it in a file called “Greet.java” public class Greet {

public static void main(String[] args) { System.out.println("Hello, world!"); }

(15)

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

(16)

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 }

^

(17)

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: / by zero at Greet.main(Greet.java:3)

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.

(18)

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

(19)

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

References

Related documents

We propose a simple causal model depicting relationships involved in dissemination of antibiotics and antibiotic resistance in agroecosystems and potential efects on human health,

documents, or any other type of file; the only requirement is that the file be located in the current working directory (see the cd command, later in this chapter) or in a folder

From left to right, the icons enable you to Copy a file or directory, move a file, create a directory, rename, upload a file to HDFS, download a file from HDFS to your local

A history of childhood attention-deficit hyperactivity disorder (ADHD) impacts clinical outcome in adult bipolar patients regardless of current

Proposed combination scheme with PCR6 rule yields the best verification accuracy compared to the statistical match score combination algorithms and DS theory-based combination

Testing shall be performed according to a Test Plan, which shall be included in the test report. It may be necessary to carry out some investigatory testing in order to establish some

User menyampaikan keluhan secara langsung atau melalui Short Message Service (SMS) atau telephone, karena pelaporan masih menggunakan SMS atau telephone pelapor

Properly authenticated, an FTP Client can access OCS file system functions, which include file read, file create, file delete, file rename, file write, directory read, directory