Welcome to UNIX and Shell Scripting workshop
                      BY
                R.Chockalingam

Contact Us
https://www.facebook.com/Karkhadotcom
Ph : 9543111174,9789989552.
UNIX

  Multitasking , Multiuser
 Operating System.
Unix operating systems are
 widely used in servers
 ,workstations and mobile
 devices.
The Unix system is composed of several components
 In addition to the kernel of an operating system – the
 development environment, libraries, documents, and
 the portable, modifiable source-code for all of these
 components
UNIX as an Operating System
All operating systems provide services for programs
 they run
Executing a new program.
Opening a file.
Reading a file.
Allocating a region of memory.
Getting the current time of day etc …
The Kernel and the Utilities

The Unix system is itself logically divided into two
 pieces: the kernel and the utilities.
The kernel is the heart of the UNIX system and
 resides in the computer's memory from the time the
 computer is turned on and booted until the time it is
 shut down.
The utilities, on the other hand, reside on the
 computer's disk and are only brought into memory as
 requested.
Architecture of the UNIX
operating system
Connecting Remotely
 You must connect to a public CS machine.

 Public Machines:

    Linux Machines (all of them are now running a Lucid build):
      juiblex, mig, totoro

 In order to find available cshosts
    Type cshosts pub on the command line for public machines or use the online list:
    http://apps.cs.utexas.edu/unixlabstatus/


 To log in, use a SSH program such as SSH Secure Shell or PuTTY (both
  available from Bevoware).
    https://www.utexas.edu/its/bevoware/download/
    When connecting, enter a machine name as follows: name.cs.utexas.edu
File Types
Two different types of files in UNIX are
1. Regular files and
2. Directories


Regular file. The most common type of file, which
 contains data of some form. There is no distinction to
 the UNIX kernel.
Directory file. A file that contains the names of other
 files and pointers to information on these files. Any
 process that has read permission for a directory file
 can read the contents of the directory, but only the
 kernel can write directly to a directory file.

Block special file. A type of file providing buffered I/O
 access in fixed-size units to devices such as disk
 drives.

Character special file. A type of file providing un
 buffered I/O access in variable-sized units to devices.
 All devices on a system are either block special files or
 character special files.
Symbolic link. A type of file that points to another
 file.

File type macros in <sys/stat.h>
Macro Type of file
S_ISREG() regular file
S_ISDIR() directory file
S_ISCHR() character special file
S_ISBLK() block special file
S_ISFIFO() pipe or FIFO
File System
 The UNIX file system consists of directories and files. Directories
  contain files or other directories. Files can be programs, text
  documents, etc.
    You can think of a directory as a folder in Windows or Macs.
    Directories contain files or other folders, just like typical operating
        systems

 The file system is organized as an inverted tree.
    the directories are branches, and the files are leaves.
    The topmost directory is /, and is called root
      all other directories and files are underneath root.



 All of a user’s files are in a home directory, /u/user
    user is the login name for that account.
    It is abbreviated ~user, mine is ~rivin

 Use the command pwd if you can’t remember which directory you are
  in.
File System Structure
The Unix file system is a hierarchical structure that
  allows users to store information by name. At the top
  of the hierarchy is the root directory, which always
  has the name /

The location of a file in the file system is called its
  path. Since the root directory is at the top of the
  hierarchy, all paths start from /.
Flags and Arguments
 command [flags] [arguments]

 command: The name of the command.
     ls lists the contents of a directory.


 flags: switches that modify the function of the base command. Flags usually begin with a "-”.
     ls –a shows all the files (including hidden files) in the current directory.
     ls –l shows files, in long-listing format, in the current directory.

     Flags can be combined.
         ls –al shows all the files (including configuration files) in long-listing format, in the current directory.
         Flags are swappable, so ls –la will do the same.


 arguments: usually the name of a file or directory to perform operations on.
     ls dirname shows all the files (in normal-listing format) in the directory dirname.


 Flags and Arguments can be combined.
     ls –al dirname shows all the files (including hidden files) in long-listing format, in the directory
        dirname.
Text Editors
 pico:

    A very easy to use text editor (the one that is used inside of alpine by default).


 vi:

    A small, fast, consistent text editor that does not have a windows interface.
    Type man vi for various commands to manipulate files.
    Type :help inside of vi to bring up the help file.


 emacs:

    Feature-rich
    Can run with a text interface from within a terminal shell, or in its own window with
     menus, etc. Type emacs to start the program and then Ctrl-h t to run a tutorial.
    Emacs will run in its own window when running Xwindows. Just type emacs and the
     window will come up.
Basic UNIX Commands
 ls directory: Lists the contents of the directory. If no directory name is given, it will list the
   files in the directory you are in.

 cd directory: Change from current directory to directory. If no name is given, it will go to
   your home directory.

 mkdir dir1: Create a directory named dir1. Multiple directory names can be given.

 cp file1 file2: Make a copy of file1 and name it file2. If file2 already exists, it will be
   overwritten.

 rm file1: Remove the file named file1. Multiple filenames can be given.

 mv file1 file2: Rename file1 as file2
 mv file1 directory2: Move file1 into directory2.
      For example, mv project1 CS310 would move the file project1 into the directory CS310
          The directory CS310 must be created first.


 .snapshot: Grab the most recent version of a file if you accidentally deleted it.
Creating a file
cat>firstfile



To read a file :


 %cat > firstfile
Creating a Directory

Creating directories permits you to organize your
 files. The command
 mkdir project1
Basic Commands
man [section] <command> Look up a command
pwd Display current directory
ls [-al] List directory contents
cd<directory> Change Directory
rm <filename> Delete file
cp <filename> <ending
filename>
Copy file to new location
mv <filename> <ending
filename>
Move file to new location
cat <filename> Display entire file
more <filename> Display file one page at a time
wc [-l|w|c] <filename> Return file statistics: number
 of words, lines, and
ps [ax or ef] List processes running on the system
kill -<signal> < process ID> Send end signal to a
 running process
Editor
Vi
emacs


vi is the classic screen-editing program for Unix
vi test.txt
i Changes to insert mode
or a (after the character under the cursor)
:w Return Save the file
:w<filename> Save the file to a new name
:q Return To exit vi
Command
:q! Return Quit without saving
Insert Esc key Changes to command mode
mode
Backspace and Delete keys Backspaces or deletes, but
 only for data
|       The Pipe
|
(shift-)
takes the output of one command and feeds it to another command.


less is a pager (shows you a text file one page at a time).
cshosts shows you all the hosts in CS (many pages).

cshosts | less
takes the output of cshosts (many pages) and runs less on it.


grep string filename looks for instances of string inside of filename.

cshosts | grep mo
tells you the machine names returned by cshosts that contain instances of “mo”.
More UNIX Commands
X acroread filename.pdf: Read a .pdf file. If no filename is given, it
  will open the program with no initial file.
   - xpdf can also be used.
X gv filename.ps: If you ever get a .ps (postscript) file, use this to view
  it. If no name is given, it will open the program with no initial file.
X gimp filename: View a graphics file (.jpg or .gif).

 chkquota: Check your disk-space usage in megabytes.

 gzip filename: Zip a file to conserve space.
 gunzip filename: Unzip a file that is zipped.
 zip/unzip filename: Winzip compatible

 ps uxw (ps –u user): Check the processes you have running.
 kill 14083: Kill the process with PID# 14083.
Electronic Mail
 Your email address is your_login@cs.utexas.edu.

 Mail Programs:
  There are literally hundreds of mail readers freely available for UNIX.
    Staff supports alpine, mutt, and thunderbird.


 Reading mail:
  Typing alpine with no username puts you into the Alpine mail program.
    Selecting Message Index will show you the contents of your mailbox.


 Sending mail:
  alpine username sends mail to the person with the login username. If it’s a
  CS login, you can leave off the domain name. Otherwise, use the entire email
  address: (name@domain.com).

 Finding addresses:
  phone name will try to find someone with that name in CS.
Printing
 All printers are numbered in the CS department as lw# (for example, lw4).
 A banner page is printed with your file to keep users’ work separated.

 lpr
   lpr –Plw4 file1 will send file1 to printer number 4, in the Taylor basement lab.

 lpq
   lpq –Plw4 will list job numbers of all the files currently in lw4’s print queue (those waiting to
   be printed).

 lprm
   lprm –Plw4 900 will remove job number 900 from lw4’s print queue (the job number can be
   found with lpq).

 less /lusr/share/etc/printcap gives a list of printers and their locations

 Printing FAQ:
   http://www.cs.utexas.edu/facilities/faq
   http://www.cs.utexas.edu/facilities/documentation/printing-options
The UNIX System Shell Programming
Steps to Create Shell Programs
Specify shell to execute program
 Script must begin with #! (pronounced “shebang”)
   to identify shell to be executed
Examples:
    #! /bin/sh         (defaults to bash)
    #! /bin/bash
    #! /bin/csh
    #! /usr/bin/tcsh
Make the shell program executable
 Use the “chmod” command to make the
  program/script file executable                     30
Example: “hello” Script
#! /bin/csh
echo "Hello $USER"
echo "This machine is `uname -n`"
echo "The calendar for this month is:"
cal
echo "You are running these processes:"




                                           Karkha.com
ps

                                          31
Example script output
% chmod u+x hello
% ./hello
Hello ege!
This machine is turing
The calendar for this month is
   February 2008
 S M Tu W Th F S
 1 2 3 4 5 6 7
 8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
You are running these processes:
   PID TTY       TIME CMD
 24861 pts/18    0:00 hello.csh
 24430 pts/18    0:00 csh

                                   Karkha.
                  32                  com
Shell logic structures needed for program
Basic
       Logic Structures
  development:

  Sequential logic
  User input
  Decision logic
  Looping logic
  Case logic




                                            Karkha.
                33                             com
Input to a C shell script
 Reading/prompting for user input
Providing input as command line arguments
Accessing contents of files




                                             Karkha.
              34                                com
Reading user input with $<
Use a special C shell variable:
      $<

Reads a line from terminal (stdin)
 up to, but not including the new line




                                         Karkha.
              35                            com
Example: Accepting User Input
#! /bin/csh
echo "What is your name?"
set name = $<
echo Greetings to you, $name
echo "See you soon"




                               Karkha.
           36                     com
Example: greetings User Input
% chmod u+x
            Accepting
% ./greetings            User entered Laura
                         Flowers
What is your name?
Laura Flowers
Greetings to you, Laura Flowers
See you soon




                                              Karkha.
              37                                 com
Command line arguments
Use arguments to modify script behavior


command line arguments become
  positional parameters to C shell script

positional parameters are numbered
  variables:   $1, $2, $3 …




                                            Karkha.
               38                              com
Command line arguments
     Meaning
$0          name of the script
$1, $2            first and second parameter
${10}       10th parameter
                 { } prevents “$1”
    misunderstanding
$*          all positional parameters
$#argv      the number of arguments


                                               Karkha.
              39                                  com
Example: Command Line
Arguments
#! /bin/csh
# Usage: greetings name1 name2
# Input: name1 and name2
echo $0 to you $1 $2
echo Today is `date` $1 $2
echo Good Bye $1 $2




                                 Karkha.
           40                       com
Example: Command Line
Arguments
                             $0 => greetings
                               $1 => Mark
                              $2 => Flowers


% chmod u+x greetings
% ./greetings Mark Flowers
./greetings to you Mark Flowers
Today is Mon Feb 16 14:18:03 CST
 2008
Good Bye Mark Flowers

                                               Karkha.
           41                                     com
Decision logic
if Statement: simplest forms


 if ( expression ) command



 if ( expression ) then
   command(s)
 endif


                                Karkha.
             42                    com
Decision Statement
if-then-else
              logic
 if ( expression ) then
   command(s)
 else
   command(s)
 endif




                          Karkha.
           43                com
Decision Statement
if-then-else
              logic
 if ( expression ) then
   command(s)
 else if ( expression ) then
   command(s)
 else
   command(s)
 endif

                               Karkha.
           44                     com
Basic Operators in Expressions
              Meaning
       ()       grouping
       !        Logical “not”
    > >= < <=   greater than, less than
     == !=      equal to, not equal to

       ||       Logical “or”
       &&       Logical “and”


                                          Karkha.
         45                                  com
Expression examples$2
if ( $1 == “next” ) echo


if ( $#argv != 0 ) then
    …
 endif

if ( $#argv > 0 && $#argv < 5) then
    …
 endif
                                       Karkha.
          46                              com
Example: Command Line
Arguments
#! /bin/csh
if ( $#argv == 0 ) then
   echo -n "Enter time in minutes: "
   @ min = $<
else
   @ min = $1
endif
@ sec = $min * 60
echo “$min minutes is $sec seconds”
                                       Karkha.
           47                             com
Example: Reading file contents
#! /bin/csh
# Usage: lookup nameOrNumber
set list = "users.txt"
if ( $#argv == 0 ) then
   echo -n "Enter name OR z-id: "
   set name = $<
else
   set name = $*
endif
grep -i "$name" $list
if ( $status ) echo "$name not found"   Karkha.
            48                             com
File Testing operators)
Syntax: if ( -opr filename

    opr   Meaning
     r    Read access
    w     Write access
    x     Execute access
     e    Existence
     z    Zero length
     f    Ordinary file
    d     directory

                              Karkha.
             49                  com
Example: File Testing
if ( -e $1 ) then
     echo $1 exists
     if ( -f $1 ) then
          echo $1 is an ordinary file
     else
          echo $1 is NOT ordinary
 file
     endif
else
     echo $1 does NOT exist
endif
                                        Karkha.
           50                              com
C Shell looping constructs
predetermined iterations
 repeat
 foreach



condition-based iterations
 while



                              Karkha.
             51                  com
Fixed number iterations
Syntax:

  repeat number command

  executes “command” “number” times


Examples:
 repeat 5 ls
 repeat 2 echo “go home”

                                       Karkha.
             52                           com
The foreach Statement
  foreach name ( wordlist )
    commands
  end

wordlist is:
  list of words, or
  multi-valued variable
each time through,
 foreach assigns the next item in wordlist to
 the variable $name
                                                Karkha.
                53                                 com
Example: foreach Statement
 foreach word ( one two three )
    echo $word
 end

or


 set list = ( one two three )
 foreach word ( $list )
    echo $word
 end
                                  Karkha.
          54                         com
Loops with foreach
 useful to process result of command,
  one at a time

Example:
#! /bin/csh
@ sum = 0
foreach file (`ls`)
         set size = `cat $file | wc -c`
         echo "Counting: $file ($size)"
         @ sum = $sum + $size
end
echo Sum: $sum                            Karkha.
                  55                         com
The while Statement
  while ( expression )
     commands
  end

use when the number of iterations is not
 known in advance
execute ‘commands’ when the expression is
 true
terminates when the expression becomes
 false
                                             Karkha.
             56                                 com
Example: while
#! /bin/csh
@ var = 5
while ( $var > 0 )
     echo $var
     @ var = $var – 1
end




                        Karkha.
           57              com
Example: while
#! /bin/csh
echo -n "Enter directory to list: "
set dirname = $<
while ( ! -d $dirname )
    echo "$dirname is not directory"
    echo -n "Enter directory to list:
 "
    set dirname = $<
end
ls $dirname
                                        Karkha.
           58                              com
loop control
 break
 ends loop, i.e. breaks out of current loop

continue
 ends current iteration of loop, continues with
 next iteration




                                                  Karkha.
              59                                     com
loop control example
 #! /bin/csh
while (1)
        echo -n "want more?   "
        set answer = $<
        if ($answer == "y")   echo
 "fine"
        if ($answer == "n")   break
        if ($answer == "c")   continue
        echo "now we are at   the end"
end
                                         Karkha.
           60                               com
loop control example
 #! /bin/csh
while ( 1 )
    echo -n "Enter directory to list:
 "
    set dirname = $<
    if ( -d $dirname ) break
    echo "$dirname is not directory"
end
ls $dirname

                                        Karkha.
           61                              com
The when a variable can take different
Use
     switch Statement
 values
Use switch statement to process different
 cases (case statement)

Can replace a long sequence of
  if-then-else statements




                                             Karkha.
              62                                com
The switch Statement
switch ( string ) C shell compares
 case pattern1:   ‘string’ to each ‘pattern’
                  until it finds a match
     command(s)
 breaksw
                  When a match is
 case pattern2:
                  found, execute the
     command(s)   command(s)
 breaksw
endsw
                  … until breaksw


                                               Karkha.
          63                                      com
The switch Statement
switch (string)
 case pattern1:
     command(s)
 breaksw
 case pattern2:
     command(s)
 breaksw          When a match is not
 default:         found, execute the
                  commands following the
     command(s)   default label
 breaksw
endsw                                      Karkha.
          64                                  com
Example: switch
switch ($var)
        case one:
                echo it is 1
        breaksw
        case two:
                echo it is 2
        breaksw
        default:
                echo it is $var
        breaksw
endsw                             Karkha.
           65                        com
The switch Statement no default,
if no pattern matches and there is
 then nothing gets executed

do not omit the breaksw statement !
 If you omit the breaksw statement, all the
   commands
 under the next case pattern are executed until a
 breaksw or endsw statement is encountered

pattern may contain wildcards:
 *, ?, []
                                                    Karkha.
               66                                      com
Example: switch greeting
#! /bin/csh
# Usage: greeting name
# examines time of day for greeting
set hour=`date`
switch ($hour[4])
    case 0*:
    case 1[01]*:
          set greeting=morning ; breaksw
    case 1[2-7]*:
          set greeting=afternoon ; breaksw
    default:
          set greeting=evening
endsw
echo Good $greeting $1
                                             Karkha.
             67                                 com
Example C Shell program
    AVAILABLE OPTIONS
   *******************
 [1] Display today's date
 [2] How many people are logged on
 [3] How many user accounts exist
 [4] Exit
 Enter Your Choice [1-4]:




                                     Karkha.
            68                          com
userutil shell script 1 of 2
#! /bin/csh
# Usage: userutil
while (1)
  echo "AVAILABLE OPTIONS"
  echo "*******************"
  echo "[1] Display today's date"
  echo "[2] How many people are logged on"
  echo "[3] How many user accounts exist"
  echo "[4] Exit"
  echo "Enter Your Choice [1-4]:"



                                             Karkha.
              69                                com
userutil shell script 2 of 2
   set answer = $<
    switch ($answer)
       case "1":
          echo `date`; breaksw
       case "2":
          echo `users | wc -w` users are logged in
          breaksw
       case "3":
          echo `cat /etc/passwd | wc -l` users exists
          breaksw
       case "4":
          echo "BYE"
          break
          breaksw
    endsw
end             # end of while

                                                        Karkha.
                70                                         com
Advanced C Shell Programming
Quoting
Here
Debugging
Trapping Signals


Functions ?
  calling other scripts
  exec, source, eval



                           Karkha.
               71             com
Quoting for marking a section of a
mechanism
  command for special processing:

  command substitution: `...`
  double quotes: “…“
  single quotes: ‘…‘
  backslash: 




                                     Karkha.
              72                        com
Doublebreakup of string into words
prevents
          quotes
turn off the special meaning of most wildcard
 characters and the single quote
  $ character keeps its meaning
  ! history references keeps its meaning


Examples:
 echo "* isn't a wildcard inside quotes"
 echo "my path is $PATH"

                                                 Karkha.
              73                                    com
Single quotes and command
wildcards, variables
 substitutions are all treated as ordinary text
history references are recognized


Examples:
 echo '*'
 echo '$cwd'
 echo '`echo hello`'
 echo 'hi there !'

                                                  Karkha.
               74                                    com
backslash
backslash character 
  treats following character literally

Examples:
 echo $ is a dollar sign
 echo  is a backslash




                                         Karkha.
               75                           com
Debugging Scripts
% csh –n scriptname
parse commands but do not execute them


% csh –v scriptname
Display each line of the script before execution


% csh –x scriptname
Displays each line of the script after variable
 substitutions and before execution

can also be added to shebang line !
                                                    Karkha.
                76                                     com
Calling other scripts
as subshell, via:
    csh scriptname
    scriptname


subshell does not see current shell’s variables
subshell sees current environment variables




                                                   Karkha.
              77                                      com

Karkha unix shell scritping

  • 1.
    Welcome to UNIXand Shell Scripting workshop BY R.Chockalingam Contact Us https://www.facebook.com/Karkhadotcom Ph : 9543111174,9789989552.
  • 3.
    UNIX  Multitasking, Multiuser Operating System. Unix operating systems are widely used in servers ,workstations and mobile devices.
  • 4.
    The Unix systemis composed of several components In addition to the kernel of an operating system – the development environment, libraries, documents, and the portable, modifiable source-code for all of these components
  • 5.
    UNIX as anOperating System All operating systems provide services for programs they run Executing a new program. Opening a file. Reading a file. Allocating a region of memory. Getting the current time of day etc …
  • 6.
    The Kernel andthe Utilities The Unix system is itself logically divided into two pieces: the kernel and the utilities. The kernel is the heart of the UNIX system and resides in the computer's memory from the time the computer is turned on and booted until the time it is shut down. The utilities, on the other hand, reside on the computer's disk and are only brought into memory as requested.
  • 7.
    Architecture of theUNIX operating system
  • 8.
    Connecting Remotely  Youmust connect to a public CS machine.  Public Machines:  Linux Machines (all of them are now running a Lucid build): juiblex, mig, totoro  In order to find available cshosts  Type cshosts pub on the command line for public machines or use the online list:  http://apps.cs.utexas.edu/unixlabstatus/  To log in, use a SSH program such as SSH Secure Shell or PuTTY (both available from Bevoware).  https://www.utexas.edu/its/bevoware/download/  When connecting, enter a machine name as follows: name.cs.utexas.edu
  • 9.
    File Types Two differenttypes of files in UNIX are 1. Regular files and 2. Directories Regular file. The most common type of file, which contains data of some form. There is no distinction to the UNIX kernel.
  • 10.
    Directory file. Afile that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory, but only the kernel can write directly to a directory file. Block special file. A type of file providing buffered I/O access in fixed-size units to devices such as disk drives. Character special file. A type of file providing un buffered I/O access in variable-sized units to devices. All devices on a system are either block special files or character special files.
  • 11.
    Symbolic link. Atype of file that points to another file. File type macros in <sys/stat.h> Macro Type of file S_ISREG() regular file S_ISDIR() directory file S_ISCHR() character special file S_ISBLK() block special file S_ISFIFO() pipe or FIFO
  • 12.
    File System  TheUNIX file system consists of directories and files. Directories contain files or other directories. Files can be programs, text documents, etc.  You can think of a directory as a folder in Windows or Macs.  Directories contain files or other folders, just like typical operating systems  The file system is organized as an inverted tree.  the directories are branches, and the files are leaves.  The topmost directory is /, and is called root  all other directories and files are underneath root.  All of a user’s files are in a home directory, /u/user  user is the login name for that account.  It is abbreviated ~user, mine is ~rivin  Use the command pwd if you can’t remember which directory you are in.
  • 13.
    File System Structure TheUnix file system is a hierarchical structure that allows users to store information by name. At the top of the hierarchy is the root directory, which always has the name / The location of a file in the file system is called its path. Since the root directory is at the top of the hierarchy, all paths start from /.
  • 15.
    Flags and Arguments command [flags] [arguments]  command: The name of the command.  ls lists the contents of a directory.  flags: switches that modify the function of the base command. Flags usually begin with a "-”.  ls –a shows all the files (including hidden files) in the current directory.  ls –l shows files, in long-listing format, in the current directory.  Flags can be combined.  ls –al shows all the files (including configuration files) in long-listing format, in the current directory.  Flags are swappable, so ls –la will do the same.  arguments: usually the name of a file or directory to perform operations on.  ls dirname shows all the files (in normal-listing format) in the directory dirname.  Flags and Arguments can be combined.  ls –al dirname shows all the files (including hidden files) in long-listing format, in the directory dirname.
  • 16.
    Text Editors  pico:  A very easy to use text editor (the one that is used inside of alpine by default).  vi:  A small, fast, consistent text editor that does not have a windows interface.  Type man vi for various commands to manipulate files.  Type :help inside of vi to bring up the help file.  emacs:  Feature-rich  Can run with a text interface from within a terminal shell, or in its own window with menus, etc. Type emacs to start the program and then Ctrl-h t to run a tutorial.  Emacs will run in its own window when running Xwindows. Just type emacs and the window will come up.
  • 17.
    Basic UNIX Commands ls directory: Lists the contents of the directory. If no directory name is given, it will list the files in the directory you are in.  cd directory: Change from current directory to directory. If no name is given, it will go to your home directory.  mkdir dir1: Create a directory named dir1. Multiple directory names can be given.  cp file1 file2: Make a copy of file1 and name it file2. If file2 already exists, it will be overwritten.  rm file1: Remove the file named file1. Multiple filenames can be given.  mv file1 file2: Rename file1 as file2  mv file1 directory2: Move file1 into directory2.  For example, mv project1 CS310 would move the file project1 into the directory CS310  The directory CS310 must be created first.  .snapshot: Grab the most recent version of a file if you accidentally deleted it.
  • 18.
    Creating a file cat>firstfile Toread a file : %cat > firstfile
  • 19.
    Creating a Directory Creatingdirectories permits you to organize your files. The command mkdir project1
  • 20.
    Basic Commands man [section]<command> Look up a command pwd Display current directory ls [-al] List directory contents cd<directory> Change Directory rm <filename> Delete file cp <filename> <ending filename> Copy file to new location mv <filename> <ending filename> Move file to new location
  • 21.
    cat <filename> Displayentire file more <filename> Display file one page at a time wc [-l|w|c] <filename> Return file statistics: number of words, lines, and ps [ax or ef] List processes running on the system kill -<signal> < process ID> Send end signal to a running process
  • 22.
    Editor Vi emacs vi is theclassic screen-editing program for Unix vi test.txt
  • 23.
    i Changes toinsert mode or a (after the character under the cursor) :w Return Save the file :w<filename> Save the file to a new name :q Return To exit vi Command :q! Return Quit without saving Insert Esc key Changes to command mode mode Backspace and Delete keys Backspaces or deletes, but only for data
  • 25.
    | The Pipe | (shift-) takes the output of one command and feeds it to another command. less is a pager (shows you a text file one page at a time). cshosts shows you all the hosts in CS (many pages). cshosts | less takes the output of cshosts (many pages) and runs less on it. grep string filename looks for instances of string inside of filename. cshosts | grep mo tells you the machine names returned by cshosts that contain instances of “mo”.
  • 26.
    More UNIX Commands Xacroread filename.pdf: Read a .pdf file. If no filename is given, it will open the program with no initial file. - xpdf can also be used. X gv filename.ps: If you ever get a .ps (postscript) file, use this to view it. If no name is given, it will open the program with no initial file. X gimp filename: View a graphics file (.jpg or .gif).  chkquota: Check your disk-space usage in megabytes.  gzip filename: Zip a file to conserve space.  gunzip filename: Unzip a file that is zipped.  zip/unzip filename: Winzip compatible  ps uxw (ps –u user): Check the processes you have running.  kill 14083: Kill the process with PID# 14083.
  • 27.
    Electronic Mail  Youremail address is [email protected].  Mail Programs: There are literally hundreds of mail readers freely available for UNIX.  Staff supports alpine, mutt, and thunderbird.  Reading mail: Typing alpine with no username puts you into the Alpine mail program.  Selecting Message Index will show you the contents of your mailbox.  Sending mail: alpine username sends mail to the person with the login username. If it’s a CS login, you can leave off the domain name. Otherwise, use the entire email address: ([email protected]).  Finding addresses: phone name will try to find someone with that name in CS.
  • 28.
    Printing  All printersare numbered in the CS department as lw# (for example, lw4).  A banner page is printed with your file to keep users’ work separated.  lpr lpr –Plw4 file1 will send file1 to printer number 4, in the Taylor basement lab.  lpq lpq –Plw4 will list job numbers of all the files currently in lw4’s print queue (those waiting to be printed).  lprm lprm –Plw4 900 will remove job number 900 from lw4’s print queue (the job number can be found with lpq).  less /lusr/share/etc/printcap gives a list of printers and their locations  Printing FAQ: http://www.cs.utexas.edu/facilities/faq http://www.cs.utexas.edu/facilities/documentation/printing-options
  • 29.
    The UNIX SystemShell Programming
  • 30.
    Steps to CreateShell Programs Specify shell to execute program Script must begin with #! (pronounced “shebang”) to identify shell to be executed Examples: #! /bin/sh (defaults to bash) #! /bin/bash #! /bin/csh #! /usr/bin/tcsh Make the shell program executable Use the “chmod” command to make the program/script file executable 30
  • 31.
    Example: “hello” Script #!/bin/csh echo "Hello $USER" echo "This machine is `uname -n`" echo "The calendar for this month is:" cal echo "You are running these processes:" Karkha.com ps 31
  • 32.
    Example script output %chmod u+x hello % ./hello Hello ege! This machine is turing The calendar for this month is February 2008 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 You are running these processes: PID TTY TIME CMD 24861 pts/18 0:00 hello.csh 24430 pts/18 0:00 csh Karkha. 32 com
  • 33.
    Shell logic structuresneeded for program Basic Logic Structures development: Sequential logic User input Decision logic Looping logic Case logic Karkha. 33 com
  • 34.
    Input to aC shell script Reading/prompting for user input Providing input as command line arguments Accessing contents of files Karkha. 34 com
  • 35.
    Reading user inputwith $< Use a special C shell variable: $< Reads a line from terminal (stdin) up to, but not including the new line Karkha. 35 com
  • 36.
    Example: Accepting UserInput #! /bin/csh echo "What is your name?" set name = $< echo Greetings to you, $name echo "See you soon" Karkha. 36 com
  • 37.
    Example: greetings UserInput % chmod u+x Accepting % ./greetings User entered Laura Flowers What is your name? Laura Flowers Greetings to you, Laura Flowers See you soon Karkha. 37 com
  • 38.
    Command line arguments Usearguments to modify script behavior command line arguments become positional parameters to C shell script positional parameters are numbered variables: $1, $2, $3 … Karkha. 38 com
  • 39.
    Command line arguments Meaning $0 name of the script $1, $2 first and second parameter ${10} 10th parameter { } prevents “$1” misunderstanding $* all positional parameters $#argv the number of arguments Karkha. 39 com
  • 40.
    Example: Command Line Arguments #!/bin/csh # Usage: greetings name1 name2 # Input: name1 and name2 echo $0 to you $1 $2 echo Today is `date` $1 $2 echo Good Bye $1 $2 Karkha. 40 com
  • 41.
    Example: Command Line Arguments $0 => greetings $1 => Mark $2 => Flowers % chmod u+x greetings % ./greetings Mark Flowers ./greetings to you Mark Flowers Today is Mon Feb 16 14:18:03 CST 2008 Good Bye Mark Flowers Karkha. 41 com
  • 42.
    Decision logic if Statement:simplest forms if ( expression ) command if ( expression ) then command(s) endif Karkha. 42 com
  • 43.
    Decision Statement if-then-else logic if ( expression ) then command(s) else command(s) endif Karkha. 43 com
  • 44.
    Decision Statement if-then-else logic if ( expression ) then command(s) else if ( expression ) then command(s) else command(s) endif Karkha. 44 com
  • 45.
    Basic Operators inExpressions Meaning () grouping ! Logical “not” > >= < <= greater than, less than == != equal to, not equal to || Logical “or” && Logical “and” Karkha. 45 com
  • 46.
    Expression examples$2 if ($1 == “next” ) echo if ( $#argv != 0 ) then … endif if ( $#argv > 0 && $#argv < 5) then … endif Karkha. 46 com
  • 47.
    Example: Command Line Arguments #!/bin/csh if ( $#argv == 0 ) then echo -n "Enter time in minutes: " @ min = $< else @ min = $1 endif @ sec = $min * 60 echo “$min minutes is $sec seconds” Karkha. 47 com
  • 48.
    Example: Reading filecontents #! /bin/csh # Usage: lookup nameOrNumber set list = "users.txt" if ( $#argv == 0 ) then echo -n "Enter name OR z-id: " set name = $< else set name = $* endif grep -i "$name" $list if ( $status ) echo "$name not found" Karkha. 48 com
  • 49.
    File Testing operators) Syntax:if ( -opr filename opr Meaning r Read access w Write access x Execute access e Existence z Zero length f Ordinary file d directory Karkha. 49 com
  • 50.
    Example: File Testing if( -e $1 ) then echo $1 exists if ( -f $1 ) then echo $1 is an ordinary file else echo $1 is NOT ordinary file endif else echo $1 does NOT exist endif Karkha. 50 com
  • 51.
    C Shell loopingconstructs predetermined iterations repeat foreach condition-based iterations while Karkha. 51 com
  • 52.
    Fixed number iterations Syntax: repeat number command executes “command” “number” times Examples: repeat 5 ls repeat 2 echo “go home” Karkha. 52 com
  • 53.
    The foreach Statement foreach name ( wordlist ) commands end wordlist is: list of words, or multi-valued variable each time through, foreach assigns the next item in wordlist to the variable $name Karkha. 53 com
  • 54.
    Example: foreach Statement foreach word ( one two three ) echo $word end or set list = ( one two three ) foreach word ( $list ) echo $word end Karkha. 54 com
  • 55.
    Loops with foreach useful to process result of command, one at a time Example: #! /bin/csh @ sum = 0 foreach file (`ls`) set size = `cat $file | wc -c` echo "Counting: $file ($size)" @ sum = $sum + $size end echo Sum: $sum Karkha. 55 com
  • 56.
    The while Statement while ( expression ) commands end use when the number of iterations is not known in advance execute ‘commands’ when the expression is true terminates when the expression becomes false Karkha. 56 com
  • 57.
    Example: while #! /bin/csh @var = 5 while ( $var > 0 ) echo $var @ var = $var – 1 end Karkha. 57 com
  • 58.
    Example: while #! /bin/csh echo-n "Enter directory to list: " set dirname = $< while ( ! -d $dirname ) echo "$dirname is not directory" echo -n "Enter directory to list: " set dirname = $< end ls $dirname Karkha. 58 com
  • 59.
    loop control break ends loop, i.e. breaks out of current loop continue ends current iteration of loop, continues with next iteration Karkha. 59 com
  • 60.
    loop control example #! /bin/csh while (1) echo -n "want more? " set answer = $< if ($answer == "y") echo "fine" if ($answer == "n") break if ($answer == "c") continue echo "now we are at the end" end Karkha. 60 com
  • 61.
    loop control example #! /bin/csh while ( 1 ) echo -n "Enter directory to list: " set dirname = $< if ( -d $dirname ) break echo "$dirname is not directory" end ls $dirname Karkha. 61 com
  • 62.
    The when avariable can take different Use switch Statement values Use switch statement to process different cases (case statement) Can replace a long sequence of if-then-else statements Karkha. 62 com
  • 63.
    The switch Statement switch( string ) C shell compares case pattern1: ‘string’ to each ‘pattern’ until it finds a match command(s) breaksw When a match is case pattern2: found, execute the command(s) command(s) breaksw endsw … until breaksw Karkha. 63 com
  • 64.
    The switch Statement switch(string) case pattern1: command(s) breaksw case pattern2: command(s) breaksw When a match is not default: found, execute the commands following the command(s) default label breaksw endsw Karkha. 64 com
  • 65.
    Example: switch switch ($var) case one: echo it is 1 breaksw case two: echo it is 2 breaksw default: echo it is $var breaksw endsw Karkha. 65 com
  • 66.
    The switch Statementno default, if no pattern matches and there is then nothing gets executed do not omit the breaksw statement ! If you omit the breaksw statement, all the commands under the next case pattern are executed until a breaksw or endsw statement is encountered pattern may contain wildcards: *, ?, [] Karkha. 66 com
  • 67.
    Example: switch greeting #!/bin/csh # Usage: greeting name # examines time of day for greeting set hour=`date` switch ($hour[4]) case 0*: case 1[01]*: set greeting=morning ; breaksw case 1[2-7]*: set greeting=afternoon ; breaksw default: set greeting=evening endsw echo Good $greeting $1 Karkha. 67 com
  • 68.
    Example C Shellprogram AVAILABLE OPTIONS ******************* [1] Display today's date [2] How many people are logged on [3] How many user accounts exist [4] Exit Enter Your Choice [1-4]: Karkha. 68 com
  • 69.
    userutil shell script1 of 2 #! /bin/csh # Usage: userutil while (1) echo "AVAILABLE OPTIONS" echo "*******************" echo "[1] Display today's date" echo "[2] How many people are logged on" echo "[3] How many user accounts exist" echo "[4] Exit" echo "Enter Your Choice [1-4]:" Karkha. 69 com
  • 70.
    userutil shell script2 of 2 set answer = $< switch ($answer) case "1": echo `date`; breaksw case "2": echo `users | wc -w` users are logged in breaksw case "3": echo `cat /etc/passwd | wc -l` users exists breaksw case "4": echo "BYE" break breaksw endsw end # end of while Karkha. 70 com
  • 71.
    Advanced C ShellProgramming Quoting Here Debugging Trapping Signals Functions ? calling other scripts exec, source, eval Karkha. 71 com
  • 72.
    Quoting for markinga section of a mechanism command for special processing: command substitution: `...` double quotes: “…“ single quotes: ‘…‘ backslash: Karkha. 72 com
  • 73.
    Doublebreakup of stringinto words prevents quotes turn off the special meaning of most wildcard characters and the single quote $ character keeps its meaning ! history references keeps its meaning Examples: echo "* isn't a wildcard inside quotes" echo "my path is $PATH" Karkha. 73 com
  • 74.
    Single quotes andcommand wildcards, variables substitutions are all treated as ordinary text history references are recognized Examples: echo '*' echo '$cwd' echo '`echo hello`' echo 'hi there !' Karkha. 74 com
  • 75.
    backslash backslash character treats following character literally Examples: echo $ is a dollar sign echo is a backslash Karkha. 75 com
  • 76.
    Debugging Scripts % csh–n scriptname parse commands but do not execute them % csh –v scriptname Display each line of the script before execution % csh –x scriptname Displays each line of the script after variable substitutions and before execution can also be added to shebang line ! Karkha. 76 com
  • 77.
    Calling other scripts assubshell, via: csh scriptname scriptname subshell does not see current shell’s variables subshell sees current environment variables Karkha. 77 com

Editor's Notes

  • #3 UNIX can be intimidating, and is not necessarily user-friendly. Your CS degree is built on knowing a variety of tools, and your knowledge of UNIX will prove very useful in your time here as well as your CS career. Most professors will require UNIX at some point. UNIX is a multi-user OS where users can share one machine&apos;s resources.
  • #9 Telnet is not supported. Run “ cshosts publinux, pub64, pub32” If you choose a machine with a long name, it will likely be less loaded than a machine with a name such as &apos;tig&apos;.
  • #13 In Windows, it starts at c: and then there&apos;s program files, then Microsoft office, then word, etc. Your home directory is yours to do everything you want. This is also how you access your homepage. Use the folder public_html. This shows up at http://www.cs.utexas.edu/~user
  • #16 Most of the time, you will get a warning message when something has failed. ***Talk about each column***
  • #17 Very few people who&apos;ve used UNIX for any amount of time end up using pico. emacs and vi are the two big editors, and both have a big learning curve. Typically, people choose one based on things like working with a lab partner who uses emacs, so they end up learning it too.
  • #18 NOTE: UNIX is Case Sensitive cd .. takes you up one directory cp and mv have two funtionalities: copy files copy to another directory ***When you overwrite a file, you will not get a message. A successful operation does not give feedback*** If you move a file to a dirname that hasn’t been created yet, the file will just be renamed.
  • #26 Space Bar, Page Down Up/Down Arrow, line-by-line
  • #27 X have to be running X Text files compress well. grep &apos;whatever&apos; to hang a process for termination. This is a sure bet that the process will hang since whatever doesn’t appear in any of my files AND there is no argument to complete the grep.
  • #28 Alpine, by default, comes with pico as it’s editor. &lt; &amp; &gt; go in and out of folders. ^J attaches a file
  • #29 Only print things for CS! Show how to get a webpage or pdf to print (entering command for printer) ***grep TAY from /etc/printcap***
  • #30 Karkha.com NIU - Department of Computer Science
  • #31 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #32 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #33 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #35 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #36 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #37 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #38 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #39 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #40 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #41 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #42 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #43 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #44 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #45 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #46 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #47 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #48 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #49 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #50 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #51 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #54 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #55 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #57 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #63 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #64 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #65 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #67 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #69 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #72 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • #77 The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005