Sei sulla pagina 1di 78

Unix Shells & Shell

Scripts

Pankaj Dharmik

2010 Wipro Ltd - Confidential

The purpose of this tutorial is to understand some of the


basics of shell script programming, and to introduce you to
some of the possibilities of simple but powerful
programming using awk and sed available under the korn
shell.

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Agenda
1 Unix Shells and Shell Scripts
2 Writing and Debugging Scripts
3 The Shell Environment
4 Regular Expressions and grep
5 Conditional and Loops

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Unix Shells and Shell Scripts

2010 Wipro Ltd - Confidential

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

The Shell History


The basic shells come in three main language forms. These are (in
order of creation) sh, csh and ksh.
1) Bourne shell ($) - Historically the sh language was the first to be created and

goes under the name of The Bourne Shell. It is the default shell for the typical UNIX
computing environment. It is typically used by system administrators. It has a very
compact syntax which makes it obtuse for novice users but very efficient when used
by experts.

2) C shell (%) Next up was The C Shell (csh), so called because of the similar
syntactical structures to the C language. The UNIX man pages contain almost twice
as much information for the C Shell as the pages for the Bourne shell, leading most
users to believe that it is twice as good. Its a shell based on the C programming
language. It has features such as aliasing and history.
3) Korn shell ($) Lastly we come to The Korne Shell (ksh) made famous by IBM's AIX
flavour of UNIX. The Korne shell can be thought of as a superset of the Bourne shell
as it contains the whole of the Bourne shell world within its own syntax rules. The
korn shell offers compatibility with the Bourne shell with some added features.

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

10

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

11

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Writing and Debugging

12

2010 Wipro Ltd - Confidential

13

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

14

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

15

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

16

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

17

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

18

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

19

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

20

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

21

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

22

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

23

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

24

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

25

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

26

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

27

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

28

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

29

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Exercise

30

grep lines starting with root in /etc/groups


grep lines ending with root in /etc/groups
From /usr/dict/words list all words that
docontaina y or Y,
but:
doNOTstart with a y or Y, and
doNOTend with a y or Y.

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Exercise ---- grep

From /usr/dict/words list all words that


docontaina y or Y,
but:
doNOTstart with a y or Y, and
doNOTend with a y or Y.

31

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Solution
Pipe of grep commands
$ grep "[yY]" /usr/dict/words | grep "^[^yY]" | grep "[^yY]$"
-- or -$ grep -i y /usr/dict/words | grep -i "^[^y]" | grep -i "[^y]$"
-- or -$ grep -i y /usr/dict/words | grep -iv "^y" | grep -iv "y$"
Single grep command
$ grep "^[^yY].*[yY].*[yY]$" /usr/dict/words
-- or -$ grep -i "^[^y].*y.*[^y]$" /usr/dict/words

32

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Examples of Scripts

33

2010 Wipro Ltd - Confidential

Piped Script
#!/bin/csh
setenv PASSWD `cat ~/.passwd`
foreach server_name (`cat hostlist`)
echo "connecting to server $server_name"
(sleep 5;echo $USERID;\
sleep 5;echo $PASSWD;\
sleep 2;echo passwd;\
sleep 2;echo $OLDPASSWD;\
sleep 2;echo $NEWPASSWD;\
sleep 2;echo $NEWPASSWD;\
sleep 2;exit;) | telnet $server_name | tee -a
$server_name.log
end

34

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

RC and Case Statement Scripts


Case Statement Scripts ---- Show script from /etc/init.d/

Getopts Scripts
#!/bin/ksh
PROG_NAME=$(basename $0)
A_FLAG=FALSE
B_FLAG=FALSE
while getopts abl: OPTION
do
case ${OPTION} in
a) A_FLAG=TRUE;;
b) B_FLAG=TRUE;;
l) LOGFILE=${OPTARG};;
\?) print -u2 "Usage: ${PROG_NAME} [ -a -b -l logfile_name ]"
exit 2;;
esac
done

35

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

sed ( stream editor)

36

2010 Wipro Ltd - Confidential

37

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

38

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

39

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

sed print examples


USE data.file

sed
sed
sed
sed

40

3,5p data.file
-n 3,5p data.file
-n /west/p data.file
-n /west/,/southern/p data.file

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

41

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

sed substitution examples


sed s/3/X/ data.file
sed s/3/X/g data.file
sed -n / [0-9]$/p data.file
sed s/ [0-9]$/& Single Digit/ data.file

42

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

43

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

sed delete examples


sed 4,8d data.file
sed /west/d data.file
sed /^west/d data.file
sed /south/,/north/d data.file

44

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

45

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

46

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Exercise (use passwd/hosts file)

47

substitute username pdesh to pdeshpande


substitute all $ with _
change all accounts using ksh to sh
change all user home dirs to /home2/username (e.g.
/home2/pdesh etc.)
change all uids in 300 to 399 range to corresponding 400 to
499 range
delete all hosts which do not belong to network 159.17.x.x
substitute your host name by that name + -intel
delete all blank lines in passwd file
comment all lines in hosts file
add aliases to hosts such as
(for sun1 (and sun2) sun1
sun1-sparc (and sun2-sparc),
for the rest sunxx sunxxintel)
remove the loghost alias in hosts file
replace multiple spaces (and tab(s)) and a single space in

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Exercise (use article.txt)

1. Lines with <article> and </article> should be deleted.


2. Replace <title> with Title:, and replace </title> with nothing.
3. Replace all <para> and </para> tags with the null string. If the
resulting line is empty, delete the line. (You may need to use curly
braces to make this happen.)
4. Replace all <emphasis> and </emphasis> tags with asterisks.
Thus:This is a <emphasis>great</emphasis> bargain.will
becomeThis is a *great* bargain.
5. Replace the word web with Web everywhere.
6. Replace lines starting with <listing> by ---begin listing
7. Replace lines starting with </listing> by ---end listing

48

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Few solutions.

"/article.$/d

"s/title/Title/"

49

"s/<.*para>//

"s/<.*emphasis>/*/

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

AWK
(awk lets you manipulate files that consist of fields separated by delimiters.)

50

2010 Wipro Ltd - Confidential

TheawkProcessing Cycle

1. Read a line from the file into a variable named$0.


2. Split up the fields. Theawkcommand can be invoked with
an option that tells what the delimiter is. If you dont give a
delimiter, then fields are delimited by whitespace.The first
field is placed in variable$1, the second in$2, and so forth
3. Do whatever command or commands are in the
braces{and }

awk '{some command}' <file name>

51

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Examples
awk {print $1, print $2 }' people.txt
awk -F';' '{print $1, "was born", $3 "."}' people.txt
awk -F'[;,-]' '{some command}' people.txt

Use lab3.data file


print the names for all people whose last contribution ended in
the digit 5

52

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

print the names for all people whose last contribution


ended in the digit 5
awk -F :' '/5$/{print $1}' lab3.data

53

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Conditionals

54

2010 Wipro Ltd - Confidential

55

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

56

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

57

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

=>> Write a if then else script in ksh (snoopy.ksh)


=>> Modify the above script to pass the parameter as an
argument

58

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

59

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

60

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

61

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Loops

62

2010 Wipro Ltd - Confidential

63

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

64

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

65

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

66

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

67

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

68

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

69

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Exercise
Write a ksh while script which reads input (string )
from terminal continuously till it finds a string exit
(use combination of while loop and if condition)
(use while read var statement

70

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

#!/bin/ksh
# Script name: readinput.ksh
print -n "Enter a string: "
while read var
do
print "Keyboard input is: $var"
print -n "\nEnter a string: "
if [[ "$var" = "exit" ]]
then
echo "INPUT IS >>>>>>> $var"
break
fi
done
print "End of input."

71

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Internal Field Separator

72

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

73

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

74

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

This can be used to have menu based scripts

75

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Prompt string in select loops

76

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

menu.ksh

77

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Thanks !!!

78

2010
2010 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential

Potrebbero piacerti anche