Lecture 1: Introduction, Basic UNIX: Advanced Programming Techniques Summer 2003
Lecture 1: Introduction, Basic UNIX: Advanced Programming Techniques Summer 2003
Basic commands File system Shell Filters (wc, grep, sort, awk)
1Many
of the examples for this lecture come from the UNIX Prog. Env. and AWK books shown (see lecture outline for full references)
Operating Systems
An Operating System controls (manages) hardware and software.
provides support for peripherals such as keyboard, mouse, screen, disk drives, software applications use the OS to communicate with peripherals. The OS typically manages (starts, stops, pauses, etc) applications.
Flavors of Unix
There are many versions of Unix that are used by lots of people:
SysV (from AT&T) BSD (from Berkeley) Solaris (Sun) IRIX (SGI) AIX (IBM) LINUX (free software)
customize the shell and user interface. string together a series of Unix commands to create new functionality. create custom commands that do exactly what we want.
There are many standard applications: file system commands text editors compilers text processing
Hardware
Kernel (OS)
Interacts directly with the hardware through device drivers Provides sets of services to programs, insulating these programs from the underlying hardware Manages memory, controls access, maintains file system, handles interrupts, allocates resources of the computer Programs interact with the kernel through
system calls
Logging In
To log in to a Unix machine you can either:
sit at the console (the computer itself) access remotely, via SSH, e.g.
The system prompts you for your username and password. Usernames and passwords are case sensitive!
CS Dept. Accounts
See http://www.cs.drexel.edu/~kschmidt/Re f/csLogin.html All CS machines (that you have access to) running Linux
tux machines the farm you may connect to from anywhere lab machines any of the desktops you may sit at in the lab, classrooms
Username
(typically) a sequence of alphanumeric characters of length no more than 8. the primary identifying attribute of your account. (usually) used as an email address the name of your home directory is usually related to your username.
Password
a password is a secret string that only the user knows (not even the system knows!) When you enter your password the system encrypts it and compares to a stored string. passwords should have at least 6 characters It's a good idea to mix case, include numbers and/or special characters (don't use anything that appears in a dictionary!)
Home Directory
The users personal directory. E.g.,
/home/kschmidt /home/vzaychik
Where all your files go (hopefully organised into subdirectories) Mounted from a file server available (seemlessly) on *any* department machine you log into
Home Directory
Your current directory when you log in cd (by itself) takes you home Location of many startup and customization files. E.g.:
.vimrc .bashrc .bash_profile .forward .plan .mozilla/ .elm/ .logout
Directories
A directory is a special kind of file - Unix uses a directory to hold information about other files. We often think of a directory as a container that holds other files (or directories). A directory is the same idea as a folder on Windows.
Files that are in different directories can have the same name.
bin
etc
home/
scully X
tmp
bin ls
usr
etc who
Unix Filesystem
The filesystem is a hierarchical system of organizing files and directories. The top level in the hierarchy is called the "root" and holds all files and directories in the filesystem. The name of the root directory is /
Pathnames
The pathname of a file includes the file name and the name of the directory that holds the file, and the name of the directory that holds the directory that holds the file, and the name of the up to the root The pathname of every file in a given filesystem is unique.
Pathnames (cont.)
To create a pathname you start at the root (so you start with "/"), then follow the path down the hierarchy (including each directory name) and you end with the filename. In between every directory name you put a "/".
Pathname Examples
/ bin/ etc/ home/ scully/ X tmp/ usr/
Syllabus /home/hollid2/unix/Syllabus
/usr/bin/ls
Absolute Pathnames
The pathnames described in the previous slides start at the root. These pathnames are called "absolute pathnames". Special absolute:
~kschmidt/ /home/kschmidt (for users home directories only) ~/ Your home directory (so, relative to login, $USER)
Relative Pathnames
Prefixed w/the current directory, $PWD So, relative to the current working directory $ cd /home/hollid2 $ pwd /home/hollid2 $ ls unix/Syllabus unix/Syllabus $ ls X ls: X: No such file or directory $ ls /home/scully/X /home/scully/X
hollid2
scully
man Pages
To get information about anything that's been properly installed, use man:
man ls man cat man man
The ls command
The ls command displays the names of some files. If you give it the name of a directory as a command line argument it will list all the (unhidden) files in the named directory.
l long format (include file times, owner and permissions) a all (shows hidden* files as well as regular files) F include special char to indicate file types. C place into columns
cd change directory
The cd command can change the current working directory: cd change directory
The general form is: cd [directoryname]
Viewing files
cat concatenate, send to stdout. View contents of text files less, more paging utilities (hit h for help) od octal dump. For viewing raw data in octal, hex, control chars, etc.
cp copy file
cp someDir/file someDir/file.copy
gzip, gunzip
tar on Linux does gzip compression with the z option: $ tar czf 571back.tgz CS571 $ tar xzf assn1.tgz
File attributes
Every file has some attributes:
Access Times:
when the file was created when the file was last changed
Size Owners (user and group) Permissions Type directory, link, regular file, etc.
when the file was last changed ls -l sort by modification time ls -lt
File Owners
Each file is owned by a user. You can find out the username of the file's owner with the -l or -o option to ls:
[jjohnson@ws44 winter]$ ls -l total 24 drwxr-xr-x 7 jjohnson users -rw------1 jjohnson users -rw-r--r-1 jjohnson users 80 Jan 8258 Jan 8261 Jan 3 3 3 2005 cs265/ 2005 cs265.html 2005 cs265.html~
ls -l
$ ls -l foo -rw-rw----
permissions
name
File Permissions
Each file has a set of permissions that control who can mess with the file. There are three types of permissions:
ls -l and permissions
-rwxrwxrwx
User Type of file: - plain file d directory s symbolic link Group Others
rwx
Files:
Directories:
Changing Permissions
The chmod command changes the permissions associated with a file or directory. There are a number of forms of chmod, this is the simplest: chmod mode file
r4 w2 x1
chmod - examples
$ chmod 700 CS571 $ ls o Personal
drwx-----10 kschmidt 4096 Dec 19 2004 CS571/
u user g group o other a all + add permission - remove permission = set permission
chmod examples
$ ls -al foo -rwxrwx--x 1 hollingd grads foo $ chmod g-wx foo $ ls -al foo -rwxr----x 1 hollingd grads foo $ chmod u-r . $ ls ls: .: Permission denied
allows you to edit a command line. can establish alternative sources of input and destinations for output for programs.
Session Startup
Once you log in, your shell will be started and it will display a prompt.
(for our examples, we will use $ as the prompt. It is not part of the input)
When the shell is started it looks in your home directory for some customization files.
You can change the shell prompt, your PATH, and a bunch of other things by creating customization files.
Customization
Each shell supports some customization.
The customization takes place in startup files files that are read by the shell when it starts up
Startup files
sh,ksh: /etc/profile (system defaults) ~/.profile bash: ~/.bash_profile ~/.bashrc ~/.bash_logout csh: ~/.cshrc ~/.login ~/.logout
Incorrect login
You will receive the Password: prompt even if you type an incorrect or nonexistent login name Can you guess why?
Entering Commands
The shell prints a prompt and waits for you to type in a command. The first token on the line is taken to be a command (for now). Come in 2 flavors:
shell builtin - commands that the shell interprets directly. External programs (utilities) standalone programs on disk (directories in your $PATH are searched, in order)
Checks for aliases Checks for user-defined functions Looks for a builtin Checks directories in $PATH for a utility
Getting help
manual original Unix help (flat, single page) $ man who $ man man info 2-d system, emacs-like navigation $ info who The resource frame on the class page Internet google, wikipedia The linux documentation project (http://www.tldp.org/) Safari online Friends, group-mates, and others
usr
ls -ao lists (long) all files in a directory du -sh disk usage summary, human readable quota
Logging off
exit command
Exits the shell If it is the login (top-level) shell, then it disconnects you
A shell is just another program that is running. Can recursively invoke shells Please dont just disconnect w/out exiting
Standard I/O
When you enter a command the shell creates a subshell to run the process or script. The shell establishes 3 I/O channels:
Standard Input (0) keyboard Standard Output (1) screen Standard Error (2) screen
Program
Shell metacharacters
Some characters have special meaning to the shell. These are just a few:
I/O redirection < > | wildcards * ? [ ] others & ; $ ! \ ( ) space tab newline
Wildcards
* matches 0 or more characters ? matches exactly 1 character [<list>] matches any single character in <list> E.g.
ls ls ls ls *.cc list all C++ source files in directory a* list all files that start w/a a*.jpeg list all JPEGs that start w/a * - (make sure you have a subdirectory, and try it)
Wildcards - classes
[abc] matches any of the enclosed characters ls T[eE][sS][tT].doc
listed.
ls [!0-9]*
Shell Variables
bash uses shell variables to store information Shell variables are used to affect the behavior of the shell, and many other programs We can access these variables:
set new values for some to customize the shell. find out the value of some to help accomplish a task.
Setting/Viewing Variables
To assign (in sh, ksh, bash):
VAR=someString OTHER_VAR=I have whitespace Note, no whitespace around the =!
set
The set command with no args prints out a list of all the shell variables. Some bash options
noclobber won't let re-direct overwrite an existing file ignoreeof Shell won't exit on ctrl-D vi use vi-style interface -n dry-run (just parse, but don't execute). Handy for scripts
Input Redirection
The shell can attach things other than your keyboard to standard input.
A file (the contents of the file are fed to a program as if you typed it). A pipe (the output of another program is fed as input as if you typed it).
Output Redirection
The shell can attach things other than your screen to standard output (or stderr).
A file (the output of a program is stored in file). A pipe (the output of a program is fed as input to another program).
Redirecting stdout
Use > after a command (and its arguments) to send output to a file: ls > lsout
if lsout previously existed it will be truncated (gone), unless noclobber is set (see bash)
Redirecting stdin
To tell the shell to get standard input from a file, use the < character: sort < nums The command above would sort the lines in the file nums and send the result to stdout.
Appending Output
Use >> to append append output to a file:
ls /etc >> foo ls /usr >> foo
Redirecting stderr
stderr is file descriptor 2, so:
gcc buggy.c 2> error.log grep [Vv]era *.html > log 2> errorlog
Or
find . -name 'core*' 2> core.lis
prog2
STDIN
Process Control
Processes are run in a subshell (by default) Subshells inherit exported variables Each 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 bash 22251 pts/4 00:00:00 vim 22300 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
Background jobs
If you follow a command line with "&", the shell will run the job in the background.
you don't need to wait for the job to complete, you can type in a new command right away. you can have a bunch of jobs running at once. you can do all this within a single terminal (window). ls -lR > saved_ls &
Listing jobs
The command jobs will list all background jobs:
> jobs [1] Running > ls -lR > saved_ls &
The shell assigns a number to each job (this one is job number 1).
Suspend means the job is stopped, but not dead. The job will show up in the jobs output.
You give fg a job number (as reported by the jobs command) preceeded by a %.
Killing a job
Kill may also take a job number or even a job name, introduced by %:
$ find . name core\* -print > corefiles & $ firefox& $ jobs [1]+ Running find . name [2]+ Running firefox $ kill %2
Editors
A 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
filters
Programs that read some input (but dont 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