unix_environment

Download Report

Transcript unix_environment

Unix Environment
•What we learn this time:
–Big figure of Unix environments
– basic concepts and design principles
of Unix
–Shells
–File structures, naming, permissions
–Scripting
Spring 2004
FSU COP 4342 Unix Tools
1
What about Unix
• Designed by programmers for
programmers (i.e. hackers)
• a.k.a PWB (programmer’s work bench)
• Who wrote Unix and where?
– Ken Thompson and Dennis Ritchie
– AT&T Bell Labs
– Add-on utilities/programs designed by
independent programmers
Spring 2004
FSU COP 4342 Unix Tools
2
Power
• The Unix philosophy “is the idea that
the power of a system comes from the
relationships among programs than
from the programs themselves”
• Interface is minimal, but allows
programs to be strung together
• Teaches programming
• Teaches “hacking”, the ability to build a
tool when one doesn’t exist
Spring 2004
FSU COP 4342 Unix Tools
3
Unix Core
• The core of the Unix OS is called the kernel
• Variety of Unix flavors:
– FreeBSD
– Linux
– Darwin (Mac OS X)
• Most functionality is the same between implementation
– vi, cd, man
• Built-in commands and utilities behavior might differ
– FreeBSD make vs. GNU make
• POSIX (Portable Operating System Interface)
– IEEE standard that tries to work toward apps
interoperability
– System V vs. Darwin
• are much different
Spring 2004
FSU COP 4342 Unix Tools
4
Communication with Unix
• A user never “talks” directly to a Unix OS
• What do these programs use to talk to the
OS?
– System calls
• Three programs that are used to communicate
with the OS are:
– shell
• a command interpreter
– Interactive command
• runs inside a tty (teletype) and reads your typing
• Text editor (i.e. vi)
– GUI
• implemented with a set of running programs
Spring 2004
FSU COP 4342 Unix Tools
5
Tools working together
• The Unix programs are tools
– specific in function
– multi-purpose
• To make them general-purpose:
– Must be data independent
• Output of any program should be input for another
• Info needed by a program should be contained in a data
stream passed to it or passed at command prompt
• If no arguments are passed to a program, read
stdin(keyboard) and print stdout(screen)
• Programs meeting the above criteria are called
filters
– Pipe – take output from program on left and feed to
input of program on right
• E.g. cat /etc/passwd | grep –i anyusername
Spring 2004
FSU COP 4342 Unix Tools
6
Shells
• What is a shell?
– A command interpreter
– “protects you from the kernel”
– It really protects the kernel from you
Spring 2004
FSU COP 4342 Unix Tools
7
Many Shells
• Bourne shell (sh)
– Creator Steve Bourne in the early 80’s
– First shell Used for shell programming
• C shell (csh)
– Created at UCB their Unix implementation
in the early 80’s
– Users wanted more familiar syntax
– More features (for interactive uses) than sh
(e.g. job control and history)
Spring 2004
FSU COP 4342 Unix Tools
8
Many Shells
• Korn shell (ksh)
–
–
–
–
–
Created by David Korn in the mid 80’s
Compatible with sh but having most features of csh
Features history editing (a.k.a. command-line editing)
Was available on System V
Public-domain version is pdksh
• T-shell (tcsh)
– has all csh features and less bugs
• Bourne-again shell (bash)
– Offered by FSF (free software foundation)
– Similar to ksh and csh
– Command-line editing
Spring 2004
FSU COP 4342 Unix Tools
9
Many Shells
• Z shell (zsh)
– hybrid
• Plan 9
– Created by Bell Labs
• Tcl (tclsh), wish
Spring 2004
FSU COP 4342 Unix Tools
10
What’s my shell?
• Check your prompt
– Usually bash uses $
– Usually csh uses %
– Usually tcsh uses >
– Superuser “root” usually is #
• grep yourusername /etc/passwd
• echo $SHELL
Spring 2004
FSU COP 4342 Unix Tools
11
Programming the Shell
• The shell is a complete
programming language
• Easy
– As simple as a single complex
command saved in a file
– Look at the example used in the text
mac2unix
Spring 2004
FSU COP 4342 Unix Tools
12
Converting a MAC file to
Unix
MAC file ends each line with carriage return (015).
Unix file ends each line with linefeed (012).
$ tr ‘\015’ ‘\012’ < file.mac > file.unix
You can save “tr ‘\015’ ‘\012’” into a file, say mac2unix
$ chmod +x mac2unix
$ mac2unix < file.mac > file.unix
Spring 2004
FSU COP 4342 Unix Tools
13
Internal and External
Commands
• Internal commands
– built into the shell
– the shell performs the command
• E.g. chdir or cd
• External commands
– Require the shell to fork and exec and
a subprocess will start
• E.g. ls
Spring 2004
FSU COP 4342 Unix Tools
14
Internal & External
Commands
• Shell checks what type of command the
user is trying to run
–
–
–
–
Check if built-in
Else check if absolute path
Else check alias (except bash)
Check for executable in search path
• Search path is a list of dir that the shell must
check
• An environment variable PATH lists these dir
– Look at an example
• Search path is specified in the shell start up files
Spring 2004
FSU COP 4342 Unix Tools
15
Kernel and Daemons
• Unix is a multi-user OS
• The kernel is the main program or the
heart
–
–
–
–
Assigns memory to programs running
Partitions time fairly
Handles the I/O, etc.
Think of it as a traffic cop or a manager
• Daemons
– Group of “helper” programs
– help the system with mail, network comm,
tracking time, etc
Spring 2004
FSU COP 4342 Unix Tools
16
Kernel and Daemons
• ps command
– prints information about active
processes
• shutdown command
Spring 2004
FSU COP 4342 Unix Tools
17
Unix Filenames
• Filenames can be made up of any
character set except a slash ( / )
– Case sensitive
– Underscores are handled for word
separation
– Periods are used by some programs to
separate filenames from file extensions
– Filenames beginning with a dot (.) are
treated differently by the shells
• No concept of file version
• Deleted file is gone forever.
Spring 2004
FSU COP 4342 Unix Tools
18
File Extensions
• MS files are filename.extension
• Unix has no specific rules about file
extensions
– Some programs such as the C
compiler will look for file extensions
Spring 2004
FSU COP 4342 Unix Tools
19
Wildcards
• * - matches any character
• ? – matches any single character
• filename.[character-set]
– To match a particular group of characters,
say all files that end with an F or P
• E.g. filename.[FP]
• One exception
– slash (/), no matches can be made against
it
Spring 2004
FSU COP 4342 Unix Tools
20
Hierarchical File System
Spring 2004
FSU COP 4342 Unix Tools
21
Home dir
• /home/username – your own place
• Store any file you like
Spring 2004
FSU COP 4342 Unix Tools
22
Pathnames
• Locate files
• Current directory – currently working
directory
• Absolute vs. relative paths
–
–
–
–
–
pwd vs. cd
. Current dir
.. Parent dir
~ your home dir
~username username’s home dir
Spring 2004
FSU COP 4342 Unix Tools
23
File Permissions
• UID – user ID
• Every user belongs to a group
– groups
• When a file is first created, its owner is
the user that created and the primary
group in which they belong to.
• chgrp – change group
• chown – change owner
• chmod – change permissions on file(s)
Spring 2004
FSU COP 4342 Unix Tools
24
Superuser (root)
• Do we need to say more?
Spring 2004
FSU COP 4342 Unix Tools
25
Unix Files
• Unix is file oriented
• Everything is treated as a file
• File is a stream of bytes with no
special structure, but it is defined
by the programs that use it
• Files use newline character instead
of carriage return
Spring 2004
FSU COP 4342 Unix Tools
26
Scripting
• Scripting languages and apps differ
from compiled languages, but how?
– Interpreted as run
• Perl
– Practical Extraction and Report Language
– Created by Larry Wall
• Python
– More structured and verbose than Perl
Spring 2004
FSU COP 4342 Unix Tools
27
Networking and comm
•
•
•
•
•
•
•
•
ssh
sftp
scp
telnet
rsh
ftp
write
talk
Spring 2004
FSU COP 4342 Unix Tools
28
X Windows
• X Consortium and XFree86 Project
• A system that divides up the large scale
screen of a workstation into multiple
virtual terminal or windows
• Runs on many different kinds of
hardware
• Let’s you run a program on a remote
computer while the program’s windows
are displayed on your local terminal
Spring 2004
FSU COP 4342 Unix Tools
29