+ All Categories
Home > Documents > 1-unix.ppt

1-unix.ppt

Date post: 14-Apr-2018
Category:
Upload: rajinder-sanwal
View: 215 times
Download: 0 times
Share this document with a friend

of 90

Transcript
  • 7/30/2019 1-unix.ppt

    1/90

    1

    History of Unix 1970-1974, Simple is beautiful

    early stage of AT&T Bell Lab, on PDP-11 machine Unix is not an acronym, but a weak pun on MULTICS

    1976, first licensed release, Version 6

    1978, first portable version, Version 7

    1979, Berkeley 3BSD

    1983, System V as industry standard

    1984, Microsoft releases Xenix

    1986, BSD 4.3, AT&T Version 9

    1987, SVR4, Mach,

    1993, Linux

  • 7/30/2019 1-unix.ppt

    2/90

    2

    Unix Philosophy

    Easy to program by combining small building blocks

    Not (naive) user friendly

    A clean minimal design (nothing extra, nothing unnecessary)

    Open access:

    Provide tools and mechanism to combining the tools Minimal restrictions to the ways of doing things

    A user can be very creative (and frustrated).

    Major Unix OS features:

    Kernel

    Shell

    File System

  • 7/30/2019 1-unix.ppt

    3/90

    3

    Unix Operating System Structure

    OS mediates between the user and the computer

    User

    Application Programs

    Kernel

    Hardware

    Shell

  • 7/30/2019 1-unix.ppt

    4/90

    4

    Kernel

    Manages memory and allocates it to each process

    Schedules work done by the CPU

    Organizes transfer of data from one part of machine toanother

    Accepts instructions from shell and carries them out Enforces access permission on the file system

  • 7/30/2019 1-unix.ppt

    5/90

    5

    Shell

    Command interpreter

    Create customized environments

    Write shell scripts

    Define command aliases

    Manipulate command history

    File and command completion

    Edit the command line

  • 7/30/2019 1-unix.ppt

    6/90

    6

    File System

    Logical method for organizing and storing large amounts of

    information. Easy to manage.

    File: basic storage unit.

    Types: ordinary file (stores info)

    directory (holds other files and directories)

    special file (represents physical devices like printers, terminals, etc)

    pipe (temporary file for command linkage)

  • 7/30/2019 1-unix.ppt

    7/90

    7

    UNIX: Multi-user Multi-tasking

    More than one user can run at the same time and more than

    one task can run at the same time Unix is multiuser multitasking, Window NT is, Windows is not.

    In Unix, each program is started as a process.

    A process is a program in execution. Usually only one copy of a program, but there may be many

    processes running the same program.

    To each interactive user (window): only one process in foreground may have several processes in background

  • 7/30/2019 1-unix.ppt

    8/90

    8

    httpd

    lpd

    Processes

    /etc/init

    kernel Process 0: Kernel bootstrap. Start process 1.

    Process 1: create processes to allow login.

    inetd/etc/getty

    fork

    exec

    /bin/login

    exec

    shell

    exec

    /etc/getty

    fork

    exec

    /bin/login

    exec

    shell

    exec

    condition terminal for login

    check password

    command interpreter

    kernal modeuser mode

  • 7/30/2019 1-unix.ppt

    9/90

    9

    Unix Process Init process

    last step in booting procedure create other processes to allow the users to login

    Getty process

    conditions for terminal connection

    wait for user-id display login on the screen

    Login process

    check password with the uid

    execute .profile or .login (depends on default shell)

    display shell prompt

    Shell process (command line interpreter)

    Shell prompt ($, %)

  • 7/30/2019 1-unix.ppt

    10/90

    10

    UNIX Process

    Process environment

    Process id, parent-process-id, process-group-id

    Opened files

    Working directory

    File creation mask

    User ID, Group ID

    Resource limits

    Environment variables

    Code A child process inherits parents environment.

  • 7/30/2019 1-unix.ppt

    11/90

    11

    Processes

    useps to see the processes that you are running.

    $ ps

    PID TTY TIME CMD

    221 pts/4 4:15 netscape

    201 pts/4 0:05 bash215 pts/4 1:15 emacs-19

    use & to execute a task in background Example: $ sort infile > outfile &

    use ctrl-Z to suspend the foreground task, and then use bg.

    use fgto move a background task to foreground.

  • 7/30/2019 1-unix.ppt

    12/90

    12

    Shell: Command Interpreter

    Bourne Shell: the default shell (sh)

    original unix shell

    does not have interactive features of newer shells

    widely used for writing shell scripts

    standard on Unix System V

    C Shell (csh): available on BSD Unix and most other systems

    with syntax similar to the C language

    with many enhancement over the Bourne shell.

    Korn Shell (ksh): AT&Ts answer to C Shell combine features of Bourne and C shells

    very efficient

    Other shells: tcsh, bash

  • 7/30/2019 1-unix.ppt

    13/90

    13

    Day-to-Day UseCommand Function Meaningcat Display a file conCATenate

    cp Copies a file CoPymv Renames a file or moves it MoVerm Delete files ReMovelpr Sends a file to a printer Line Printerlp (Sys V)ls Lists the contents of a directory LiStchmod Changes the mode of permissions Change MODepwd Shows the current directory Print WorkingDircd Change current directory Change Dirmkdir Create a directory MaKe DIRrmdir Delete a directory ReMove DIR

    ps Shows the processes on the system Process Statusman Shows info. about commands Manualdf Shows file system status Disk Filedu Shows the space allocation of files Disk Utilizationgrep Search for patterns in files

  • 7/30/2019 1-unix.ppt

    14/90

    14

    Standard Command Format command [options] [arguments]

    wc [-c | -m | -C] [-lw] [file]

    stuff in brackets is optional

    boldface words are literals (must be typed as is)

    italicized (or enclosed) words are args (replace appropriately)

    Options modify how the command works

    command [options] [--] [ ]

    options ::= -option white-space options*

    option ::= noargoption* argoption | noargoption+

    noargoption ::= letterargoption ::= letter whitespace string

    $cc -po zap zap.c

  • 7/30/2019 1-unix.ppt

    15/90

    15

    Some Examples

    ls [-alRF] file-list

    a for listing all files including the dot files

    l for long format (file type, permissions, #links, owner, group, etc)

    R for recursive, list subdirectories.

    F for listing directories with a trailing /

    ps []

    List the information about running processes

    Example:

    %ps -el # print the info about all processes (e) in the long format (l)

  • 7/30/2019 1-unix.ppt

    16/90

    16

    On-line Documentation For shell command, system programs, and library functions.

    %man [n] e.g.

    %man wait %man 1 wait

    %man man %man 1 man

    %man -k

    Man(ual) page format

    Name

    SynopsisDescription (options, defaults, detail desc., examples)

    Files

    See Also

    Bugs

  • 7/30/2019 1-unix.ppt

    17/90

    17

    I/O Redirection

    Redirection and Pipe

    > redirects standard output (screen) to a file

    E.g. ls > dirlist

    outfile

    | pipe output of program 1 to input of program 2

    E.g. who | wc

    Or getit < in | check_it | process_it | format_id > out

    >> appends output to a fileE.g. ls -l >> oldfile

    Exercise: find out the definition of tee.

  • 7/30/2019 1-unix.ppt

    18/90

    18

    Sequential vs. Concurrent Process

    Sequential:

    %date

    %ps -ef OR %date; ps -ef; who

    %who

    Concurrent:%pgm1 & prgm2 > file1 & pgm3%make > log &

    %sort +1 pdir; ((pr dir | lpr) & sort +1 local))

  • 7/30/2019 1-unix.ppt

    19/90

    19

    File Name Expansion

    Each shell program recognizes a set of special characters

    called meta characters. The metacharacters are used to create patterns to match

    filenames and command names.

    Bourne and Korn shell meta/wildcard characters * matches any string (including null)

    ? matches any one character

    [a-dA-D] matches any one character in the range

    [!AB123] matches a char not in this range \ escape

    ~ : (not bourne shell) the home dir of the user.

  • 7/30/2019 1-unix.ppt

    20/90

    20

    File Name Expansion

    Assume we have the following files under the current

    directory: 120, 235, 2#5, a.out, c22, c*2, Doc.1, Doc.2,Doc.3, one.c,two.c, three.c

    ls *.c ls c*2ls [a-z]*[!0-9] a.* *.*

    ls ??? cd ~foo

    ls *

  • 7/30/2019 1-unix.ppt

    21/90

    21

    Filters

    Most UNIX utilities are filters

    A filter is a program which reads input (text) from standard input only

    writes output to standard output only

    writes error to standard error only may use temporary files for intermediate results

    Filters can be combined to work together using pipes

    Pipe: takes stdout of one command and uses it as stdin of

    another command

    ls | wc

  • 7/30/2019 1-unix.ppt

    22/90

    22

    Command Alias

    Assign your own name for a command

    Syntax is shell dependentalias ll ls -l C shell

    alias ll=ls -l Korn, Bourne shell

    Displaying the value of an aliasalias ll (displays value)

  • 7/30/2019 1-unix.ppt

    23/90

    23

    Unix File Systems

    File: a collection of data

    Just a sequence of bytes

    no record or block structure required

    Directory

    A file that contains information for files distinction between a directory and a file

    system can alter the contents of a directory

    rooted tree file structure (inverted tree)

    directories can contain both files and other directories info.

  • 7/30/2019 1-unix.ppt

    24/90

    24

    5binbinetc

    Unix File System Road Map

    Special files: /dev/* represents I/O devices.

    /

    /etc /var /bin /tmp /usr /mnt /home

    sue john

    passwd

    hosts... spool admprintermail(in)mail(out)uucp

    messageswtmp

    libinclude

    /dev

  • 7/30/2019 1-unix.ppt

    25/90

    25

    File Systems and the I-nodes

    Each disk drive contains one or more file systems

    Each file system occupies a number of cylinder groups.

    Each file system has a superblock, an i-node table and files

    The superblock is at a fixed location relative to the beginning

    of the file system. It contains a description of the file system. One can find the location of the I-node table thru superblock.

    Each entry of the I-node table is an I-node, which uniquelyrepresents a file in the file system.

    An I-node contains uid, gid, time of creation, modification,

    and access, permissions, location of the file on the disk.

    superblock I-node table file1 file2 free file3 free

  • 7/30/2019 1-unix.ppt

    26/90

    26

    Inodes od -b .

    0000000 4 ; . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \00000020 277 ( . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \00000040 390 = b l a h \0 \0 \0 \0 \0 \0 \0 \0 \0 \0

    0000060

    A filename is a link(links name in directory hierarchy to theinode, thus to the file contents)

  • 7/30/2019 1-unix.ppt

    27/90

    27

    Directories Directory is an ordinary file

    can be read as ordinary files (by any program that reads text) cant be created or written as ordinary files (only system can)

    od - octal dump

    cat foo

    hi therewelcome to unix

    od -c myfile (output in decimal by byte pairs)

    0000000 h i t h e r e \n w e l c o m e0000020 t o u n i x \n0000031

    1st 7-digits are position in file, ordinal number of next

    character (in octal)

  • 7/30/2019 1-unix.ppt

    28/90

    28

    Symbolic Links

    Can have many links to the same file (inode)

    rm - does not remove inode, removes directory entry (link)

    Only when all links are gone is the file (inode) removed

    ln -command for creating symbolic links ln oldfile newfile (creates another link to the inode)

  • 7/30/2019 1-unix.ppt

    29/90

    29

    Permissions

    Every file has a set of permissions associated with it

    three types of permissions: read ( r), write (w), and execute (x) three sets of permission: user, group, world.

    In Unix system, users are identified by numbers:uid, gid

    ls -l-rwxr-xr-x 1 root 3743 Jan 4 1970 test

    user group others #links owner size (time of last mod) (file name)

    Problem: how do you write a program which allows differentusers to access a set of files? E.g. the program passwd

  • 7/30/2019 1-unix.ppt

    30/90

    30

    Permissions (cont.)

    Solution: use the set-uid bit

    When a user execute a program with the set-uid bit set, theuser assume the identity of the owner of the program.

    For examplels -l /bin/passwd

    -rwsr-xr-x 1 root 8454 Jan 4 1994 /bin/passwd

    set-uid

    Set-uid bit may break the security wall. (users can run the/bin/passwd and act like root)

    Only special programs can be set-uid program, particularly ifthe owner is root.

  • 7/30/2019 1-unix.ppt

    31/90

    31

    Chmod

    Change the access permissions of a file

    chmod permissions can be specified as 3 octal digits, ,

    the three bits of an octal means r,w,x Example: chmod 755 test

    permissions can be specified as +x, or u+x, or g+r,

    chmod +s test sets the set-uid bit for file test.

    If a directory has x in its permision, the dir is searchable, ie.,

    one can do ls on the directory.

  • 7/30/2019 1-unix.ppt

    32/90

    32

    Pathnames

    Every file and directory in the file system can be identified by

    a full path name (route from root to file)/home/sue/email/f1

    Relative path name

    location relative to current directory. Current directory.. Parent directory

    if cwd is /home/sue:ls email

    ls ./email

    cd ..

    ls ../fred

    fred

    /

    home

    sue

    docs email

    f1 f2

  • 7/30/2019 1-unix.ppt

    33/90

    33

    Mounting File Systems

    A file system must be mounted before it can be used

    Root file system is mounted at boot time.

    A file system can be mounted to a dir. of another mounted fs.

    Mounting is done in memory

    /

    /etc /var /bin /tmp /usr /mnt /home/dev

    a file system

  • 7/30/2019 1-unix.ppt

    34/90

    34

    Whats Next?

    Shell scripts!

    But first, some details

  • 7/30/2019 1-unix.ppt

    35/90

    35

    Some Details

    cp [-ir] file1 file2

    cp [-ir] file-list directory i for interactive. prompt use whenever a file will be overwritten

    r for recursive. copy a directory tree

    ls [-alRF] file-list a for listing all files including the dot files

    l for long format

    R for recrusive. list the all subdirectories.

    F for listing directories with a trailing /

    date [+format]

    %date +%h %d, 19%y

    Oct 1, 1996

  • 7/30/2019 1-unix.ppt

    36/90

    36

    Some Details (cont.) wc file-list

    display the number of lines, words and charaters

    more file-list

    Browse through text files on page at a time.

    head [-n ] file-list

    Display the first n lines of the files (default=10)

    tail [+n|-n| -f| ]

    Display the last few lines in the files (default = 10)

    Example:%tail +5 foo # display the last parf of foo starting from line 5

    %tail -5 foo # display the last five lines of foo

    %tail +30 foo | head -15 | more #display line 30-45 of foo

    %tail -f foo # wait and display the new lines appended to foo

  • 7/30/2019 1-unix.ppt

    37/90

    37

    Some Details (cont.) cut -c list file

    cut -f list [-dChar] file Cut out selected charaters or fields from each line of a file

    Examples:

    %cut -c 1-5,15-20 foo

    # extract chars 1-5 and 5-20 from each line of foo.%cut -f 1,3 -d moo # extract field 1 and 3 from each line of moo.

    paste file1 file2

    Concatenate corresponding lines of the given input files Example (reverse two fields of the file abc)

    %cut -f1 abc > abc1

    %cut -f2 abc > abc2

    %paste abc2 abc1 > xyz

  • 7/30/2019 1-unix.ppt

    38/90

    38

    Some Details (cont.)

    grep, egrep, fgrep

    grep [-nv...] pattern [file-list] Search the input text files for lines matcing the pattern

    %grep Unix doc.1 # Display the lines in doc.1 that contains Unix

    %grep -n Unix doc.* # Display the lines with line numbers%grep -v Unix doc.1 # Display the lines which do not contain Unix

    sort [-tC] [-o outfile] [field-list] [file-list]

    sort the files

    %sort +1 list1 # sort list 1 starting from field 2 to the end of the line%sort +2-3 list2 # sort list2 on the third field

    %sort -n -o list4 list3 sort list3 numerically and place the output in list4

  • 7/30/2019 1-unix.ppt

    39/90

    39

    diff

    diff file1 file 2

    Display different lines that are found when comparing two files It prints a message that users ed-lide notation (a - append, c -

    change, d -delete) to describe how a group of lines has changed.

    It also describes what changes need to be made to the first file to

    make it the same as the second file. Example

    file1 file2 file3

    apples apples oranges

    oranges oranges bananas

    bananas kumquats kiwis

    peaches

  • 7/30/2019 1-unix.ppt

    40/90

    40

    diff (cont.)

    %diff file1 file2

    3c3kumquats

    %diff file1 file3

    1d0

    kiwis

    >peaches

    fil 1 fil 2

  • 7/30/2019 1-unix.ppt

    41/90

    41

    comm file1 file2 Takes two sorted text fiels and print common lines and lines which

    appear exclusively in one file on separate colmns.

    column1: lines only in file1, column 2: lines only in file2; col 3:comm

    Example

    file1 file2 %comm file1 file2

    apple apple applebanana cantaloup banana

    grape grade cantaloup

    orange kiwi grape

    lemon kiwi

    %comm -12 file1 file2

    apple

    grape

  • 7/30/2019 1-unix.ppt

    42/90

    42

    tr [-csd] pattern1 pattern2

    Translate input character to output character based on the input

    and output patterns Example

    %tr [A-Z] [a-z] out

    # xlate all letters to lower case.

    %tr -s \012\011\040 \012\012\012 < in > out# xlate blank, tab and newline chars to newline chars and

    squeeze (-s) consecutive newline char into one

    %tr -cs [a-z][A-Z] [\012*] < in > out

    # change all non-aplphbetic (-c) chars to newline chars andsqueeze consecutive newlne char into one.

    %tr -d \040 < in > out

    # delete all blanks.

  • 7/30/2019 1-unix.ppt

    43/90

    43

    uniq [-cdu] file-list

    Display a fiel, removing all successive repeated lines

    Example:file1: %uniq file1

    apple apple

    banana banana

    banana apple

    apple

    banana

    %sort fruit | uniq -capple 2

    banana 3

    %tr -cs [a-z][A-Z] [\012*] < fileA | sort | uniq

    # show a list of distinct words in fileA.

    f

  • 7/30/2019 1-unix.ppt

    44/90

    44

    find

    Recursively search the directory tree rooted at and

    find all files whose names satisfy There are many details in the expression.

    Examples:

    %find . -name \*.doc -print # list all files names ending with .doc

    %find /etc/source -atime 2 -print# print the names of the files under /etc/source whose lst accesstime was 2 days ago.

    %find . -name [a-z]* -exec rm {} \;

    # remove the files under the curent directory whose names beginwith a lower case letter.

    %find / \(-name a.out -o -name *.o \) -atime +7 -exec {} \;

    # remove the object and binary executable files under the rootdireory which have not be accessed more than 7 days.

    i i[ d ] [ B ]

  • 7/30/2019 1-unix.ppt

    45/90

    45

    cpio -i[cdv] -o[cBv]

    System V file archive and backup progam

    Example%find proj -print | cpio -ocBv > /dev/rmt8

    # cpio get file names from stdin. -o create archive which isredirected to the tape device.

    %find proj -print | cpio -ocBv > proj.cpi

    # get file name from stdin and -o createsarchive which isredirected to proj.cpio

    %cpio -icdv *.c

  • 7/30/2019 1-unix.ppt

    46/90

    46

    tar [options] [file-list] key := c (create) | t (table of content) |r (append the file) | u (update

    the file)

    options := v (verbose) | b (block) | f (file name follows) | m (useextraction time as the mod file)

    Create/extracting archive files for backup and transporting files

    %tar cvf proj.tar proj # create archive file proj.tar from file or dir proj

    %tar xvf proj.tar # extract files in proj.tar

    % tar tf proj.tar # list of the filenames in proj.tar without extractingdata.

    %tar cf - proj | (cd /newproj/; tar xvpf -) # copy proj to the directory/newproj/. p to keep all the protection mode.

    cp -r copies a dir tree but all the time info is gone. Tar preserve thetime info.

    %tar cbf 20 proj.tar /usr/local/proj # avoid using full path names.When ou extract the file, tar will insist to ut fiels to /usr/local/ ro .

    d & d d

  • 7/30/2019 1-unix.ppt

    47/90

    47

    uuencode & uudecode

    Generate an ASCII encoded version of the give files

    Example:%uuencode file.bin newfile.bin > file.bin.uu

    # encode file.bin and put the result in file.bin.uu

    %uudecode file.bin.uu

    # decode the file file.bin.uu and generate a new file newfile.bin

    Sending a dir tree via email

    %tar cvf proj.tar proj

    %compress proj.tar # compress proj.tar to proj.tar.Z%uuencode proj.tar.Z proj.tar.Z | mail qli

    at the receiving end, extract the mail and save it in xx

    %uudecode xx

    %zcat proj.tar.Z | %tar xvf -

    d [ ] | [ ] d d fil li t

  • 7/30/2019 1-unix.ppt

    48/90

    48

    sed [-n] | [-e] sed_cmd file_lists

    A stream editor. It copies the lines from the file_list or stdin to

    stdout, editing the lines in the process. Examples:

    %sed -n /hello/p < input > output

    # copy the lines contains hello. -n suppress stdout so only the

    lines that matches are copied.

    %sed 5,7d file1 # delete lines 5 to 7 from file1. File1 is unchanged.

    %sed s/Unix/UNIX/ doc2

    #replace the first occurrence of Unix in each line by UNIX.

    %sed s/Unix/UNIX/g doc2

    # replace all Unix by UNIX

    k [ f fil ] [ F ] [ ] [fil ]

  • 7/30/2019 1-unix.ppt

    49/90

    49

    awk [-f progfile] [-Fc] [prog] [files]

    Pattern matching and stream editor.

    Example: Program awkexample:

    BEGIN { linetype=0} # initialization

    NR == 1 { print $1 $NF} # if it is the first line, print the last field

    /^$/ { print This is an empty line }

    /^Unix/ { printf(Line starts with Unix\n %s\n, $0); linetype=1; next;}

    /NonUnix$/ { printf(End with NonUnix\n); linetype=0; next;}

    linetype == 1 { print $0}END {printf(%d lines processed\n, NR);} # finishing it up

    k ( t )

  • 7/30/2019 1-unix.ppt

    50/90

    50

    awk (cont.)

    Test data file (awktest):

    Line1-field1 this is the las-fieldThis line should not show

    Unix is simple and difficult

    Hello world is very simple

    Next blank line should show

    I line some other NonUnix

    This line should not show

    Bye

    k ( t )

  • 7/30/2019 1-unix.ppt

    51/90

    51

    awk (cont.)

    %awk -F -f awkexample awktest

    Line1-field1 las-fieldThis is an empty line

    Line starts with Unix Unix is simple and difficult

    Hello world is very simple

    Next blank line should show

    This is an empty line

    End with NonUnix

    This is an empty line

    11 lines processed

    Command line program: most of the awk commands can be used inhe command line.

    %awk {print $2 $1} # exchange field1 and field2

    Oth C d

  • 7/30/2019 1-unix.ppt

    52/90

    52

    Other Commands chown [-R]

    Only owner and root can chown. (Root only in somesystems.Why?)

    Example:

    %chown -R john testdir

    # john becomes the owner of all files under testdir. kill [-signal] pid

    Sending the specified singal to the process. The process can beprogrammed to catch most of the signals.

    Examples:%kill -1 1 # poke the init process to reinitialize (reading /etc/ttytab)

    %kill -9 1234 # kill process 1234

    Signals can be spefified by name, e.g., HUP is 1, KILL is 9.

    %kill -l # lists signal names

    Oth C d ( t )

  • 7/30/2019 1-unix.ppt

    53/90

    53

    Other Commands (cont.)

    du [-as.] [dir list] [file list]

    Reports the allocated dispace for each file and/or directoryspecified

    -a lists all files, -s lists the grand total of each dir given

    Examples:

    %du -s # print the total disk space used by the files in and under thecurrent dir.

    %du -s * # print the disk space used by each file and dir in the currentdir.

    k

  • 7/30/2019 1-unix.ppt

    54/90

    54

    make

    A tool for maintaining programming projects

    make [-isrntqpd] [-f file] [macro-definition] [targets] It allows the users to specify dependencies among different source

    and binary files in his/her applications.

    -i ignore error code returned by a command

    -s silent mode

    -r suppress built-in rules

    -n no execute mode

    -t touch target file -q question before change

    -p printout macro definitions and target descriptions

    -d debug mode

    -f alernative make file name

    make (cont )

  • 7/30/2019 1-unix.ppt

    55/90

    55

    make (cont.)

    Makefile:

    prog: x.o y.o z.occ -o prog x.o y.o z.o -lm

    x.o: x.c def.h

    cc -c x.c

    y.o:y.c def.h

    cc -c y.c

    z.o:z.c

    cc -c z.c

    make does depth-first search on the dependency graph

    prog

    z.c

    y.c

    def.h

    x.c

    x.o

    y.o

    z.o

    Makefile format

  • 7/30/2019 1-unix.ppt

    56/90

    56

    Makefile format

    a makefile containing explicit dependency lines in the

    following format:target1 [target2 ]: [dependency ] [; commands] [#commnets]

    [ commands] [#comments]

    each command line start with a tab character, and no other lines

    should start with a tab.

    commands are bourne shell commands

    a set of built-in rules are used by make, e.g.:

    .c.o: $(CC) $(CFLAGS) -c $ file direct stdout to file

    >> prog>>file append stdout to file< prog

  • 7/30/2019 1-unix.ppt

    66/90

    66

    Shell Metacharacters`` run commands the output of produces

    () run commands in in a sub-shell$1, $2 arguments to shell file

    $var value of shell variable var

    \ \c take c literally (dont evaluate)

    take literally

    take literally, after $, ``, \ interpreted

    # comment

    var=val assign variable var

    p1 && p2 run p1, if successful, run p2

    p1 || p2 run p1, if unsuccessful, run p2

    Whats going on?

  • 7/30/2019 1-unix.ppt

    67/90

    67

    What s going on?

    What processes, programs, pipes, and files are used?

    %cat doc1 > out1; wc -l OUT2 ; cat OUT &

    %cat doc1 | wc -l

    Whats going on?

  • 7/30/2019 1-unix.ppt

    68/90

    68

    What s going on?

    % date ; who | wc

    Wed Sep 24 16:00:00 PDT 19974 24 182

    | higher precedence than ;

    % (date ; who ) |wc

    5 30 211

    Whats Next?

  • 7/30/2019 1-unix.ppt

    69/90

    69

    What s Next?

    Shell scripts!

    Guidelines for writing Unix

  • 7/30/2019 1-unix.ppt

    70/90

    70

    gCommands/Tools/Scripts

    Standard command format

    Recognize meta-characters (handle multiple files)

    Standard I/O (stdin,stdout, stderr. If file arg is absent use std)

    Keep messages and prompts to a minimum.

    Provide verbose options Input/output data should be text whenever possible.

    Use dot files and environment variables for frequently used

    info. Use standard library and tools to save coding effort.

    Shell Script

  • 7/30/2019 1-unix.ppt

    71/90

    71

    Shell Script

    Bourne Shell/Korn Shell

    Topics: pass arguments

    global and local variables

    macro

    functions

    Invoking a shell script

    $shell_script_file or $sh -options shell_script_file

    the script file must have execute-permission.

    Environment Variables

  • 7/30/2019 1-unix.ppt

    72/90

    72

    Environment Variables

    .profile

    PATH=.:$HOME/bin:/bin:/usr/bin:/usr/local

    TERM=vt100

    export TERM PATH

    - HOME -- home directory

    PATH -- where to look for commands

    TERM -- terminal type for programs such as the editor

    CDPATH -- shortcut directories for cd command

    PS1 PS2 -- prompt

    A shell script example

  • 7/30/2019 1-unix.ppt

    73/90

    73

    A shell script example

    The script file loginfo (must be executable):

    echo The current date and time: \cdate

    echo The number of users: \c

    who | wc -l # count number of lines

    echo personal info: \c);who am i

    exit 0

    $chmod +x loginfo

    $loginfo

    The current date and time: Thursday October 10

    The number of users: 7

    Personal info: lseiter ttya .

    Another Example

  • 7/30/2019 1-unix.ppt

    74/90

    74

    Another Example

    $cat shellvariables

    #! /bin/sh# show predefined shell variables

    echo The number of arguments is $#

    date &

    echo The process id of date command is $!wait

    echo The process id of this shell is $$

    grep root /etc/passwd

    echo The return code from grep is $?

    echo The current set of options is $-

    $shellvariables one 2 xyz

    $sh -a shellvariables one 2 xyz

    Positional parameters

  • 7/30/2019 1-unix.ppt

    75/90

    75

    Positional parameters

    set to shell script arguments. e.g.

    $my_script a b xy 1 2 3 positional parameters have the values

    $0 -- my_script

    $1 -- a

    $2 -- b$3 -- xy

    $4 -- 1 2 3

    shift command:

    $shift [n]

    # parambers $n+1 $n+2 become $1 $2 .

    # default value of n is 1

    Using Shell Variables

  • 7/30/2019 1-unix.ppt

    76/90

    76

    Using Shell Variables

    Symbolic Variables

    parameter = valueExamples:

    I=5; Y=$I

    # There should be no space around the = sign

    # More than one command can be in a line separated by ;x = hello there\n; y=Hello

    echo $x

    dir1=/usr/bob/doc

    ls -a $dir1

    echo x = $x

    Quoting

  • 7/30/2019 1-unix.ppt

    77/90

    77

    Quoting

    string take string literally

    $echo * $HOME* $HOME

    string take string literally, except $,`,\,,

    \c take c literally

    Quoting & Compound Command

  • 7/30/2019 1-unix.ppt

    78/90

    78

    Quoting & Compound Command

    The Quotes from Hell

    This is a string\nThe amount is $100.0

    quotes all char except \ $ `

    quotes all char except

    command substitution (grave accent):$echo Users currently on the system:\n `who`

    $echo The banner command,\n `banner the banner`

    Compound commands: a pipeline, a list, a group (), a command that begins with a certainreserved words: for, if, case, time,

    I/O redirection applies to the complete command except a pipeline,

    a list and the time command.

    test Command or [ ]

  • 7/30/2019 1-unix.ppt

    79/90

    79

    test Command or [ ]

    if test $# -eq 0 if [ $# -eq 0 ]

    then thenecho no positional param! echo no positional param!

    fi fi

    options:-r | -w | -x | -f | -d file

    # the file is readable, writeable, executable, a file, or a directory.

    n1 -eq | -ne | -gt | -ge | -lt | -le n2

    # n1 = | | > | >= | < |

  • 7/30/2019 1-unix.ppt

    80/90

    80

    Test Command

    Options

    File testing Numerical comparison String Comparison-r file n1 -eq n2 str1 = str2-w file n1 -ne n2 str1 != str2-x file n1 -gt n2 str-f file n1 -ge n2

    -d file n1 -lt n2n1 -le n

    Logical Connectives

    !-a-o

    Parameter substitution

  • 7/30/2019 1-unix.ppt

    81/90

    81

    Parameter substitution

    Parameter substitution

    ${param}${param:-word}

    value of param if defined, otherwise word,param remains undefined

    ${param:=word}value of param if defined, otherwise word,if param undefined, param defined to word

    ${param:?word}

    if defined, param, otherwise print word and exit shell${param:+word}

    word if param defined, otherwise nothing

    Commands and Functions

  • 7/30/2019 1-unix.ppt

    82/90

    82

    Commands and Functions

    Reserved word commands

    [[ test_expression]] if, case, for, select, while, until

    command grouping:

    (command_list)

    {command_list}

    Function definition

    function id

    {compound_list

    }

    CX command

  • 7/30/2019 1-unix.ppt

    83/90

    83

    CX command

    Suppose you want a command cx that will take a filename and

    set its execute permission. For example,$cx foo

    Need to be able to get the filename from the command line

    Shell variables: $1 (first argument), $2 (second), $* (all args)

    chmod +x $1

    Flow Control

  • 7/30/2019 1-unix.ppt

    84/90

    84

    Flow Control

    General Format:

    if command_list1 check exit code of the commandthen 0 -> normal termination -> True

    command_list2 non-0 -> abnormal termination -> False

    elif command_list3

    then if test -d /usr; then echo its a dir; fi

    command_list4

    . $if test -d /dir

    else >then

    command_listn > echo its a dir

    fi >fi

    >$

    Looping

  • 7/30/2019 1-unix.ppt

    85/90

    85

    Looping

    What does this command do?

    $wc -l *

    6 file110 file2

    3 file319 total

    What if you would like things formatted nicely?There are 6 lines in file1There are 10 lines in file2There are 3 lines in file3

    Loops

  • 7/30/2019 1-unix.ppt

    86/90

    86

    Loops

    General Format:

    while command(s) check return code of command,do if 0, execute body and loop again

    body

    done

    while test ! -s file1

    do

    echo file1 does not exist or is still empty

    sleep 500

    done

    Loops

  • 7/30/2019 1-unix.ppt

    87/90

    87

    p

    General Format:

    until commanddo

    body

    done

    until who | grep $1

    do

    echo $1 has not logged in yet

    sleep 500

    done

    Shell scripts

  • 7/30/2019 1-unix.ppt

    88/90

    88

    p

    for var in listofwords

    docommands

    done

    for f in $*do

    x=`wc -l $f`

    echo There are `echo $x |cut -f1 -d ` lines in $fdone

    Examples

  • 7/30/2019 1-unix.ppt

    89/90

    89

    p

    if test ! -f $1

    thenecho First arg is not a file

    if

    if [$NAME = John Doe -a $BAL -gt 5000]then

    echo ok

    elseecho not okfi

    Examples

  • 7/30/2019 1-unix.ppt

    90/90

    p

    if test -f $1

    thenecho First arg is a file

    elif test -d $1then

    echo First arg is a directoryfi


Recommended