Lecture 2

Agenda

       1. Redirection, Pipes, Scripts,  I/O copies
       2. Files and Directories
       3. Processes
       4. Shells

Redirection:

     $ cat myfile
    hello
     there

   $ cat
    abc
     abc
    def
     def
    control-D

     < input        > output      >> append

      $ cat <myfile
     hello
      there

     $ date >xyz

      $ cat >xyz
      have a nice
       day
       control-D

       $ cat myfile xyz
      hello
       there
       have a nice
       day

      $ cat <myfile >def

      $ cat myfile >def

       Q) Create a file called theusers containing a list of those users currently logged in.

       A)

     $  cat myfile nofile
    hello
     there
     cat: nofile: No such file or directory

      Q) $ cat myfile nofile 2>error 1>output

           $ cat output
 

           $ cat error

      $ more /usr/dict/words

    Space next page    b prev page   Ret scroll forward one line
    ? print page of help  q quit

    Pipes

      $ X >  tempfile          equivalent to          $ X | Y
      $ Y <  tempfile

     A pipe can join 2 streams together.
 

     Q) Send email msg to sam which will inform sam of current time and date??
     date send to stdout the time and date and mailx sends a msg from input stream.

     $ date | mailx -s "Todays time and date" sam
 

     Scripts

     Create a file whenandwho with following contents:
     date
     who

     $ sh whenandwho

     $ echo Hello there (Useful for writing scripts)
     Hello there

     Q) A script called niceday which will output today's time and day with a user friendly message.
     The time and date are now:

     $ cat >niceday
    echo The time and date are now:
     date
     echo Have a nice day!
     control-D

     $ sh niceday
     The time and date are now:
     Thu Jan  7  11:17:05 EST 1999
     Have a nice day!

     Script can have redirections and pipes

     Q) Mail sam a message which says running my script now

     $ echo running my script now  | mailx -s "What I'm doing" sam

    Making copies of input/output

     Suppose u want to print something to the screen and save in a file

     $ tee "copy_of_input" | "command_name"
      This command has the same effect as command_name, except a copy of standard input
      is sent to copy_the_input.

     Q) Send user jo and keep a copy of the message in file jo_message.
     $ tee jo_message | mailx jo
     hi,
     this is a test.
     ^D

     $ cat jo_message

      To use tee to copy standard output, pipe the output of a command through tee.
     Q) Run the command who, but store a copy of the output in file who_out
      $ who| tee who_out
 

     Getting Help

     $ man who
      Name, Synopsis, Description, Option

    Q) Find out how to print the current h/w type on which your system is running by using uname
    $ man uname and -m option does it

    Q) To find command which will print out just your login name
     $ man -k  login
     logname



 

    Chapter 3: Files

     $ pwd                      (Current location within file system)

     $ cd /usr/bin

     $ cd /squiggle

     $ ls /                           ( lists files in root)

     $ cd /bin

     $ ls

     Q) What files does sam have in his home directory?
      $ ls ~sam

     Filesystems

     $ df    (Discover the amount of free disk space and information on which filesystems are set up on system)
 

     Manipulating files

     $ mkdir dir1
     $ ls

     $ cp myfile foo       (creates new inode)

     $ mv myfile bar

     $ ls

     $ ln /cs/grad/sam/datafile samsdata (links -s  )

     $ ls -i
     total 561
      241563  myfile            43532 dir1       562241 foo       562241 bar

    Q) What is the inode of your home directory?
           $ cd
           $ ls -i
           $ ls -id

File Access Control

Each file divides the users of the machine into 3 categories : owner, group, other
Access Priveleges: read, write, execute

$ ls -l

$ ls -R dir                                            (Lists all subdirectories recursively)

Wildcards: * matches any character, ? matches any single character. , [...] matches any character in the encoded list or range

$ ls *.txt
udil.txt wais_www.txt

$ ls letter?.*
letter1.txt lettera.com

$ ls report_[a-c]
report_A report_b report_c

$ chmod go-w myfile

$ chmod u+x myfile

$ chmod g=r-x myfile

Q) Create a file which no-one can read and confirm that you yourself cannot read it.

File Contents

What does the file contain? c program, library, text??

$ file myfile

Q) What type of file is /usr?

$ head -n 3 myfile
$ tail -n 4 myfile
$ wc myfile

file 1: A                                        file 2: A
          test                                               testing
          file                                                file

$ diff file1 file2
2c2
< test
--
> testing

$ uniq A (To find out how often repetitions occur or to filter out the repetitions)
(Check out  options)

$ paste A B      (Concat 2 files so that output is like table  columns)

17.04.61  Smith      Fred
22.01.63  Jones      Susan
03.11.62   Bloggs    Zach

$ cut -c7-8 myfile
61
63
62

$ cut -f2 -d' ' myfile          (with delimiter option)
Smith
Jones
Bloggs

$ sort A

$ sort A | uniq         (Filter out repeated lines and prints in sorted order)

Q) Find out how many separate inodes are represented by the files(excluding 'dot' files)
$ ls -il | cut -c1-6 | sort -u | wc -l

$ lp -n 2 -d def myfile                       (line printer)

$ pr -2 -l 50 abc | lp -d def                 (stdout, columns, lines)

$ du       (will print the number of kilobytes used to store the data in the files in that directory)


Chapter 4: Processes and Devices

Process: Machine code program (suspended/running)

$ ps
$ date &
$ date
$ sleep 15
$ date
$ sleep 100 &
$ ps

$ kill -s KILL "pid"

Job: Collection of processes grouped together and identified by a small integer

$ jobs

$ cat testfile | wc &                       (2 processes, process # of wc)
[2] 2374

Q) Arrange to check mail in 2 minutes.
       $ cat >mymailer
        sleep 120
        echo Checking mail
        mailx
        control-D
      $ sh mymailer&

Default job: If jobs are running or stopped, one of them is default and indicated by +(Also %%). It is the job most recently created.

      $ bg %1                 (Any stopped job can be reactivated to run in the background)
       [1]+ testA &

      $ fg %%

Command History list:

$ fc -l        (Lists the commands u have already run)
$ fc 2 4
$ fc -s 3  (rerun commands)

Homework:  at, batch, nice, nohup, time.

Environment

A collection of variable names together with an associated value for each one.

$ echo LOGNAME
LOGNAME
$ echo $LOGNAME

$ ADDRESS="9250 Verree Road"
$ echo I live at $ ADDRESS
 I live at  9250 Verree Road

EG: EDITOR, HOME, PATH, PRINTER, PS1, PS2, SHELL, TERM

$ echo PATH (When UNIX encounters a command not built in the shell, it looks at PATH, which is a sequence of pathnames. Each path is checked to  ee if the executable is present)

$ env | more (list all variables)

$ PS1="type in a command:"    (PS1 controls the prompt that he shell gives u)
type in a command: echo $PS1
type in a command:

Homework: Global & Local variables

Executable Scripts

$ sh myfile
or
$ chmod a+x myfile
$ ./myfile or $HOME/myfile

Examine the value of your PATH     $echo $PATH

If there is a dot in your path, then $ myfile should work

Q) Find out the name of the terminal u are using
$ echo $TERM

Q) Change your prompt from $ to "enter command"
$ PS1 ="enter command"
enter command: export PS1

Q) Write a script which will prompt the user for the name of a file and then print the file on their terminal
$ echo Type in a filename

$ read FILENAME
$ cat $FILENAME

Q)Update PATH do that it includes the subdirectory MYbin of your home dir
$ PATH=$PATH:$HOME/MYbin
$ export PATH

Q) Write a command changeprompt which will request you to enter a new shell prompt and will then reset it to its new value
$ echo Type in new prompt:
$ read PS1
$ export PS1

Q) Write a script which will read in the name of a file and display a message only if it
cannot be read
$echo Type the name of the file
read FILENAME
cat $FILENAME > /dev/null

Q) Reset your shell prompt to the name of the shell followed by a > symbol.
$ echo $SHELL
$ PS1=$( basename $SHELL)">"
sh>