The Unix Shell Chapter 8 in Textbook - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

The Unix Shell Chapter 8 in Textbook

Description:

Mostly Bourne/Korn shell for programming. csh, tcsh for interactivity ... ENV - Variable that contains the name of a Korn shell startup file. ... – PowerPoint PPT presentation

Number of Views:131
Avg rating:3.0/5.0
Slides: 35
Provided by: marthas
Category:
Tags: chapter | korn | shell | textbook | unix

less

Transcript and Presenter's Notes

Title: The Unix Shell Chapter 8 in Textbook


1
The Unix ShellChapter 8 in Textbook
2
Shell is
  • A command interpreter
  • Execute single commands or utilities
  • Execute a sequence of commands
  • A programming language
  • Use shell constructs and Unix commands together
    as a programming language to create scripts.
  • Layer between kernel and user

3
General Features of Shell
  • Use of metacharacters and character classes
  • Redirect input and output, connect commands
  • Customize your shell to meet your own needs
  • Create a unique environment for the execution of
    commands
  • Automate tasks by using aliases, scripts and
    cron/at services

4
Which shell?
  • Mostly Bourne/Korn shell for programming
  • csh, tcsh for interactivity
  • bash, the best of both worlds
  • Programming(sh) interactivity
  • Find out which you are running by default by
    typing
  • echo SHELL

5
ps - process status
  • ps
  • PID TTY TIME CMD
  • 3953 ttyq0 000 csh
  • 3985 ttyq0 000 ps
  • sh
  • ps
  • PID TTY TIME CMD
  • 3953 ttyq0 000 csh
  • 3982 ttyq0 000 sh
  • 3994 ttyq0 000 ps

current shell current shell
6
Background commands
  • When the shell starts a command, it does not
    accept another command until the first one
    completes
  • Sometimes a command may run for a long time, and
    you may want to get other work done
  • you can open another window
  • you can run the command in the background
  • primenum
  • primenum
  • z
  • bg

7
Background commands
cc prime-num.c -o prime-num prime-num 1
4046 ps PID TTY TIME CMD 4029 ttyq5
000 csh 4035 ttyq5 000 ps 4046 ttyq5
004 prime-num 4050 ttyq5 000 sh kill -9
4046 ps PID TTY TIME CMD
3678 ttyq5 000 ps 4029 ttyq5 000
csh 4050 ttyq5 000 sh 1 Killed
prime-num
is an operator that instructs the shell not
to wait for the command to finish
8
Input and Output Redirection
INPUT OUTPUT fd0 fd1
ERROR fd2
Keyboardor file or pipe
Terminalor file or pipe
Process
Terminalor file
See figures 8.2, 8.3 and 8.4 in textbook
fd file descriptor
9
cal output cat output S M Tu W Th F S
2 3 4 5 6 7 8 touch f1 f2 f3 ls
-la output cat output total 0 drwx--x--x 2
joe user 96 Jan 27 0209 . drwx--x--x 3 joe
user 96 Jan 27 0209 .. -rw------- 1 joe user
0 Jan 27 0209 f1 -rw------- 1 joe user 0
Jan 27 0209 f2 -rw------- 1 joe user 0 Jan
27 0209 f3 -rw------- 1 joe user 0 Jan 27
0209 output
Destructive Output Redirection
why is outputs size zero?
10
Appending Output Redirection
date Tue Jan 27 024328 CST 2004 date
output cat output total 0 drwx--x--x 2 joe
user 96 Jan 27 0209 . drwx--x--x 3 joe user
96 Jan 27 0209 .. -rw-r--r-- 1 joe user
0 Jan 27 0209 f1 -rw-r--r-- 1 joe user 0
Jan 27 0209 f2 -rw-r--r-- 1 joe user 0 Jan
27 0209 f3 -rw-r--r-- 1 joe user 0 Jan 27
0209 output Tue Jan 27 024328 CST 2004
and are interpreted by the shell
11
Permissions and Output Redirection
chmod u-w output ls -l output -r--r--r-- 1
joe user 150 Sep 12 1542 output date
output sh output cannot create Permission
denied ls -l output sh output cannot
create Permission denied
12
Input Redirection cat f4 aa bb cc zz xx yy wc -l 2 cat bb cc zz xx yy
Notice equivalent commands, the needed
13
cat output wc -l 8
date cal Wed Sep 12 155545 PDT September
2001 S M Tu W Th F S 2 3 4 5 6 7 8
Pipelines and filters
The symbol separates commands, it does not
connect output to input
The standard output of one command may be
connected to the standard input of another by
using a pipe () symbol
Process or command
Process or command
14
cat nums 3 6 1 8 d sort nums 1 3 6 8 cat
nums sort 1 3 6 8
cat nums 3 3 6 7 3 2 grep 3 nums 3 3 3 grep
-v 3 nums 6 7 2 cat numsgrep -v 3sort 2 6 7
PIPE
filter
15
Equivalent commands
  • ls -la output
  • cat output wc -l
  • ls -la wc -l

This one has the advantage that the intermediate
output file is not generated
16
Pattern matching
  • Wildcards
  • matches zero, one or more characters
  • ? matches exactly one character
  • Character class
  • A set of characters enclosed within brackets
    . Matching takes place for a single character in
    the class.

17
Pattern matching examples
ls f1 f2 f3 f4 nums
output ls f f1 f2 f3 f4 touch f1.1 f2.1
f2.2 f2.3 f3.1 f3.3 ls f1 f2 f2.2
f3 f3.3 nums f1.1 f2.1 f2.3 f3.1
f4 output ls f?.? f1.1 f2.1 f2.2 f2.3
f3.1 f3.3 ls f12.1 f1.1 f2.1 ls
f123.2 f2.2 ls f123.1 f1.1 f2.1 f3.1
18
echo f1 f1.1 f2 f2.1 f2.2 f2.3 f3 f3.1 f3.3
f4 hello nums output
?
19
What is a shell script?
  • A series of Unix commands in a file that are
    executed sequentially.

Commands executed in a shell script
more report echo Report date echo echo Those
logged on are who echo echo My files in HOME
are ls HOME echo echo these are my current
processes ps chmod 755 report
Commands typed at command line
date who ls HOME ps
20
Shell variables
  • Shell variables provide a convenient and
    effective way to store information in memory
  • Variable names can be made up of alphabetic,
    numerals and underscores.
  • Variables names can begin with _ or alphabetic
    characters only

21
Shell environment variables
  • Available to the shell and commands run by the
    shell
  • Used to affect the operation of commands
  • By convention, variables use upper case letters
  • To list and set environment variables and their
    values, use command
  • env
  • printenv
  • setenv

22
env _/sbin/env LANGC HZ100 PATH/usr/sbin/us
r/bsd/sbin/usr/bin /usr/bin/X11/usr/java/bin/
. NOMSGLABEL1 REMOTEUSERUNKNOWN LOGNAMEjoe MAIL
/usr/mail/joe USERjoe MSGVERBtextaction DISPLA
Y192.168.1.10 SHELL/bin/csh REMOTEHOST192.168.
1.1 HOME/usr/people/joe TERMvt100 PWD/usr/peopl
e/joe TZPST8PDT
env/printenv
23
Some shell environment variables
  • PATH tells the shell which directories to search
    for commands
  • TERM tells the shell what kind of terminal you
    are on so it can make the proper setup
  • USER or LOGNAME display the username/login of
    the current user
  • HOME displays the home directory of the
    current user
  • SHELL Default shell for current user

24
echo HOME HOME echo HOME /home/cs/m/mxs015000
echo PS1 PS1"next " next echo
PS1 next next echo TERM ansi next
TERMvt100
PS1 defines the primary prompt string which by
default is ''
25
Setting/changing the PATH variable
next echo PATH /usr/local/bin/usr/bin/usr/cc
s/bin next PATH PATH /home/cs/r/rml021000/bin
next echo PATH /usr/local/bin/usr/bin/usr/cc
s/bin /home/cs/r/rml021000/bin
26
Shell local variables
  • Available only to the shell where created
  • Used to affect the operation of the shell
  • By convention, local variable names are usually
    all in lowercase letters
  • Local variables and their values can be listed
    with the set command

27
Variable scope
cat ex1 echo var var200 echo var var20
echo var 20 ex1 200 export var
ex1 20 200 echo var 20
var10 echo var 10 cat ex var100 echo
var ex 100 echo var 10
Local variable
Global variable
28
Positional parameters
cat ex1 echo Arguments echo 1 2 3 4 chmod
755 ex1 ex1 a Arguments a ex1 file1 file2
file3 Arguments file1 file2 file3 ex1 a b c d
e f Arguments a b c d
n refers to the nth command line argument if
applicable
cat hello echo "hello" 1 hello martha hello
martha hello peter hello peter hello
mary hello mary
29
/dev/null
  • Commonly known as the bit-bucket
  • Any output redirected to /dev/null is
    incinerated
  • Its size does not change (always zero)
  • Pseudo-device file, since it is not tied to any
    physical device
  • cat nonexistent-file 2 /dev/null

30
/dev/tty
  • Pseudo-device tied to the current terminal
  • All users in all their terminals/windows could
    use it to get the output of a command in the same
    terminal
  • Shorthand, instead of finding the specific tty,
    and then redirecting output to it
  • program /dev/tty

31
Single, double quotes and \
  • echo Your example in the
    book
  • Your example in the book
  • echo I owe you 10
  • I owe you notify0
  • echo I owe you 10
  • I owe you 10
  • echo I owe you \10
  • I owe you 10

32
Back ticks
  • When the output of a command is needed as an
    argument to another command
  • echo Todays date is date
  • Or
  • Todaydate
  • Note What would happen if I substitute double
    quotes by single in the example?

33
Startup files
  • Startup files can be used to setup the initial
    environment
  • They reside in the users home directory
  • Startup files begin with a dot so they are hidden
    by the ls command without the a option

34
Startup files
  • Examples of startup files
  • C Shell and T Shell
  • .login
  • .cshrc
  • Bourne Shell and Korn Shell
  • .profile
  • ENV - Variable that contains the name of a Korn
    shell startup file.
  • When a login shell exits, shell reads and
    executes commands from the file
  • .logout
  • .bash_logout
Write a Comment
User Comments (0)
About PowerShow.com