1. Trang chủ
  2. » Giáo Dục - Đào Tạo

036 exit statuses and return codes kho tài liệu training

15 93 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 15
Dung lượng 89,02 KB

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

Nội dung

What You Will Learn● How to check the exit status of a command.. ● How to make decisions based on the status.. ● How to use exit statuses in your own scripts... Checking the Exit Status●

Trang 1

Exit Status

Trang 2

What You Will Learn

● How to check the exit status of a command

● How to make decisions based on the status

● How to use exit statuses in your own scripts

Trang 3

Exit Status / Return Code

● Every command returns an exit status

● Range from 0 to 255

● 0 = success

● Other than 0 = error condition

● Use for error checking

Use man or info to find meaning of exit status

Trang 4

Checking the Exit Status

● $? contains the return code of the previously

executed command

ls /not/here

echo "$?"

Trang 5

ping -c 1 $HOST

if [ "$?" -eq "0" ]

then

echo "$HOST reachable."

else

Trang 6

ping -c 1 $HOST

if [ "$?" -ne "0" ]

then

echo "$HOST unreachable."

Trang 7

ping -c 1 $HOST

RETURN_CODE=$?

if [ "$RETURN_CODE" -ne "0" ]

then

echo "$HOST unreachable."

Trang 8

&& and ||

● && = AND

mkdir /tmp/bak && cp test.txt /tmp/bak/

● || = OR

cp test.txt /tmp/bak/ || cp test.txt /tmp

Trang 9

HOST="google.com"

ping -c 1 $HOST && echo "$HOST reachable."

Trang 10

HOST="google.com"

ping -c 1 $HOST || echo "$HOST unreachable."

Trang 11

The semicolon

● Separate commands with a semicolon to

ensure they all get executed

cp test.txt /tmp/bak/ ; cp test.txt /tmp

# Same as:

Trang 12

Exit Command

● Explicitly define the return code

○ exit 0

○ exit 1

○ exit 2

○ exit 255

○ etc

● The default value is that of the last command

Trang 13

HOST="google.com"

ping -c 1 $HOST

if [ "$?" -ne "0" ]

then

echo "$HOST unreachable."

exit 1

Trang 14

● Other than 0 = error condition

● $? contains the exit status

Trang 15

Exit Status

DEMO

Ngày đăng: 17/11/2019, 08:18

TỪ KHÓA LIÊN QUAN