Unix Introduction

W
Document Sample
scope of work template
							                                Unix Introduction



¯ These notes partly based on
   – CS 211 lecture notes
   – UWO CS department Unix introduction




                                        1
                              Unix: Power to the Expert



¯ Unix: intended for expert computer users
   – Steep learning curve
   – Difficult for first-time computer users
   – Easy and powerful, once learned
¯ Terse, “cute” command names
¯ When not running an editor or an application, you will spend most of your time on Unix
   – Interacting with a “shell” (command interpreter program)
   – Running “utility commands”
¯ In X window system (Cygwin), select “shell” from menu to get a shell window




                                             2
                                          Shells



¯ Several different versions of shell: sh, csh, tcsh, bash, . . .
¯ As soon as you log in, you will be interacting with an instance of your default shell
   – To find what your default shell is:
       – Issue command finger username
         where username is your username
       – What is listed after Shell: is your default shell
¯ You may have a different default shell on GAUL and RENT
¯ To run a different shell instance on top of your current shell instance, type its name (e.g.
  bash)
¯ To exit a shell, type exit or logout
¯ For the rest of this tutorial, we will assume bash




                                             3
                                   Editing Files: pico



¯ Easiest editor to use: pico
¯ To start editing new or existing file: pico fname
¯ File name at top of screen
¯ Some commands described at bottom of screen
¯ Type letters to enter them into the file
¯ Use arrow keys to navigate around the file
¯ Control-X to exit and save changes
¯ Google pico editor for help on the Web




                                              4
                                  Getting Help: man



¯ man command
  – Gives “manual page” (help information) about command
¯ man -k keyword
  – Prints list of all manual pages that contain keyword in title or brief description




                                            5
                               Basic Unix Commands



¯ Available in all shells
¯ Simple commands
¯ Displaying files
¯ Copying, renaming, removing files
¯ Directories
¯ Moving around and creating directories
¯ Listing directory contents
¯ More copying, renaming, removing




                                           6
                              Some Simple Commands



¯ echo text
  – Just displays text on window
¯ ls
  – Lists files in current directory
¯ touch fname
  – If fname did not exist, creates it as empty file
  – If fname did exist, leaves it the same
¯ history
  – Gives history of all past commands since shell created




                                             7
                                    Displaying Files



¯ cat fname
   – Types the file on the window containing the shell
¯ cat fname1 fname2 . . . fnameN
   – Types all those files on the window containing the shell
     (cat is short for “concatenate”)
¯ more fname
   – Displays as much of the file as can fit in the window
   – If more of the file exists, says “more”
   – Type space to get next “page” of the file
   – Type q to quit
¯ more fname1 fname2 . . . fnameN
   – Displays those files one by one




                                              8
                        Copying, Renaming, Removing Files



¯ cp fname1 fname2
  – Copies entire contents of file fname1 to fname2
  – If fname2 did not exist, creates it
  – If fname2 did exist, replaces it with new contents
¯ mv oldfname newfname
  – Renames file oldfname to newfname
  – If newfname existed before, replaces it
¯ rm fname
  – Removes file completely




                                              9
                                        Directories



¯ Files organized into tree of directories
¯ Root directory of entire file system: “/”
¯ Every file has an full path name: complete unique identification relative to root directory
   – e.g. /csd/faculty/einstein/courses/unixIntro/unixA.txt
¯ You are always in a current working directory
   – e.g. /csd/faculty/einstein/courses/unixIntro/
¯ You can refer to files in working directory without giving full path name
   – e.g. unixA.txt
¯ You have a default “home directory”
   – Your working directory when you first log in
¯ /tmp
   – Directory for temporary files; everyone can create files there
   – All files deleted when system reboots


                                             10
                       Moving Around and Creating Directories



¯ pwd
     – Prints working directory
¯ cd dirname
     – Changes your working directory to dirname
¯ cd
     – Changes your working directory to your home directory
¯ mkdir dirname
     – Makes new directory in the working directory
¯.
     – Single dot: always same as working directory
¯ ..
     – Double dot: always same as parent directory of working directory
     – Thus, cd .. takes you to parent directory


                                            11
                             Listing Directory Contents: ls



¯ ls
   – Lists files and directories inside working directory
¯ ls fname
   – Lists just that file name, if file exists
¯ ls fname1 fname2 . . . fname3
   – Lists all those file names, if files exist
¯ ls dirname
   – Lists contents of that directory
¯ ls -l
   – Lists details of files and directories inside working directory
¯ ls -l fname1 fname2 . . . fname3
   – Lists details of those files
¯ ls -l dirname
   – Lists details of all files in that directory

                                                12
                            Listing Directory Contents: ls



¯ ls -a . . .
   – Lists all files, including those whose names start with “.”
¯ ls -F . . .
   – Indicates what kind of file each file is: directory, executable, etc.
¯ ls -R . . .
   – Lists contents recursively down to leaf nodes
¯ ls -t . . .
   – Sorts files by last modification time instead of name




                                            13
                         More Copying, Renaming, Removing



¯ cp fname dirname
   – Copies that file into that directory
¯ cp fname1 fname2 . . . fnameN dirname
   – Copies all those files into that directory
¯ mv fname dirname
   – Moves that file into that directory
¯ mv fname1 fname2 . . . fnameN dirname
   – Moves all those files into that directory
¯ rm -r dirname
   – Removes that directory and everything in it




                                            14
                                       Searching: grep



¯ grep string fname
   – Searches for string in file fname
¯ grep string fname1 fname2 . . . fnameN
   – Searches for string in all files
¯ grep -i string fname
   – Ignores the case (uppercase / lowercase) of the string
¯ grep -w string fname
   – Searches for string standing alone as a word
¯ Many more options (see man grep for details)
¯ grep is short for “get regular expression and print”




                                             15
                                Shell Input Processing



¯ Every command you give to the shell is processed first
   – If you use certain special characters, parts of your command will be replaced by other
     things
¯ Most non-alphanumerics are “special”! Including:
  ! # $ % & * | ~ ‘ ; " ’ < > ?
   – Not a good idea to use these or spaces in any file name
   – Safe to use the following in file names:
     _ - + = .




                                           16
                                    Star Character



¯ * character: for accessing patterns of file names
¯ Examples:
   – *.java
      – Shell replaces this with a space-separated list of all file names in working directory
        that end in “.java”
   – myapp*
      – Shell replaces this with a space-separated list of all file names in working directory
        that start with “myapp”
¯ Thus:
   – ls myapp*
      – Lists all files in working directory that start with “myapp”
   – mv *.java src
      – Moves all files in working directory that end with “.java” to directory src (if it
        exists)




                                           17
                        Bang (Exclamation Mark) Character



¯ ! character: for accessing patterns of previous commands
¯ Examples:
   – !!
      – Shell replaces this by the last command issued
   – !cd
      – Shell replaces this by the last cd command issued
   – !javac
      – Shell replaces this by the last javac command issued




                                          18
                                    Tilde Character



¯ ~ character: for accessing users’ home directories

  Examples:

   – ~einstein
      – Shell replaces this by name of home directory of user einstein
   – ~curie
      – Shell replaces this by name of home directory of user curie
   –~
      – Shell replaces this by name of your home directory

¯ Thus:
   – ~/bin
      – Name of directory bin inside your home directory




                                            19
                             Dollar Sign and Shell Variables



¯ Shell has arbitrary number of untyped variables
   – Don’t have to declare them
   – They always have values that are sequences of characters
¯ Assign a shell variable a value e.g. with

  UNIVERSITY=Western

¯ Then, shell replaces string $UNIVERSITY in commands by Western
¯ If you use e.g. $UNIVERSITY without variable UNIVERSITY being given a value, then
  shell complains
¯ export varname
   – After you do this, programs that you run will be able to find value you set for varname




                                              20
                                     Echo Revisited



¯ echo any text
   – Just prints the words it is given as parameters
   – Shell processes that command just like any other command
¯ Try these commands:

 echo    *.java
 echo    myapp*
 echo    !!
 echo    !cd
 echo    $PATH




                                            21
                                    Running Programs



¯ First sequence of nonblank characters you type in a command: command name
¯ If command name is a shell command, shell executes that command
¯ If not, then the shell:
    – Looks for a file with that name
    – Interprets it as an executable file
    – Tries to execute that file
¯ Where does it look?
    – On each directory listed on your “path”
    – Your “path” contained in shell variable $PATH
    – Colon-separated list of directory names
¯ Typical PATH:

  /usr/local/bin:/usr/ucb:/usr/bin:/bin:/usr/X11/bin:.




                                           22
                               Breaking Programs



¯ If it looks like a program is hanging, type control-C in the shell window where you
  launched it
   – May cause program to terminate




                                        23
                                          Unix I/O



¯ Standard input and output
¯ Default standard input and output
¯ Redirecting standard input and output
¯ Behaviour of Unix commands
¯ Piping output from command to command




                                             24
                            Standard Input and Output




          Standard Input                                 Standard Output
                                       Unix
                  (stdin)                                (stdout)
                                      Program




¯ Here “Unix program” can be a built-in command, system program or program written by
  user
¯ Standard output (stdout): where printf for C programs goes
   – Most Unix utilities write something to stdout
¯ Standard input (stdin): where data for scanf, gets, etc. for C programs comes from
   – Some Unix utilities read from stdin in some cases




                                        25
                         Default Standard Input and Output




              Keyboard
                                                         Displayed
             (typed into                Unix
                                                        on shell window
            shell window)              Program




¯ By default, reading from stdin causes program to read whatever user types
   – User can type control-D to signal end of file
¯ By default, writing to stdout causes program to display information on shell window




                                           26
                Redirecting Standard Output: Greater-Than Symbol




                                 Unix                     Output
                                Program                    File




¯ To redirect standard output to fname, put >fname at end of command; e.g.

  ls >filelist.txt

  will put list of files on file filelist.txt
   – If file did not exist, command creates file
   – If file did exist, contents replaced




                                           27
                  Redirecting Standard Input: Less-Than Symbol




                          Input                      Unix
                           File                     Program




¯ To redirect standard input to come from fname, put <fname at end of command; e.g.

  myapp <input.txt

  will cause program myapp to read from file input.txt




                                          28
                       Redirecting Standard Input And Output




               Input                     Unix                  Output
                File                    Program                 File




¯ Of course you can redirect both stdin and stdout, e.g.

 myapp <input.txt >output.txt

 will cause myapp to read from input.txt and write output on output.txt




                                        29
                         Behaviour of Some Unix Commands



¯ If cat is given no file name argument, it reads from standard input
   – cat
     will cause whatever the user types (until control-D) to be echoed
   – cat >newfile.txt
     will cause whatever the user types (until control-D) to be put on newfile.txt
¯ If more is given no file name argument:
   – If standard input is a shell window, it complains
   – Otherwise, it reads from standard input
      – Shows one screenful at a time, as usual




                                            30
              Piping Output from Command to Command: Bar Symbol




                 Unix                        Unix                     Unix
               Program 1                   Program 2                Program 3




¯ We can connect stdout of one command with stdin of another command with “|”
  character
¯ Examples:
   – ls | more
      – If there are a lot of files, shows one screenful at a time
   – cat file1.txt file2.txt file3.txt | more
      – Concatenates all the files together and shows one screenful at a time




                                            31
                 Ownership and “Mode” (Access Permission Status)



¯ Every file and directory has
   – An owner (userid)
   – A “mode” (access permission status)
¯ Generally, you can only create a file on a directory if you are the owner of that directory
   – ls -l will tell you about which files you own
¯ By default:
   – You can read, write, create, remove files and directories that you own
   – You can’t read, write, create, remove files or directories that you don’t own




                                            32
                        Useful Mode Changing Operations



¯ chmod a+rX fname
  – Changes permissions so everyone can read fname
¯ chmod -R a+rX dirname
  – Changes permissions so everyone can read every file in dirname directory tree, recur-
    sively down to leaf nodes
¯ chmod u+x fname
  – Allows you to “execute” fname (treat it as a runnable program)




                                         33
                        Compiling and Running Programs



¯ Compiling C code
¯ Compiling Java code
¯ Running Java code




                                      34
                               gcc: Compiling C Code


¯ gcc myapp.c
  – Compiles main program myapp.c
  – Creates executable file a.out
  – Changes mode of a.out so it is executable
¯ gcc -o myapp myapp.c
  –   Compiles main program myapp.c
  –   Creates executable file myapp
  –   Changes mode of myapp so it is executable
  –   After this, if . is on PATH, issuing command myapp will run that executable file
¯ gcc -c mymodule.c
  – Compiles C code in mymodule.c
  – Creates object code file mymodule.o
¯ gcc -o myapp.c mymodule.o
  – Compiles main program myapp.c
  – Links object code in mymodule.o
  – Creates executable file myapp, etc.

                                           35
                            javac: Compiling Java Code



¯ javac MyApp.java
   – Compiles Java code in MyApp.java
   – Creates MyApp.class
¯ javac -classpath component1:component2:. . . :componentN MyApp.java
  For instance:
  javac -classpath ~andrews/abbot/abbot.jar:. MyApp.java
   – Treats given colon-separated list of components as the classpath
   – Each component can be either
      – A directory (looks in there for .class files)
      – A .jar file
   – Note, components separated by colons; no spaces
¯ To avoid having to type in classpath every time, set shell variable CLASSPATH to a path
  of that form and export it; e.g.

  CLASSPATH=~andrews/abbot/abbot.jar:.
  export CLASSPATH
  javac MyApp.java

                                           36
                             java: Running Java Code



¯ java MyApp
  – Looks for MyApp.class in CLASSPATH
  – Looks for main method in that file
  – Runs it
¯ java -classpath classpath MyApp
  – Takes that as the classpath
¯ java -jar jarfile MyApp
  – Runs main method in indicated class in jarfile




                                        37
                                  bash Shell Scripts



¯ bash shell script = sequence of bash shell commands
¯ Like Windows .bat files
¯ Edit them with normal text editors




                                          38
                                Contents of Shell Script



¯ Find out where bash lives on the system by typing

  which bash

¯ Whatever it says should appear after “#!” on first line of script
¯ Examples:
   – If which bash says /usr/local/bin/bash, then first line of shell script should
     be
     #!/usr/local/bin/bash
   – If which bash says /usr/bin/bash, then first line of shell script should be
     #!/usr/bin/bash
¯ After first line, just enter normal shell commands into the script




                                            39
                         Bash Features Useful in Shells


¯ Variables
¯ Arithmetic expressions, inside $(( ... ))
¯ Conditionals, e.g.
  if [ $i -eq 5 ]
  then
    echo Equal to 5
  else
    echo Not equal to 5
  fi
¯ Loops, e.g.
  i=0
  while [ $i -le 10 ]
  do
    echo $i
    i=$(( i+1 ))
  done
¯ Comparison operators: -eq, -ne, -lt, -gt, -le, -ge

                                       40
                      Permitting and Running Your Shell Script



¯ Change your shell script so it is executable by saying
  chmod u+x scriptname
¯ Now run your shell script as if it were an executable file, i.e. by giving the file name as a
  command




                                            41

						
Related docs