You are on page 1of 35

The Shell

Latharani T R, Asst. Professor, Dept. of CSE, AcIT. 1


The Shell’s interpretive life cycle
• On logging on to a UNIX machine, we see a
prompt and system seems to be idle.

• In fact the UNIX command will be running at


the terminal called Shell.

• It swings into action the moment you key in


something
Latharani T R, Asst. Professor, Dept. of CSE,
2
AcIT.
• ps command execution show that the shell is
running.

• When a command is keyed in, it goes as an


input to the shell.

Latharani T R, Asst. Professor, Dept. of CSE,


3
AcIT.
The following activities are performed by the shell in
its interpretive cycle
• The shell issues the prompt and waits for the
command
• Then it scans the command line for meta
characters and expands abbreviations to recreate
a simplified command (rm *)
• Then it passes the command line to the kernel for
execution.
• The shell waits for the command to complete and
normally can’t do any work
• After completion, the prompt reappears and shell
returns to its waiting role to start the next cycle
Latharani T R, Asst. Professor, Dept. of CSE,
4
AcIT.
The wild cards
• The command to list all chapter files is
ls chap chap01 chap02 chap03 chapx chapy chapz
• Shell offers a facility to represent them by a single
pattern framed using metacharacters
• Shell will expand it suitably before command
execution.
• Metacharacters are called wild-cards (a joker that
can match any card)

Latharani T R, Asst. Professor, Dept. of CSE,


5
AcIT.
The Shell’s Wild Cards
Wild card Matches

* zero or more characters


? any single character
[ijk] A single character – either an i, j or k
[x–z] A single character that is within the ASCII
range of characters x and z
[!ijk] A single character that is not an i, j or k

[! x – z ] A single character that is not within the ASCII


range of characters x and z
{ pat1, pat2…} pat1, pat2 etc.

Latharani T R, Asst. Professor, Dept. of CSE,


6
AcIT.
The * and ?
• Matches any number of characters including
none
• The command
ls chap chap01 chap02 chap03 chapx chapy chapz
can be shortened as
ls chap*
• On encountering this command, the shell
identifies * as a wild-card, looks in the current
directory and recreates the command as
ls chap chap01 chap02 chap03 chapx chapy chapz
and hands it over to the kernel for execution
Latharani T R, Asst. Professor, Dept. of CSE,
7
AcIT.
echo *
Lists all filename in the current directory

rm *
Deletes all the files in the current directory

Latharani T R, Asst. Professor, Dept. of CSE,


8
AcIT.
?- Match a single character
ls chap?
chapx chapy chapz

ls chap??
chap01 chap02 chap03

ls .???*
.bash_profile .exrc .netscape .profile

ls emp*lst
emp.lst emp1.lst emp22lst empn.lst
Latharani T R, Asst. Professor, Dept. of CSE,
9
AcIT.
The character class [ ]
ls chap0 [1 2 3 4]
chap01 chap02 chap03

ls chap0 [1 - 4]
chap01 chap02 chap03

ls chap0 [x - z]
chapx chapy chapz

Latharani T R, Asst. Professor, Dept. of CSE,


10
AcIT.
Negating the character class [! ]
* . [! c o]
• Matches all filenames with single char
extension excluding .c or .o

[! a – z A – Z ]
Matches all filenames that don’t begin withan
alphabet.

Latharani T R, Asst. Professor, Dept. of CSE,


11
AcIT.
Matching totally dissimilar patterns
cp $HOME / progs / * . {c, java} .

cp / home / kumar /{project, html, scripts}/*

Latharani T R, Asst. Professor, Dept. of CSE,


12
AcIT.
Some Examples
• ls *.c
• mv * . . / bin
• cp foo foo*
• cp ?????? Progs
• lp note [0-1] [0-9]
• rm *.[ ! l][ ! o] [ ! g ]
• cp -r /home/kumar/{include, lib, bin} .
Latharani T R, Asst. Professor, Dept. of CSE,
13
AcIT.
Escaping and quoting
• Escaping – Providing a \ before the wild-card to
remove its special meaning

• Quoting – Enclosing the wild card or the entire


pattern, within quotes to remove its special
meaning

Latharani T R, Asst. Professor, Dept. of CSE,


14
AcIT.
Escaping
• rm chap\*

• ls chap0 \ [ 1 - 3\]

Latharani T R, Asst. Professor, Dept. of CSE,


15
AcIT.
The three standard files
• These are actually streams of characters

• Stream- sequence of bytes

• When user logs in, shell makes user available


these streams.

• Each stream is associated with a default device


Latharani T R, Asst. Professor, Dept. of CSE,
16
AcIT.
• Standard input – file representing input,
connected to the keyboard

• Standard output - file representing output,


connected to the display.

• Standard error – the file representing error


messages emanate from command or shell.
– Also connected to display

Latharani T R, Asst. Professor, Dept. of CSE,


17
AcIT.
Standard input
• Three input sources
– Keyboard – default source
– A file using redirection with < symbol
– Another program using pipeline

$ wc
Count this input
And display
[ctrl-d]
Latharani T R, Asst. Professor, Dept. of CSE,
18
AcIT.
$ wc < sample.txt
3 14 71
• Keep the command ignorant of the source
• On the above command execution
– On seeing the <, the shell opens the disk file,
sample.txt for reading
– It unplugs the standard input file from its default
source and assigns to sample.txt
– Wc reads from standard input
• cat - f1
• cat f1 - f2
Latharani T R, Asst. Professor, Dept. of CSE,
19
AcIT.
Standard output
• Three possible destinations
– The terminal – default destination
– A file using the redirection symbols > and >>
– As input to another program using a pipeline

– $ wc sample.txt > newfile

– $ cat newfile

– $ wc sample.txt >> newfile


doesn’t disturb previous contents
Latharani T R, Asst. Professor, Dept. of CSE,
20
AcIT.
• Cat * . C > c_progs.txt

• ( ls -x *.c ; echo ; cat * . C) > c_progs.txt

Latharani T R, Asst. Professor, Dept. of CSE,


21
AcIT.
Standard error
• Each standard file is represented by a number,
called file descriptor
• Kernel maintains the table of file descriptors
for every process running in the system.
– 0 - standard input ( < or 0< )
– 2 - standard output ( > or 1> )
– 3 - standard error ( 2> )

Latharani T R, Asst. Professor, Dept. of CSE,


22
AcIT.
$ cat f1
cat: cannot open f1

$ cat f1 > errorfile


cat: cannot open f1

$ cat f1 2> errorfile

$ cat errorfile
cat: cannot open f1

Latharani T R, Asst. Professor, Dept. of CSE,


23
AcIT.
$ cat f1 2>> errorfile

$ cat f1 > f2 2> errorfile

Latharani T R, Asst. Professor, Dept. of CSE,


24
AcIT.
Shell Variables
• shell variables are useful both in command line
and shell scripts.

• Examples
• $ count = 5
• $ echo $count
• $ total = $count
• $ echo $total

Latharani T R, Asst. Professor, Dept. of CSE,


25
AcIT.
• When shell reads command line, it interprets
any word preceded by $ as a variable.
• Then replaces the word by its value
• No white space on either side of = symbol
• Begin with a letter
• Can contain numerals and _
• Case sensitive
• Data type need not be mentioned
• All shell variables are string type
• Initialized to null string by default
• X=“” or x=‘’ or x=
Latharani T R, Asst. Professor, Dept. of CSE,
26
AcIT.
• A variable can be removed with unset
• $ unset x

• Protected by making it readonly


• Readonly x

Latharani T R, Asst. Professor, Dept. of CSE,


27
AcIT.
$ progs = ‘/home/kumar/progs’

$ cd $progs ; pwd
/home/kumar/progs

$ mydir = `pwd` ; echo $mydir


/home/kumar/progs

$ size =` wc -c < sample.txt


$ base sample ; ext = .c
$ file = $base$ext
Latharani T R, Asst. Professor, Dept. of CSE,
28
AcIT.
• $ base=sample

• $ file = $base’.c’

• $ file = $base’01’

Latharani T R, Asst. Professor, Dept. of CSE,


29
AcIT.
grep command – searching for a
pattern
• Syntax
grep options pattern filename(s)
Examples
grep “sales” emp.lst
grep president emp.lst
grep “director” emp1.lst emp2.lst
grep ‘Jai Sharma’ emp.lst
Latharani T R, Asst. Professor, Dept. of CSE,
30
AcIT.
grep options
• Ignoring case (-i)
– Ignores case for pattern matching
$ grep –i ‘agarwal’ emp.lst

• Deleting Lines (-v)


– Selects all lines except those containing patterns
$ grep –v ‘director’ emp.lst > otherlist

Latharani T R, Asst. Professor, Dept. of CSE,


31
AcIT.
• Displaying line numbers (-n)
– Display line numbers coontaining the pattern
along with lines
$ grep -n ‘marketing’ emp.lst

• Counting lines containing pattern (-c)


– Counts the number of lines containing the pattern
– Not same as number of occurrences
$ grep -c ‘director’ emp.lst
$ grep -c ‘director’ emp*.lst

Latharani T R, Asst. Professor, Dept. of CSE,


32
AcIT.
• Displaying Filenames (-l)

– Displays only the names of files containing the


pattern
$ grep -l ‘manager’ *.lst

• Matching multiple patterns (-e)

$ grep -e “Agarwal” -e “aggarwal” -e


“agrawal” emp.lst

Latharani T R, Asst. Professor, Dept. of CSE,


33
AcIT.
• Taking patterns from a file (-f)
• Place all patterns in a file, one pattern per line.
• $ grep -f pattern.lst emp.lst

Latharani T R, Asst. Professor, Dept. of CSE,


34
AcIT.
Latharani T R, Asst. Professor, Dept. of CSE,
35
AcIT.

You might also like