1. Trang chủ
  2. » Công Nghệ Thông Tin

linux crash course chapter 10 3

33 84 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 33
Dung lượng 581 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Control structure tests• Control structures depend on a test that equates either true or false • The test builtin in bash allows logical, relational, and file property-based tests • Synt

Trang 1

Chapter 10:

BASH Shell Scripting

Fun with fi

Trang 2

In this chapter …

• Control structures

• File descriptors

• Variables

Trang 3

Control structure tests

• Control structures depend on a test that equates either true or false

• The test builtin in bash allows logical,

relational, and file property-based tests

• Syntax:

test expression OR [ expression ]

Trang 4

test expressions

• If expression is true, test returns 0; if false, it

returns not 0 (usually 1)

• Comparing text strings

string1 = string2

string1 != string2

• Comparing numbers

num1 –OP num2

– Where OP can be eq, ne, lt, gt, le, ge

Trang 5

test expressions con’t

f : file is a regular file

Plus many more (check man bash)

Trang 6

• [[expression]] can be used for logical

expressions and string comparisons

• See pages 505-506 for complete list

Trang 7

• test-command must evaluate true or false

• commands can be zero or more lines

Trang 8

if … then … else structure

Trang 9

if … then … elif structure

Trang 10

if … then … elif con’t

• You can have one or more elif blocks

• Remember, each elif line is following by a then statement

• Rather than multiple elif’s, might try a case statement instead

Trang 12

case structure con’t

• test-string is any string – usually we want to

check the contents of a variable, so we’d use

something like $myvar

• The patterns are similar to ambiguous file

references – so the shell special characters apply ([ ], ?, *, |)

• If the last pattern is *, it’s a catch all or

default

Trang 13

• loop-index is a variable name – does not

have to be previously declared

• argument-list is a space-delimited list

Trang 17

test-break and continue

• break exits a loop structure – jumps down to after done statement

• continue exits current loop iteration – jumps down to the done statement, and begins next loop iteration test

• Used to short circuit loops

Trang 19

select structure con’t

• select structure displays a numbered menu

allowing user to select an arg

• After displaying the menu, select displays the PS3 prompt – by default it’s #?

• Set PS3 to customize the prompt to

something more intelligible

• The user’s selection (what was actually

typed) is stored in REPLY

Trang 20

File descriptors

• Recall 0<, 1>, 2> … now let’s make more

• Syntax:

exec n> outfile AND exec m< infile

• exec associates streams with files

• Then can treat those streams just like the standard ones

• To close:

exec n>&- AND exec

Trang 21

• Array is zero based and referenced via [ ]

• [*] returns all the elements in the array, IFS delimited

• [@] returns all the elements in the array, for the purpose of copying entire arrays

Trang 22

Variable Scope

• By default, bash variables have local scope

• To make global, you must use export (or

declare/typeset with –x)

• Variables used in a shell script are local to the script, unless exported

Trang 23

Special Parameters

• $$ the PID of the process running

• $? the exit status of the last process

• $# the number of command line arguments

• $0 the name of the calling program

• $n the nth command line argument

– ${n} must be used for n > 9

– the shift builtin rotates through the arguments

Trang 24

Null and unset variables

• ${varname:-default} : if varname is not set or

is null, substitutes for default

• ${varname:=default} : if varname is not set or null, substitues for default and sets varname

• ${varname:?message} : if varname is not

set, displays an error

Trang 25

• Note on scope – functions have same scope

as calling script/shell … which means you

can access (or step on!) existing variables

Trang 27

• Provides info about a command/builtin

• Syntax: type command

• Basically, what is being run?

– Path to executable

– Shell builtin

– Shell alias

– Hashed reference

Trang 28

• Syntax:

read [options] [varname]

• Reads input from standard in

• If varname not supplied, input goes in

REPLY

• Gets everything the user types in before hitting RETURN

Trang 29

– n num – read n characters

– p prompt – displays prompt to user

– u number – grabs from given file descriptor

Trang 30

– Options followed by : denote required args

– If optstring starts with : getopts handles errors – varname used to hold each argument

Trang 31

getopts con’t

• Usually placed in a loop to read options in

one at a time for processing

• Keyword variable OPTIND contains an index

of what option you’re processing

• Keyword variable OPTARG contains the

argument for the given option

Trang 32

:) display error for missing arg ;;

\?) display error for wrong opt ;;

esac done

Ngày đăng: 06/02/2018, 09:55

TỪ KHÓA LIÊN QUAN