Lecture 3

Tar,PAX, Compress, Uncompress

Copy of a complete directory for backup or to send it to a different computer system.
cp has 2 problems:
  1. Different O.S's and machines will store data in different formats. It may be necessary to convert the format in which the files are stored.
  2. cp does not handle links
tar(tape archive) and cpio(copy in out) historically used. Both work by copying all files in the directory, together with data describing the structure of the directory, into a single file called archive. Unfortunately, both tar and cpio work differently and produce archives in different formats. So functionality of both combined in pax.

Neither tar nor cpio is part of POSIX, but a new command pax has been written.

To create a new archive, use pax -w or tar -c. The archive file will be sent to STDOUT.

To archive the contents of the current dir to the tape drive /dev/rst8, use
$ tar -c .  >/dev/rst8      or            $ pax -w .  >/dev/rst8

To extract contents:       $ tar -x dir      or                           $ pax -r dir

To check the contents: $ tar -t dir       or                        $ pax -t dir

Compress the archive(say, mydir.pax) and reduce size using compress. This will create a file
mydir.pax.Z and delete mydir.pax; and it is of smaller size.

$ ls
mydir.pax
$ wc -c mydir.pax
206336
$ compress mydir.pax
$ ls
mydir.pax.Z
$ wc -c mydir.pax.Z
89473

To reverse compression use uncompress.
 

Shells

Command Interpreter, introduce constructs which are a part of the shell and which makes it a programming language in its own right.
    1. Check for success and failure of commands.
    2. Check state of filesystem.
    3. Check values of environment variables.
    4. Process this knowledge.


    It is a  programming language in the full sense -it has the power of other programming languages - but tailored for use in conjunction with an operating system. Designed for user interface rather than numerical computations.

    Comments: Start with a # sign. Info on author, description, date and functionality.
    cat >welcome
    #This prints a welcome message
    echo Hello World
    Control-D

    $ date; sleep 5; date                                           sequential list
    $ date & uname & who &                             asynchronous list

    Utility: Name of the program (wc, date, ls)
    Command : Instruction to the shell to perform a task.

    Types of commands:
    1. Simple command: It is UNIX utility name together with options and arguments, any input or output redirection, and possibly preceded by variable assignments.
    $ date    $ cat 0< inputfile 1> outputfile     $ VAR =42   $ NAME=Chris

    2. Pipeline: It is a sequence of commands separated by the pipe symbol(|);
    $ date      $ who | cut -c1-8 | sort

    $ ls -l /usr/local/bin 2> errorfile | wc -l outputfile

    $ who | VAR=42 mycommand | VAR=99 mycommand
    where mycommand is a script not system utility.

    Exit status : Everytime a UNIX command terminates, that command returns a number, called its exit status, to the shell which caused it to run.
    0 successful      other than 0 if failure
    $ echo $?          provides exit status of precious command.

    Q) What is the exit status of  $mv ~/X   /
    A) Command should fail either because ~/X does not exist or no write permission to root dir.
    $ mv ~/X   /
    an error message
    $ echo $?
    1

    3. List command: It is a sequence of pipelines separated by either || (or list, atleast one 0) or &&(and list, both are 0)
    Based on exit status.
    $ mycommand || echo Cannot run mycommand
    $ mycommand && echo mycommand ran OK

    Q) Compare files named file1 and file2, and if they are different mail yourself a message indicating the differences.
    A) diff 0 exit status only when arg1 and arg2 are identical.
    $ diff file1 file2 >diffout ||  mailx -s "o" araja < diffout

    Q) Compare files named file1 and file2, and if they are identical delete file2
    A) $ cmp file1 file2 && rm file2

    bc: Sophisticated calculator
    $ bc
    1+2
    3
    100/7
    14
    scale=5       (output correct to 5 decimals)
    100/7
    14.28571
    sqrt(2)
    1.41421
    10 * ( 3+4)
    70
    1 + 3*4   {= 1 + (3*4)}
    13

    Q) Use bc to find the number of seconds in a day
    A) $bc
         24*60*60
         86400
         Control-D

    $ echo "1 + 2" | bc
        3

    Q) Write a script which will read in two numbers and display their product.
    A)  cat >myscript
          echo Input two numbers:   #Prompt the user
          read NI N2                              #read in two numbers
          echo "$N1 * $N2" | bc           #pass their product to bc
          Control-D

    test command
    To be able to compare numbers(such as file sizes) and strings(values of environment variables), and to interrogate easily the existence and access permissions of files.

    test arguments     or    [ arguments]
    $ [ -e testfile ] && echo Testfile exists
    $ test -e testfile && echo Testfile exists
    [ "$NAME" ] || echo NAME is unset

    Q) Write a script which will read in the name of a file and print out a message indicating whether or not it is a directory.
    A) cat >script
         echo Input a file name:        #Prompt the user
        read FILENAME                  #input a filename
         ([ -d $FILENAME ] &&       #check its  directory
                                                           # confirm this, if so
           echo $FILENAME is a directory) || echo $FILENAME is not a directory

    Q) Write a script which will greet the person running it if they are logged in as user chris.
    A) [ "$ ( logname )" = chris ] && echo Hello Chris
     

    Grouping commands
    By enclosing a list of commands in parentheses, a new invocation of the shell is formed to execute that list of commands, just as of you had placed those commands in a file and run that file as a separate shell script.

    To add prologue and epilogues

    $  cat <<END > temp                 $  cat <<END | sh | lp                     $ (echo "This is the start"
    echo "This is the start"              echo "This is the start"                 > cat myfile
    cat myfile                                      cat myfile                                          > echo "This is the end" ) | lp
    echo "This is the end"                echo "This is the end"
    END                                                 END
    $ sh temp | lp
     

    Q) Without creating any temperory files, and using a single shell instruct your shell to display the names of files in your current dir, preceded by a message and paged.
    A ) $ (echo "Your files are:"; ls) | more

    'if' statement

    Q)  Write a script which will inform you whether or not you have used more than 100k of disk space
    A)   cat >dsp
        #Evaluate the number of kilobytes of storage used
           KBYTES=$( du -s ~ | cut -f2 -d' ')
        #Check the value of the KBYTES
          if [ $KBYTES -gt 100]
           # and display message if > 100
          then echo "You have used more than 100 K"
           # and display message if <= 100
          else echo "You have used less than 100 K"
           fi
     

    'For loops'

    Q) Send a personalised greeting to each of users jo, sam and george
    A) $ for user in jo sam george
          > do
          > echo "Hello $user" | mailx $user
          > done

    'While' and 'until' loops
    Q) Use a while loop to print out the 'twelve times table'
    1 x 12 = 12
    ....
    A) $i=1
         $ while [ $i -le 12]
         > do
         >    result=$(echo "$i * 12" | bc)
         >    echo "$i x 12 = $result"
          >   i=$( echo "$i + 1" | bc )
          > done
     

    Q) Script which will once a minute check to see whether there is a core file in your home dir and will terminate with a message warning.
    A)
    until [ -f $HOME/core ]
    do
       sleep 60
    done
    echo core file created

    HomeWork: read about break and continue

    Searching for files

    To print the pathnames of all files in your home directory called myfile .
    $ find ~ -name myfile -print

    Q) Remove all files name core from your filespace
    $ find ~ -name core -exec rm {} \;
    execute rm on selected file , where {} is short hand for that file, ; terminates the action to be taken.

    Formatted output

    HomeWork: ReadSection, Similar to formatting in 'C'

    Scripts which take arguments

    $ cat >testfile
    for i in $*
    do
    echo $i
    done

    $testfile jo sam george

    Parameter expansion
    ${variable:-default}

    Q) Create a welcome message which initially checks variable NAME to find out your name; if that is unset, checks LOGNAME, and if LOGNAME is unset uses command logname as a last resort.
    A) $ echo Hello ${NAME:-${LOGNAME"-$(logname)}}
     

    Arithmetic Expansion:   $(( expression ))

    Q) Use a while loop to print out the 'twelve times table'
    1 x 12 = 12
    ....
    A) $i=1
         $ while [ $i -le 12]
         > do
         >    result $(( $i * 12 ))
         >    echo "$i x 12 = $result"
          >   i=$(( $i + 1 ) )
          > done
    Much faster than using bc

    Q) How many directories/files located in the root directory have names which are of three characters?
    A)  $ ls /??? | wc -w

     Q) Print detailed information on all files in the current directory with the .c suffix.
    A) $ ls -l *.c

    Q) List all commands stored in /bin whose names consist of two characters, the second one being a vowel
    A) $ ls /bin/?[aeiouAEIOU]

    cc -o myprogram prog1.o prog2.o