The Shell (PDF) by yaosaigeng

VIEWS: 11 PAGES: 38

									      The Shell

              University of Babylon
            Department of Software
             Dr.Safaa O. Al-Mamory
                              2011


04/03/11                              1
 Why Shell?
Several major reasons for learning how to use
the shell are:

1- You will know how to get around any Linux or
other UNIX-like system. For example, I can log in
to my Red Hat Linux MySQL server, my bootable
floppy router/ firewall and explore and use any
of those computer systems from a shell.


 04/03/11                                           2
2- Special shell features enable you to gather data
input and direct data output between commands
and the Linux file system. To save on typing, you
can find, edit, and repeat commands from your
shell history.

3- You can gather commands into a file using
programming constructs such as loops and case
statements to quickly do complex operations that
would be difficult to retype over and over.
Programs consisting of commands that are stored
and run from a file are referred to as shell scripts.
Most Linux system administrators use shell scripts
to automate tasks such as backing up data,
monitoring log files, or checking system health.
  04/03/11                                              3
Shells
  
      An interface between the Linux system and
      the user
  
      Used to call commands and programs
  
      An interpreter
  
      Powerful programming language
       
           “Shell scripts” = .bat .cmd EXEC REXX
  
      Many available (bsh; ksh; csh; bash; tcsh)



04/03/11                                           4
Another definition of a
Shell
  
      A shell is any program that takes input from
      the user, translates it into instructions that
      the operating system can understand, and
      conveys the operating system's output back
      to the user.
      • i.e. Any User Interface
      • Character Based v Graphics Based




04/03/11                                               5
 Shell Prompt
The default prompt for a regular user is simply
a dollar sign:

                       $

The default prompt for the root user is a
pound sign (also called a hash mark):

                       #


 04/03/11                                          6
 Which Shell I Have?

Type “ echo $SHELL” in the terminal window
              and you will get

                 /bin/bash

            If you use Fedora 10.




 04/03/11                                     7
     The Linux System
               User commands includes executable
                      programs and scripts


The shell interprets user commands. It is      User commands
 responsible for finding the commands
  and starting their execution. Several        Shell
  different shells are available. Bash is
                  popular,                     Kernel          File Systems

                                                               Device Drivers

The kernel manages the hardware resources      Hardware
         for the rest of the system.




    04/03/11                                                                    8
 Types of Shell
  
      Sh – simple shell
  
      BASH – Bourne Again Shell
  
      KSH – Korne Shell
  
      CSH – C Shell
  
      SSH – Secure Shell
  
      To use a particular shell type the shell name at the command
      prompt.
       
         Eg $csh – will switch the current shell to c shell
  
      To view the available shells in the system, type cat /etc/shells at
      the command prompt
  
      To view the current shell that is being used, type echo $SHELL at
      the command prompt




04/03/11                                                                    9
  Command Structure
    Command <Options> <Arguments>
    Multiple commands separated by ; can be executed one
    after the other




04/03/11                                                   10
Linux Command Basics
  
      To execute a command, type its name and
      arguments at the command line


                      ls -l /etc

       Command name                Arguments
                        Options
                        (flags)




04/03/11                                        11
Redirecting Output
  
      The output of a command may be sent
      (piped) to a file:

             ls -l >output
                               “>” is used to specify
                               the output file




04/03/11                                                12
Redirecting Input
  
      The input of a command may come (be
      piped) from a file:
             wc <input

                               “<” is used to specify
                               the input file




04/03/11                                                13
Connecting commands
with Pipes
  
      Not as powerful as CMS Pipes but the same
      principle
  
      The output of one command can become the
      input of another:
                                                             Like CMS Pipes, “|” is used
                                                             to separate stages

                        ps aux | grep netscape | wc -l


 The output of the ps                                          wc takes this input and
 command is sent to                                            counts the lines its output
 grep                                                          going to the console
                            grep takes input and searches for
                            “netscape” passing these lines to wc

04/03/11                                                                              14
  Pipes
  An important early development in Unix was the invention
  of "pipes," a way to pass the output of one tool to the input
  of another.
           eg. $ who | wc −l
  By combining these two tools, giving the wc command the
  output of who, you can build a new command to list the
  number of users currently on the system




04/03/11                                                          15
 Redirection and Pipes
  
      Redirection
       
         Input redirection
          − wc < file1 – Content of file 1 is given as input for wc
             command that counts the no of lines, words and
             characters in a file
       
         Output redirection
          − cat file > newfile – Copies file’s content to newfile. Over
             writes the existing content
          − cat file >> newfile – Appends the new content to the
             existing content
  
      Pipes
       
         Output of first command is input for the second and so on
       
         who | wc –l – Number of lines in the output of who command
         will be displayed



04/03/11                                                                  16
Saving command output in
a file
  
      To save the output of a command in a file,
      redirect the standard output to a file. For
      example,
                      $ ls -l >ss.out
  
      Then we can display file's content by:
                         vi ss.out




04/03/11                                            17
Command Options
  
      Command options allow you to control a
      command to a certain degree
  
      Conventions:
       
           Usually being with a single dash and are a
           single letter (“-l”)
       
           Sometimes have double dashes followed by a
           keyword (“--help”)
       
           Sometimes follow no pattern at all



04/03/11                                                18
Common Commands
  
      pwd - print (display) the working directory
  
      cd <dir> - change the current working
      directory to dir
  
      ls - list the files in the current working
      directory
  
      ls -l - list the files in the current working
      directory in long format




04/03/11                                              19
File Commands
  
      cp <fromfile> <tofile>
       
           Copy from the <fromfile> to the <tofile>
  
      mv <fromfile> <tofile>
       
           Move/rename the <fromfile> to the <tofile>
  
      rm <file>
       
           Remove the file named <file>
  
      mkdir <newdir>
       
           Make a new directory called <newdir>
  
      rmdir <dir>
       
           Remove an (empty) directory



04/03/11                                                20
More Commands
  
      who
       
           List who is currently logged on to the system
  
      whoami
       
           Report what user you are logged on as
  
      ps
       
           List your processes on the system
  
      echo “A string to be echoed”
       
           Echo a string (or list of arguments) to the terminal




04/03/11                                                          21
  Help Facilities for
  Commands
  To understand the working of the command and possible
  options use (man command)
   Using the GNU Info System (info, info command)
  Listing a Description of a Program (whatis command)
  Many tools have a long−style option, `−−help', that outputs
  usage information about the tool, including the options
  and arguments the tool takes. Ex: whoami --help




04/03/11                                                        22
  Shell Programming
       You can write shell programs by creating scripts
       containing a series of shell commands.

       The first line of the script should start with #! which
       indicates to the kernel that the script is directly
       executable.

       You immediately follow this with the name of the shell,
       or program (spaces are allowed), to execute, using the
       full path name. So to set up a Bourne shell script the
       first line would be: #! /bin/sh




04/03/11                                                         23
  Shell Programming
       The first line is followed by commands
       Within the scripts # indicates a comment from that point
       until the end of the line, with #! being a special case if
       found as the first characters of the file.
           #!/bin/bash
           cd /tmp
           mkdir t
       You also need to specify that the script is executable by
       setting the proper bits on the file with chmod, e.g.:
       $ chmod +x shell_script




04/03/11                                                            24
 Shell Scripting
  
      Shell scripting is the most useful and
      powerful feature in Linux
       
           Minimizes typing of repetitive command
       
           Can schedule jobs to run in the system
       
           Can initiate back up activities for system
           administration
       
           Similar to batch files in DOS, but more powerful
           than Batch files




04/03/11                                                      25
 Working with shell script
  
      Open a file with extension .sh using vi editor
  
      We can type any number of commands that
      we use to type at command prompt
  
      Save the file
  
      Execute the file
       
           sh file.sh
       
           ./file.sh (if the file has execution permission)




04/03/11                                                      26
 Shell Scripts
  
      To Print a line
       
           echo “Hello World” (Prints Hello World in the screen
           )
  
      To read a line
       
           read n (Stores the content entered by user in
           variable n
  
      To Comment a line
       
           # This is a comment
       
           Only single line comment is available. For multi line
           comment, we need to use # symbol in lines which
           we want to comment.


04/03/11                                                           27
     #!/bin/bash
     while
     true
     do
        cat somefile > /dev/null
        echo .
     done

           /* */
           do forever
               ‘PIPE < SOME FILE | hole’
               say ‘.’
           end



04/03/11                                   28
 Text editors
  
      Vi
  
      Emacs
  
      gEdit
  
      kWrite
  
      TextPad
  
      And more…




04/03/11          29
 VI Editor
  
      Popular text editor
  
      Just type vi <<filename>> at the prompt
      and hit the enter key.
  
      A new file will be opened
  
      Type the contents needed and save
  
      To save, press the Esc Key and then press :
      (colon) w q and then enter
  
      To quit with out saving Esc + : + q and then
      enter



04/03/11                                             30
 Vi editor
  
      Navigation
  Left     alt+- h
  Down alt+ j
  Up       alt+ k
  Right    alt+ l
       
         Top of the screen – H (shift + h) //caps lock will not work
       
         Middle of the screen – M (shift + m)
       
         Bottom of the screen – L (shift + l)
       
         $ - End Key, 0 – Home Key
  
      Edit Commands
       
         Cut – X, x
       
         Copy – yy, yw
       
         Paste – P, p



04/03/11                                                               31
Wild Characters
  You can use three types of wildcards in bash:
  
    The asterisk (*) character matches zero or
    more characters in a file name. That means *
    denotes all files in a directory.
  
    The question mark (?) matches any single
    character. If you type test?, that matches any
    five-character text that begins with test.
  
    A set of characters in brackets matches any
    single character from that set. The string [aB]
    *, for example, matches any filename that
    starts with a or B.
04/03/11                                          32
Example 1:
        The file name starts with s.
        The filename ends with .h.


 The wildcard specification s*.h denotes all file
 names that meet these criteria.
Example 2:
        cp /media/cdrom/* .
bash replaces the wildcard character * with the names of
  all the files in the /media/cdrom directory. The period
  at the end of the command represents the current
  directory.
04/03/11                                             33
Example 3:
Suppose that you have four files — image1.pcx,
  image2.pcx, image3.pcx, and image4.pcx —in the
  current directory. To copy these files to the /mnt/floppy
  directory, use the following command:

             cp image?.pcx /media/floppy

bash replaces the single question mark with any single
  character and copies the four files to /mnt.



04/03/11                                               34
Example 4:
To see a list of all filenames in the /etc/X11/xdm
directory that start with x or X, type the following
  command:
                     ls /etc/X11/xdm/[xX]*




04/03/11                                        35
Example 5:
                      $ ls ????e
                     apple grape
                      $ ls g???e*
                   grape grapefruit

The first example matches any five-character file that
  ends in e (apple, grape). The second matches any file
  that begins with g and has e as its fifth character
  (grape, grapefruit).



04/03/11                                           36
Example 6:
Here are a couple of examples using braces to do
 pattern matching:
                      $ ls [abw]*
              apple banana watermelon
                   $ ls [agw]*[ne]
               apple grape watermelon

In the first example, any file beginning with a, b, or w is
  matched. In the second, any file that begins with a, g,
  or w and also ends with either n or e is matched.

04/03/11                                                 37
  More About Commands
  Exiting the Shell:To exit the shell when you are done, type
  exit or press Ctrl+D.
  Several Commands can be found at several directories:
                           1- /bin
                         2- /usr/bin
                          3- /sbin
Then use the man command (for example, man hostname) to
  see what each command does.
  To view your history list, use the history command.

04/03/11                                                        38

								
To top