Lecture 1: Introduction, Basic UNIX

Post on 30-Jan-2016

18 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Lecture 1: Introduction, Basic UNIX. Advanced Programming Techniques. Why are we here?. What’s a computer Why do we run programs? What is needed to create a program?. Applications. Shell \. Kernel (OS). Hardware. Structure of a typical OS. There are many standard applications: - PowerPoint PPT Presentation

Transcript

Lecture 1: Introduction, Basic UNIX

Advanced Programming Techniques

Why are we here?

What’s a computerWhy do we run programs?What is needed to create a program?

Structure of a typical OS

Applications

Shell

\Kernel (OS)

Hardware

There are many standard applications:

• file system commands

• text editors

• compilers

• text processing

Logging In

Create Acct www.cs.drexel.edu/Account.php

ClusterPuTTY/SSHLog inPassword

Home Directory

The user’s personal directory. E.g., /home/kschmidt /home/vzaychik

Location of many startup and customization files. E.g.:.vimrc .bashrc .bash_profile .forward .si

gnature .plan .logout

Unix Filesystem

FilesDirectoriesOther special files

The Filesystem (eg)

/

bin etc home/ tmp usr

hollid2 scully bin etc

netprog unix X ls who

Pathnames

Unique, on a given filesystemAbsolute vs. relative./ ../ ~/

Pathname Examples

/

bin/ etc/ home/ tmp/ usr/

Hollid2/ scully/ bin/ local/

netprog unix/ X ls who

/usr/bin/lsSyllabus

/home/hollid2/unix/Syllabus

Commands for Traversing Filesystem

lspwdcdrmcpmvmkdirrmdir

Viewing files

cat

less, more od

Comparing files diff cmp

Copying, removing, linking

rm – remove filemv – move (rename) filecp – copy fileln – create hard (inode) or soft (symbolic) links to a filetouch – update file modification time, create an empty file if file doesn’t exist

Commands for directories

mkdir make directoryrmdir remove directoryDirectories can also be moved or renamed (mv), and copied (cp –r)

Commands for Archiving

tar – Tape Archive makes a large file from many files

gzip, gunzip compression utility

tar on Linux does compression with the z option:$ tar czf 571back.tgz CS571

$ tar xzf assn1.tgz

File Permissions

Three types: read abbreviated r write abbreviated w execute abbreviated x

There are 3 sets of permission:1. user2. group3. other (the world, everybody else)

ls -l and permissions

-rwxrwxrwx User Group Others

Type of file:- – plain filed – directorys – symbolic link

Bourne-again Shell (bash)

ShellsStartup Upon login (interactive), at the shell

prompt customization files:

/etc/profile .bash_profile .bashrc

Command syntax

First token is the “command”Come in 3 flavors: alias shell builtin External programs (utilities)

$PATH

Use type

Command Options and Arguments

command option(s) arguments

Options (flags) Short Long Option args

Arguments

man Pages

maninfo

Some simple commandsdate – print current datewho – print who is currently logged infinger usr – more information about usrls -ao – lists (long) all files in a directorydu -sh – disk usage summary, human readablequota

Standard I/OThe shell establishes 3 I/O channels: stdin (0) stdout (1) stderr (2)

These streams my be redirected to/from a file, or even another command

Basic control codes

Ctrl-D (^D) set ignoreeof

Ctrl-C (^C)Ctrl-U (^U)Ctrl-Z (^Z)Ctrl-L (^L)

Shell metacharactersSome characters have special meaning to the shell: I/O redirection< > |

wildcards* ? [ ]

others& ; $ ! \ ( ) space tab newline

These must be escaped or quoted to inhibit special behavior

Shell Variables

ValuesAssignmentReading

Shell maintains variables

Some common ones:$PATH – list of directories to search for

utilities$PS1 – Primary prompt$HOME – user’s home directory$USER – user’s login name$PWD – current working directory

set command (shell builtin)

The set command with no parameters will print out a list of all the shell variablesSets options in the shell -o -noclobber -ignoreeof

Quoting

Escape charStrong quotingWeak quoting

I/O Redirection

> - output to a file (clobber)>> - append< - input from a file2> - redirect stderr

Pipes – connecting processes

A pipe is a holder for a stream of data.A pipe can be used to hold the output of one program and feed it to the input of another.

prog1prog1 prog2prog2STDOUT STDIN

filtersPrograms that read some input (but don’t change it), perform a simple transformation on it, and write some output (to stdout)Some common filters… wc – word count (line count, character count) tr – translate grep, egrep – search files using regular

expressions sort – sorts files by line (lexically or

numerically) cut – select portions of a line uniq – Removes identical adjacent lines head, tail – displays first (last) n lines of a file

The Unix Philosophy

Stringing small utilities together with pipes and redirection to accomplish non-trivial tasks easilyE.g., find the 3 largest subdirectories:$ du –sh * | sort –nr | head -3120180 Files

22652 Zaychik

9472 tweedledee.tgz

pipes and combining filters

Connect the output of one command to the input of another command to obtain a composition of filters

who | wc -lls | sort -fls -s | sort -nls -l | sort -nr -k4ls -l | grep ‘^d’

Process ControlProcesses run in a subshellSubshells inherit exported variablesEach process is has an ID (pid) and a parent (ppid)Use the ps utility to look at some processes:$ ps

PID TTY TIME CMD 350 pts/4 00:00:00 bash22251 pts/4 00:00:00 vim22300 pts/4 00:00:00 ps

Job Control

The shell allows you to manage jobs place jobs in the background move a job to the foreground suspend a job kill a job

EditorsA text editor is used to create and modify text files.The most commonly used editors in the Unix community: vi (vim on Linux)

$ vimtutor emac

$ emacs Then, hit ctrl-h t (that’s control-h, followed by ‘t’)

You must learn at least one of these editors

top related