+ All Categories
Home > Technology > Shell Script Tutorial

Shell Script Tutorial

Date post: 28-May-2015
Category:
Upload: quang-minh-doan
View: 194 times
Download: 0 times
Share this document with a friend
Description:
Shell Script Tutorial from CTIVN
Popular Tags:
31
SHELL SCRIPT TUTORIAL Doan Quang Minh June 21, 2012 CTI Cosatech V
Transcript
Page 1: Shell Script Tutorial

SHELL SCRIPT TUTORIALDoan Quang MinhJune 21,

2012 CTI Cosatech VN

Page 2: Shell Script Tutorial

2

About this slide

//

Shell SCRIPT Tutorial

Page 3: Shell Script Tutorial

3

Agenda

I. Introduction (5 minutes)

II. Basic syntax (15 minutes)

III. Advanced syntax (25 minutes)

IV. Conclusion (2 minutes)

V. Reference (2 minutes)

VI. Q&A

Shell SCRIPT Tutorial

Page 4: Shell Script Tutorial

4

I. Introduction

//

Shell SCRIPT Tutorial

Page 5: Shell Script Tutorial

5

II. Basic syntax

1. Principle of Script2. Variables3. Branching4. Looping5. Command line Arguments6. Comparisons7. Variable Manipulations

Shell SCRIPT Tutorial

Page 6: Shell Script Tutorial

6

1. Principle of Script

Defining the Shell Type#!/usr/bin/ksh

Four Types of Lines#!/usr/bin/ksh

# Commentary......

file=/path/file

if [[ $file = $1 ]];then

command

fi

II. Basic syntaxShell SCRIPT Tutorial

Page 7: Shell Script Tutorial

7

1. Principle of Script(cont)

Start and End of ScriptThe script starts at the first line and ends either when it encounters an "exit" or the last line. All "#" lines are ignored.

Start and End of Command A command starts with the first word on a line or if

it's the second command on a line with the first word after a";'.

A command ends either at the end of the line or whith a ";". So one can put several commands onto one line:

print -n "Name: "; read name; print "“ Continues commands over more than one line:

grep filename | sort -u | awk '{print $4}' | \

uniq -c >> /longpath/fileII. Basic syntax

Shell SCRIPT Tutorial

Page 8: Shell Script Tutorial

8

2. Variables

Filling inWhen filling into a variable then one uses just it's

name: state="US" and no blanks. There is no difference between strings and numbers: price=50.

UsingWhen using a variable one needs to put a $ sign

in front of it: print $state $price.

II. Basic syntaxShell SCRIPT Tutorial

Page 9: Shell Script Tutorial

9

2. Variables(cont)

ArraysSet and use an array like:

arrname[1]=4 To fill in print ${arraname[1]} To print out ${arrname[*]} Get all elements ${#arrname[*]} Get the number of

elements

DeclarationThere are happily no declarations of variables

needed in ksh. One cannot have decimals only integers.

II. Basic syntaxShell SCRIPT Tutorial

Page 10: Shell Script Tutorial

10

3. Branching

“if-then”

if _expr_ then

_cmd(s)_

elif _expr_

_cmd(s)_

else

_cmd(s)_

fi

II. Basic syntax

“case” case _word_ in _pattern1_)

_cmd(s)_ _pattern2_)

_cmd(s)_ *) break ;;esac

Shell SCRIPT Tutorial

Page 11: Shell Script Tutorial

11

4. Looping

"while"while _expr_ do

_cmd(s)_

done

"for"for _variable_ in _list_

_cmd(s)_

done

II. Basic syntax

"until"until _expr_do

_cmd(s)_done

Shell SCRIPT Tutorial

Page 12: Shell Script Tutorial

12

5. Command line Argumentsprogram, function or shell $0

argument 1 through 9 $1 .. $9

nth argument ${n}

number of positional parameters $#

every positional parameter $@, $*

decimal value returned by last executed cmd $?

pid of shell $$

pid of last backgrounded command $!

II. Basic syntaxShell SCRIPT Tutorial

Page 13: Shell Script Tutorial

13

6. Comparisons

To compare strings one uses "=" for equal and "!=" for not equal.

To compare numbers one uses "-eq" for equal "-ne" for not equal as well as "-gt" for greater than and "-lt" for less than.

if [[ $name = "John" ]];then # commands....

fi if [[ $size -eq 1000 ]];then

# commands.... fi

II. Basic syntaxShell SCRIPT Tutorial

Page 14: Shell Script Tutorial

14

6. Comparisons(cont)

With "&&" for "AND" and "||" for "OR" one can combine statements:

if [[ $price -lt 1000 || $name = "Hanna" ]];then # commands....fiif [[ $name = "Fred" && $city = "Denver" ]];then # commands....fi

II. Basic syntaxShell SCRIPT Tutorial

Page 15: Shell Script Tutorial

15

7. Variable Manipulations

Removing something from a variable Variables that contain a path can very easily be stripped

of it: ${name##*/} gives you just the filename. Or if one wants the path: ${name%/*}. % takes it away

from the left and # from the right. %% and ## take the longest possibility while % and #

just take the shortest one.

Replacing a variable if it does not yet exist

If we wanted $foo or if not set 4 then: ${foo:-4} but it still remains unset. To change that we use: ${foo:=4}

II. Basic syntaxShell SCRIPT Tutorial

Page 16: Shell Script Tutorial

16

7. Variable Manipulations(cont)Exiting and stating something if

variable is not setThis is very important if our program relays on a

certain variable: ${foo:?"foo not set!"}

Just check for the variable${foo:+1} gives one if $foo is set, otherwise

nothing.

II. Basic syntaxShell SCRIPT Tutorial

Page 17: Shell Script Tutorial

17

III. Advanced syntax

1. Ksh Regular Expressions

2. Functions

3. Data Redirection

4. Pipes

5. Co-processes

6. Read Input from User and from Files

7. Special Variables

8. Action on Success or Failure of a Command

9. Trivial Calculations

10. Numerical Calculations using "bc"

Shell SCRIPT Tutorial

Page 18: Shell Script Tutorial

18

1. Ksh Regular Expressions

Ksh has it's own regular expressions. Use an * for any string. So to get all the files ending it .c use *.c. A single character is represented with a ?. So all the files starting with any sign followed bye 44.f can be fetched by: ?44.f.

Especially in ksh there are quantifiers for whole patterns:

?(pattern) matches zero or one times the pattern.*(pattern) matches any time the pattern.+(pattern) matches one or more time the pattern.@(pattern) matches one time the pattern.!(pattern) matches string without the pattern.

Question a string in a variable like: if [[ $var = fo@(?4*67).c ]];then ...

III. Advanced syntaxShell SCRIPT Tutorial

Page 19: Shell Script Tutorial

19

2. Functions(cont)

DescriptionA function (= procedure) must be defined before it is

called, because ksh is interpreted at run time. It knows all the variables from the calling shell except the commandline arguments. But has it‘s own command line arguments so that one can call it with different values from different places in the script. It has an exit status but cannot return a value like a c funcition can.

Making a FunctionOne can make one in either of the following two ways:

function foo {

# commands...

}

III. Advanced syntax

foo(){ # commands... }

Shell SCRIPT Tutorial

Page 20: Shell Script Tutorial

20

3. Data Redirection

General

Data redirection is done with the follwoing signs: "> >> < <<". Every program has at least a standardinput, standardoutput and standarderroroutput. All of these can be redirected.

Command Output to File For writing into a new file or for overwriting a file do: command

> file For appending to a file do: command >> file

Standard Error Redirection To redirect the error output of a command do: command 2> file To discard the error alltogether do: command 2>/dev/null To put the error to the same location as the normal output do:

command 2>&1

III. Advanced syntaxShell SCRIPT Tutorial

Page 21: Shell Script Tutorial

21

3. Data Redirection(cont)

File into CommandIf a program needs a file for input over standard input do:

command < file

Combine Input and Output Redirection command < infile > outfile command < infile > outfile 2>/dev/null

Commands into Program ( Here Document )Every unix command can take it's commands from a text like

listing with:

command <<EOFinput1input2input3EOF

From eof to eof all is feeded into the above mentioned command.

III. Advanced syntaxShell SCRIPT Tutorial

Page 22: Shell Script Tutorial

22

4. Pipes

For a serial processing of data from one command to the next do:

command1 | command2 | command3 ...e.g. last | awk '{print $1}' | sort -u.

III. Advanced syntaxShell SCRIPT Tutorial

Page 23: Shell Script Tutorial

23

5. Co-processes

One can have one background process with which one can communicate with read -p and print -p. It is started with command |&.

If one uses: ksh |& then this shell in the background will do everything for us even telnet and so on: print -p "telnet hostname".

III. Advanced syntaxShell SCRIPT Tutorial

Page 24: Shell Script Tutorial

24

6. Read Input from User and Files

Read in a Variable

From a user we read with: read var. Then the users can type something in. One should first print something like: print -n "Enter your favorite haircolor: ";read var; print "". The -n suppresses the newline sign.

Read into a File Line for Line

To get each line of a file into a variable iteratively do:{ while read myline;do

# process $myline

done } < filename

To catch the output of a pipeline each line at a time in a variable use:

last | sort | {

while read myline;do

# commands

done }III. Advanced syntaxShell SCRIPT Tutorial

Page 25: Shell Script Tutorial

25

7. Special Variables

program, function or shell $0

argument 1 through 9 $1 .. $9

nth argument ${n}

number of positional parameters $#

every positional parameter $@, $*

decimal value returned by last executed cmd $?

pid of shell $$

pid of last backgrounded command $!

III. Advanced syntaxShell SCRIPT Tutorial

Page 26: Shell Script Tutorial

26

8. Action (on Success or Failure of a Command)

If one wants to do a thing only if a command succeded then:

command1 && command2.

If the second command has to be performed only if the first one failed, then:

command1 || command2.

III. Advanced syntaxShell SCRIPT Tutorial

Page 27: Shell Script Tutorial

27

9. Trivial Calculations

Simpe calculations are done with either a "let" in front of it or within (( ... )).

One can increment a variable within the (( )) without a "$": (( a+=1 )) or let a+=1.

III. Advanced syntaxShell SCRIPT Tutorial

Page 28: Shell Script Tutorial

28

10. Numerical Calculations using "bc"

For bigger calculations one uses "bc" like:

$result=$(print "n=1;for(i=1;i<8;i++)n=i*n;n"|bc)

III. Advanced syntaxShell SCRIPT Tutorial

Page 29: Shell Script Tutorial

29

IV. Conclusion

Now, I hope this slide gave you some ideas about working with Shell script, especially Korn shell. Please let me know if there’s any mistake or issue in the tutorial. All comments are welcomed.

Page 31: Shell Script Tutorial

31

VI. Q&A

Questions, please ...

Shell SCRIPT Tutorial


Recommended