65
Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Introduction to Linux Shell Script Programming

Summer course, Institute of Bioinformatics

National Yang-Ming University

Page 2: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Menu Today!

Part I: Shell Script in Practice Basics about Shell and Exercises of system Shell Scripts Perl Shell Script

Part II: The applications of Shell Script Massive routing jobs Scheduling Backup

Page 3: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Shell !?

This?

This?

or this?

Page 4: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

First glance — Linux Shell

Why Shell? Computer only realize the command in binary form

which is difficult for most of human So OS provides a special program call ‘shell’ accepts

human’s command in ‘readable’ form and translates them into 1 and 0 stream

Your commands

Linux shell Converted binary commands

OS kernel

$ ls$ man$ date

BASH000100010101001011000011100110100

Linux kernel

Page 5: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Text Shells

Page 6: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Kernel

Definition It is heart of Linux OS It manages all resources of OS

What it charges I/O (Input and Output) Process Devices File Memory

Page 7: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

What is Shell?

Shell is an command language interpreter that executes commands read from the standard input device (your keyboard) or from a file

In Linux OS, it may use one of the following most popular shells (BASH, CSH and KSH)

In Microsoft DOS, the name of shell is COMMAND.COM, but it is NOT as powerful as Linux shell

Page 8: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Operating System Shell

Shell of an operating system it is a program that presents an interface to

various operating system functions and services

Why named “shell”? it is an outer layer of interface between the user

and the innards of the OS (kernel)

Page 9: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Shell, an interface

Main categories CLI (Command Line Interface)

it makes things clear text shell (what we are going to learn now)

GUI (Graphical Use interface) it makes things look easygraphic shell (what people always use nowadays)

Page 10: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

CLI

Unix shells Bourne shell (sh)

Almquist shell (ash) Bourne-Again shell (bash)

C shell (csh) TENEX C shell (tcsh)

Korn shell (ksh) Scheme shell (scsh) Z shell (zsh)

Plan 9 and Unix rc shell (rc)

DOS: command.com

OS/2 and windows NT: cmd.exe

DOS, OS/2 and NT 4DOS, 4OS2, 4NT

Page 11: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

GUI

MS windows windows explorer litestep Geoshell BB4Win Emerge Desktop

Mac OS: Machitosh Finder

X-window system (Unix) KDE, GNOME Blackbox, CDE

DOSSHELL

KDEGNOME

MS Mac

DOSSHELL

Page 12: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Popular Linux Shells

Shell NameDeveloped by …

RemarkWho Where

*BASH

(Bourne-Again SHell)Brian Fox and Chet Ramey

Free Software Foundation

Most common shell in Linux. It is Freeware shell and usually be the default shell.

CSH (C Shell) Bill JoyUniversity of California (Fro

BSD)

The C shell’s syntax and usage are very similar to the C programming language

KSH (Korn Shell)David G.

KornAT & T Bell Labs

claim that it has combined all advantages of 2 shells above

Page 13: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Bell Labs: The beginning

The research and development center of Lucent Technologies, formerly AT&T.

Bell labs is one of the most renowned scientific laboratories in the world

Alexander Graham Bell (1847~1922)

Alexander Graham Bell founds the company that becomes AT&T with 2 financial backers at 1876. He is also the inventor of the telephone.

His famous sentence “Mr. Watson. Come here! I want you!” were the first words to travel over a wire, ringing in the birth of electronic communication

Page 14: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Bell Labs: Brief

Its official name is Bell Telephone Laboratories or Bell Labs which was originally the research and development arm of the United states Bell System, and it was also the premier corporate facility of its type, developing a range of revolutionary technologies from telephone switches to specialized coverings for telephone cables, to transistor.

The work done by Bell Labs are Research

theoretical underpinnings for communications it includes math, physics, material science, behavioral sciences, computer

programming System engineering

concerning itself with conceiving the highly complex systems that make up the telecommunication networks

Development hardware and software for Bell System’s communication networks

Page 15: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Bell Labs: big events

1933, discovered radio waves emitted from center of galaxy

1947, invention of transistor (Nobel Prize in Physics in 1956)

1948, Claude Shannon published “A Mathematical Theory of Communication”

1954, the development of photovoltaic cell

1957, electronic music by Max Mathews

1970s, Unix and C language

1971, computerized switching system for telephone traffic

1980, the realization of the world’s first single chip 32-bit microprocessor, the BELLMAC-32A

1980, C++ language

late 1980s and early 1990s, developed Plan9 as a replacement for Unix

1990s, inferno OS

Page 16: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Variables in Linux

In the machine you are using, it has memory for storing your data. These memory are divided into smaller locations (address) and one can give them a name called memory variable or variable

There are 2 kinds of variables System variables

created and maintained by O.S. itself, all their name are capital. User-defined variable (UDV)

created and maintained by user, all their name are lower-case.

Check variables set: check all variables env: check system variables only

Page 17: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

The shell you use now

What is your current shell? grep username /etc/passwd echo $SHELL chsh (you can change the default shell here)

How many shell you can use cat /etc/shells

How to change current shell temporarily? just type the name of new shell

Page 18: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Script Components

designator line Tell the O.S. where is the correct interpreter it begins with #!

comments it begins with #

shell commands correct separator: semicolon (;) or new line (\n)

Page 19: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Tea Time

Take a break!

Review what you have learned

Page 20: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Start Your First Shell Script

Page 21: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Your first Bash shell script: How are you doing?

locate the shell you use, start with #!

comments, start with #

set variables (bash style)

output the results

Page 22: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Discussion:the difference between apostrophes and quotation marks

Page 23: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Your 2nd Bash shell script: some arithmetic computations

locate the shell you use, start with #!

comments, start with #

No variable declaration

Declare as Integers

Page 24: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Discussion: what is the difference?

Page 25: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Declare

Syntax: declare [-afir] variable[=value]

Options: -a: declared as array -f: declared as function -i: declared as integer -r: declared as read-only variable

Page 26: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Interactive shell script

Page 27: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Multiple Arguments:How to access?

Page 28: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Logical operators

operator meaning= -eq equal

!= -ne Not equal

< -lt Less than

> -gl Greater than

-le Less than or equal (<=)

-ge Greater than or equal (>=)

-a AND

-o OR

arithmetic only, not for text Both arithmetic and text

Page 29: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Flow control

Branches If then, else Case

Loops (for, while, until) it is a block of code that iterates a list of

commands as long as the loop control condition is true

Page 30: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Branches

Page 31: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

(1) If then, else

Syntax and example: If [condition 1]; then [statement 1] elif [condition 2]; then [statement 2] else [statement 3] fi

cond 2

cond 1

Start

stat 1stat 2stat 3

then

then

else

else

Page 32: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Simple form

yn=y

script is runningSTOP!

thenelse

User enter … (stored as yn)

Page 33: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Complex conditions

yn=y || yn=Y

script is runningSTOP!

thenelse

User enter … (stored as yn)

Page 34: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

else if

Page 35: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Check your file

Page 36: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

logic tags for file identification

tag stands for …

-e whether it exists or not

-f whether file exists or not

-d whether directory exists or not

-L whether file is link file

-r whether it can be read

-w whether it can be written

-x whether it can be executed

-s whether file is empty (true is not empty)

Page 37: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Check and Create file

check properties while it exists

ask user whether wanna create it while it does not exist

Page 38: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Remark: if then, else statements

You can link several conditions by || or && Each condition must be between [ ]

Caution! the space between condition statement and [ or ] is necessary

Page 39: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Advanced Example

Analyze your host ~

Page 40: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

(2) Case

syntax and example case variable in

value_1) [statements_1] ;;value_2) [statements_2] ;;value_3) [statements_3] ;;*) [statements_for_exception]

esac

Page 41: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Fortune Teller

Page 42: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Loops

Page 43: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

for loop

syntax: for (( initial value; stop criteria; increment/decrement ))

do [statements] done

simple test The sum from 1 to 100

Page 44: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Do what gauss has doneby shell script

Page 45: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

while loop and until loop

syntax while [ condition ]

do [statement] done

syntax until [ condition ]

do [statements] done

Page 46: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Rewritten in while loop

Page 47: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Rewritten in until loop

Page 48: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Compare while and until

while it holds, continuewhile it reaches, stop

><=

Page 49: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

for loop, revisit

alternative syntax for variable in variable_list

do [statement] done

it differs significantly from its C counterpart

Page 50: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Solar system

Page 51: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

How you login?

HOSTSSH/Telnet

enter username and password

check 1

User

read UID amd GIDread setting of home directory and shell

found

search the username you entered in /etc/passwd

check 2

check the password you entered in /etc/shadow

Login successfully

Login failed

pass

NOT found

NOT pass

Page 52: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

inside /etc/passwd

Notes for each field password has been moved to /etc/shadow UID (32bits)

0 (administrator), 1~500 (system UID), rest for others

Page 53: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

/etc/passwd and cut command

cut function: select portions of each lines of a file syntax: cut –f list [-d delim] [file] example: cut –f 1 –d : myfile the example above uses “:” to separate each line of myfile into

fields, then select the first field for each line

part of /etc/passwd of Mandrake 9.0

Page 54: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Print all accounts in your host

Page 55: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Summary: for, while and until

For loop: for (( triple condition))

do [statements] done

While while [ condition ]

do [statement] done

Until until [ condition ]

do [statements] done

alternative For loop for variable in variable_list

do [statement] done

Page 56: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

select

syntax: select variable [in list]

do [statements] break … it make script stop after selection

done

Note: if in list is not given, it will read list from arguments (shown in ex15.sh) compare to alternative for loop

Page 57: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

create Menu using select

Page 58: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Function

syntax function function_name {statements} function function_name () {statements}

Page 59: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Create menu with function

omitted list here, it will be read from arguments

call function here, call by name

Page 60: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

File I/O

#----- The file to read myFile="/root/somefile"

#----- The "big" data variable myData=""

#----- Now the read myData=`cat $myFile`

#----- Show that the data is really in the variable...#----- This is in the same format as the original file, new lines preserved echo "$myData"

#----- Show the data in non-quoted format, the space becomes the separator echo $myData

Page 61: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Read line-by-line

#----- The file to read myFile="/root/somefile"

#----- The line data variable myLine=""

#----- Loop to read file data content while [ 1 ] do read myLine || break echo "$myLine"

done < $myFile

Page 62: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

Write column-by-column

#----- Set up the path and name of file myFile="/root/temp.txt"

#----- Load data string, the separator here is the colon (:) myData="one:two:three"

#----- Here is the write... echo "$myData" | tr ':' '\n' > $myFile

Page 63: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

How to debug?

please man your shell first man bash man sh

syntax check bash --debugger script_name sh –n script_name

Page 64: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

References

Advanced Bash Scripting Guide http://www.tldp.org/LDP/abs/html/

Bash Reference Manual http://www.gnu.org/software/bash/manual/bashref.html

Learn from examples!

Page 65: Introduction to Linux Shell Script Programming Summer course, Institute of Bioinformatics National Yang-Ming University

The End