Professional Documents
Culture Documents
Unit - Iii Shell Programming: Dr.T.Logeswari
Unit - Iii Shell Programming: Dr.T.Logeswari
SHELL PROGRAMMING
Dr.T.Logeswari
Symbol :$
Executable filename is sh
Default shell for Unix
Developed by Stephen Bourne
Similar to C program
Symbol :$
Executable filename is ksh
Developed by David Korn
Symbol :$
Executable filename is bash
Developed by Fox and C Ramey
1. ed:
First editor on Unix
Developed by Ken Thompson
2. ex:
Extended editor
Developed by Bill Joy
3. vi:
Visual editor
Screen-oriented version of ex
4. vim:
Vi Improved
Improved version of vi 10
Options:
Options:
:x / :wq / :q - quits vi ,writing modified file
:w – writes modified file and remains in
command mode
:q! – quits vi, without saving the latest
changes
ZZ – saves and exits; this is known as last 13
command
Unix Shell Programming - Forouzan 13
Moving the cursor
Mouse does not work in Unix
Arrows and keys has to be used
Options:
Options:
:$ or G - movesUnixcursor to last
Shell Programming line in file
- Forouzan 15
Screen manipulation
Caps lock (^) before a letter indicates ctrl
key
Options:
^f : moves forward one full screen
^b : moves backward one full screen
^d : moves forward one half screen
^u : moves backward one half screen
16
17
19
Options:
20
Options:
^ - beginning of line
$ - end of line
24
[A_Z]
[a-z]
[0-9]
[0-9 A-Z]
[A-Z][a-zA-Z][0-9]
25
/Hello/
/^[a-zA-Z]/
/^[a-z]*/
/2134$/
/[0-9]*/
/^[!#]/
/^TEST$/
26
Options:
:set ai – turns on auto indentation
:set all – prints all options to the screen
:set eb – precedes error messages with bell
:set nu – shows line numbers
:set prompt – prompts for command input with :
:set showmode – indicates input or replace mode at 28
bottom
Unix Shell Programming - Forouzan 28
Shell variables
They have the ability to store and
manipulate information within a shell
program
Rules for naming variables:
Can contain alphanumeric character and underscore(_)
Must start with alphabet or underscore
Case sensitive
No limit on length of variable name
Ex: No_of_std, NAME 29
System variables
Local or user defined variables
Read-only variables
30
31
38
Eg $ chmod u +x prog1.sh
$./prog1/.sh
welcome
40
40
42
42
43
43
Arithmetic operators:
+, -, \*, /, %
a=5 ; b=10
expr $a + $b
expr $a - $b
expr $a \* $b
expr $a / $b
expr $a % $b
46
a=5; b=10
a=`expr $a + 1` or a=$(expr $a + 1)
echo $a
47
1: / * %
2 : + - 48
53
53
54
55
$ echo $*
Tuesday Feb 18 11:55:59 IST 2014
$ echo $# : 6
$ echo $1 : Tuesday
$ echo $4 : 11:55:59
56
Numeric test
String test
File test
59
60
Output: True : $?=0; False : $?=1
Unix Shell Programming - Forouzan 60
STRING COMPARISON
OPERATORS MEANING
TEST MEANING
OPERATORS TYPE
64
OPERATORS TYPE
! Logical NOT
-lt, -gt, -le, -ge, -eq, - Relational
ne
-a Logical AND
-o Logical OR
65