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

Module Linux essentials - Module 9: Basic scripting

18 51 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 18
Dung lượng 69,84 KB

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

Nội dung

Module Linux essentials - Module 9 introduce basic scripting. This chapter will guide how to turn commands into a script. After studying this chapter you should be able to: Basic text editing, basic shell scripting. Inviting you to refer.

Trang 1

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Module 9 Basic Scripting

Trang 2

Exam Objective 3.3 Turning Commands into a

Script

Objective Summary

Trang 3

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Text Editors

Trang 4

A script is a sequence of

commands

• If you can type it on the command line, it can be scripted

• You can branch based on tests and loop over collections

• This is all available from the command

line, too

• Scripts are repeatable and consistent and free you to do more interesting work

Trang 5

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Editors

• Scripts are text files

• LibreOffice will add formatting, making it unusable

• nano and vi / vim are popular editors

• Of the two, nano is far easier to use

Trang 6

• Type as normal, use arrow keys to move around

• Most commands are Control + another

character

• Context sensitive commands are at the

bottom of the screen, e.g ^X to exit, ^W to find

• ^G gets you help anywhere

Trang 7

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

shebang

Trang 8

• #! is the shebang (hash + bang)

• Tells the kernel what shell interpreter to use to run the script

• Must be on the first line

• #!/bin/sh

• #!/usr/bin/ruby

• Lets you run the script with /script no matter which shell the user has

Trang 9

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Working with Variables

Trang 10

• Temporary storage of data in memory

• Assigning (no $):

• Using (need a $):

Trang 11

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Special Variables

• $1 $9 are the arguments to the script

• /test.sh hello there # $1=“hello” $2=“there”

• $? is the exit code of the last command to

be run

• use “exit 1” to exit your own script with

error code 1

Trang 12

Working with Conditionals

Trang 13

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Conditionals

if something; then

do this

fi

if something; then

do this

elif something else;

then

do that

else

try this

fi

# something returns

an exit code If it is 0, then “do this” will be executed up until fi

# alternative syntax, allows for multiple tests and a default option if you want

Trang 14

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

tests

test –f /tmp/foo # test if file exists

test ! –f /tmp/foo # test if file doesn’t exist test $A –eq 1 # is $A = 1 (numeric)

test “$B” = “Hello” # string comparison

test $A –lt 10 # $A < 10?

alternatively, [ is the same as test:

if test –f /tmp/foo; then # it works

Trang 15

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

Case

case ”$GREETING" in

hello|hi)

echo "hello yourself"

;;

goodbye)

echo "nice to have met you"

echo "I hope to see you again"

;;

*)

echo "I didn't understand that"

esac

Trang 16

Loops

Trang 17

This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses

©Copyright Network Development Group 2013

The for loop

• Operates over a fixed set of items or a glob

NAMES=“Alice Bob Charlie”

for N in $NAMES; do

echo “Hello $N”

done

for FILE in *; do

ls –l $FILE

done

Trang 18

While loops

• Tests the loop condition each time

while [ ! –f /tmp/foo ]; do

try_to_make_foo # makes /tmp/foo on success

sleep 1 # wait 1 sec

done

• Used when the number of loops is

unknown or changes each loop

Ngày đăng: 30/01/2020, 03:06

TỪ KHÓA LIÊN QUAN