Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 The UNIX Shells

Similar presentations


Presentation on theme: "Chapter 3 The UNIX Shells"— Presentation transcript:

1 Chapter 3 The UNIX Shells

2 Introduction What is shell ? What kinds of shell are available in UNIX
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 Selecting a shell
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 Bourne again shell : /bin/bash
.profile .login Bourne again shell : /bin/bash .bashrc C shell : /bin/csh .cshrc

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 append
%command >fileName %ls > sample.txt append %command>>fileName %cat >> sample.txt

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

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

11 Command substitution %echo the date today is `date`
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 > out.txt %(date; ls; pwd) > 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
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
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 > find.txt & mail output to your self % find . -name a.c -print | mail glass & redirecting input % mail glass < inputfile &

17 Shell Script What is Shell script ? Which 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. 실행 준비 %chmod 700 script.csh script.ksh

19 Subshell Parent  An initial login shell when you log into UNIX Parent
 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 Env. vars Local vars Child Env. vars Local vars

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 HOME=$HOME, PATH=$PATH, MAIL=$MAIL

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

23 Example $firstname=Graham $lastname=Glass $echo $firstname $lastname
$export lastname $sh $^D

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"
% 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 27390 %kill %sleep 30 & sleep30 & sleep 30 %kill –9 0


Download ppt "Chapter 3 The UNIX Shells"

Similar presentations


Ads by Google