Chapter 3 The UNIX Shells - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 3 The UNIX Shells

Description:

Korn shell. core. C shell. 3. Selecting a Shell. Shell functionality ... This is sample Korn shell scipt. echo -n the date today is. date. ?? ?? ... – PowerPoint PPT presentation

Number of Views:186
Avg rating:3.0/5.0
Slides: 28
Provided by: csSookm
Category:
Tags: unix | chapter | korn | shells

less

Transcript and Presenter's Notes

Title: Chapter 3 The UNIX Shells


1
  • Chapter 3 The UNIX Shells

2
Introduction
  • What is shell ?
  • command interpreter
  • an interface between a user and the raw os
  • What kinds of shell are available in UNIX
  • Bourne shell
  • Korn shell
  • C shell

Korn shell
C shell
Bourne shell
core
core
3
Selecting a Shell
  • Shell functionality
  • built-in commands, scripts, variables,
    redirection
  • wildcards, pipes, sequences
  • subshells, background processing, command
    substitution
  • Selecting a shell
  • The system administrator chooses a shell for you.
  • /etc/passwd
  • Full pathname of Shells
  • Bourne /bin/sh , /bin/bash
  • Korn /bin/ksh
  • C shell /bin/csh

4
Selecting a Shell
  • How to change your shell ?
  • chsh
  • Changing login shell for chang
  • Old shell /bin/csh
  • New shell /bin/ksh
  • logout
  • login chang
  • passwd

5
Shell sequence

read a startup file in the user's home directory
display a prompt and waits for a user command
execute the user's command
Control-D
Terminate
6
Startup Files
  • Bourne shell /bin/sh
  • .profile
  • .login
  • Bourne again shell /bin/bash
  • .bashrc
  • .login
  • C shell /bin/csh
  • .cshrc
  • .login

7
Executable files vs. built-in commands
  • several built-in commands in Shell
  • echo -n arg
  • cd directory
  • utility programs (a file with execute permission)
  • stored in a directory
  • Shell locates and executes utilities usually in
    shell variable PATH
  • /bin, /usr/bin, /usr/local/bin, /usr/ucb, /etc,
    /usr/bin/X11
  • ls ... /bin/ls

8
Output Redirection
  • store the output to a file
  • command gtfileName
  • ls gt sample.txt
  • append
  • commandgtgtfileName
  • cat gtgt sample.txt

9
Input Redirection
  • Input redirection
  • execute command using the file as its standard
    input
  • command lt fileName
  • elm chang lt sample.txt
  • Here documents
  • command ltlt word
  • word

10
Pipe
  • Pipes
  • the standard output of command1 as the standard
    input of command2
  • command1 command2
  • ls wc -w

11
Command substitution
  • Command substitution
  • A command surrounded by backquote() is replaced
    by its
  • standard output.
  • echo the date today is date

12
Combination of commands
  • Grouping
  • (command1 ... commandn)
  • executed by a child shell (subshell)
  • shares the same standard input, standard output,
    and standard error
  • redirected and piped as a single command
  • date ls pwd gt out.txt
  • (date ls pwd) gt out.txt

13
Combination of commands
  • Sequences
  • command1 commandn
  • the shell executes them in sequence, from left to
    right
  • Conditional sequence
  • command1 command2
  • command2 is executed only if command1 completes
    successfully (return 0)
  • cc my prog.c a.out
  • command1 command2
  • command2 is executed only if command1 complete
    unsuccessfully
  • cc myprog.c echo compilation failed

14
Filename substitution
  • Filename substitution
  • specify several files using wildcards
  • Example
  • ls .c
  • ls /
  • ls ac
  • cc .c
  • Wildcard Meaning
  • Matches any string, including the empty
    string
  • ? Matches any single character
  • .. Matches any one of the characters between
    the brackets.
  • A range of characters may be specified.

15
Background processing
  • Background
  • Examples
  • find . -name b.c -print
  • 27174
  • date pwd
  • 27310
  • 27311

command
command
shell
16
Background processing
  • Redirecting background processes
  • To prevent the output from a background process
    from arriving at your terminal
  • redirect output to a file
  • find .-name a.c -print gt find.txt
  • mail output to your self
  • find . -name a.c -print mail glass
  • redirecting input
  • mail glass lt inputfile

17
Shell Script
  • What is Shell script ?
  • 1.A file that contains a sequence of shell
    commands
  • 2. Need execute permission.
  • 3. Run it by just typing its name
  • Which shell script
  • 1. C shell script, if the first line is
  • 2. If the first line is !pathName,
  • the executable program pathName is
  • used to interpret the script.
  • 3. Otherwise, it is Bourne shell script.

18
Shell Script Example
  • script.csh
  • This is sample C shell scripts
  • echo -n the date today is
  • date
  • script.ksh
  • !/bin/ksh
  • This is sample Korn shell scipt.
  • echo -n the date today is
  • date
  • ?? ??
  • chmod 700 script.csh script.ksh

19
Subshell
  • ? An initial login shell
  • when you log into UNIX
  • ? When your current shell(parent) creates a new
    (child) shell ?
  • 1. When a grouped command is executed, such as
    (ls pwd date).
  • 2. When a script is executed.
  • 3. When a background job is executed.

Parent
Child
Parent
Child
20
Environment variables vs Local variables
  • ? Every shell contains two data areas
  • 1. environment variable space
  • A child shell inherits a copy of its parent's
  • 2. local variable space
  • A child shell get a clean local variable space

21
Environment variables
  • Predefined environment variables
  • echo HOMEHOME, PATHPATH, MAILMAIL

22
Local variables
  • ? Local variables
  • - shell variables except predefined environment
    variables
  • - export make local variables environment
    variables
  • ? local built-in variables

23
Example
  • firstnameGraham
  • lastnameGlass
  • echo firstname lastname
  • export lastname
  • sh
  • echo firstname lastname
  • D
  • echo firstname lastname

24
Quoting
  • ? To inhibit the shell's wildcard replacement,
    variable substitution, and command substitution
  • 1. single quote (') inhibit wildcard
    replacement, variable substitution, and command
    substitution
  • 2. double quote (") inhibit wildcard replacement
    only
  • 3. when quotes are nested, the outer quote have
    any effect.

25
Quoting
  • ? Examples
  • echo 3 4 12
  • echo "3 4 12"
  • echo '3 4 12'
  • set name Graham
  • echo 'my name is name and the date is 'date'
  • echo "my name is name and the date is 'date' "

26
Job Control
  • ? Process status ps
  • ps -arux
  • list process status information, by default your
    processes.
  • -a option all processes
  • ? sleep seconds
  • sleeps for the specified number of seconds and
    then terminate
  • ? wait pid
  • The shell suspends until the child process with
    pid terminates.
  • If no arguments are supplied, the shell waits
    for all its child processes

27
Job Control
  • ? Signalling process kill
  • kill -signalId pid kill -l
  • 1. Kill(TERM signal) process or
  • 2. sends the signal with code signalId to the
    list of numbered processes.
  • (sleep 10 echo done)
  • ps
  • (sleep 10 echo done)
  • 27390
  • kill -9 27390
  • sleep 30 sleep30 sleep 30
  • kill 9 0
Write a Comment
User Comments (0)
About PowerShow.com