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

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

15 92 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

Nội dung

Exit Status LinuxTrainingAcademy.com 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 LinuxTrainingAcademy.com Exit Status / Return Code ● ● ● ● ● ● Every command returns an exit status Range from to 255 = success Other than = error condition Use for error checking Use man or info to find meaning of exit status LinuxTrainingAcademy.com Checking the Exit Status ● $? contains the return code of the previously executed command ls /not/here echo "$?" Output: LinuxTrainingAcademy.com HOST="google.com" ping -c $HOST if [ "$?" -eq "0" ] then echo "$HOST reachable." else echo "$HOST unreachable." fi LinuxTrainingAcademy.com HOST="google.com" ping -c $HOST if [ "$?" -ne "0" ] then echo "$HOST unreachable." fi LinuxTrainingAcademy.com HOST="google.com" ping -c $HOST RETURN_CODE=$? if [ "$RETURN_CODE" -ne "0" ] then echo "$HOST unreachable." fi LinuxTrainingAcademy.com && and || ● && = AND mkdir /tmp/bak && cp test.txt /tmp/bak/ ● || = OR cp test.txt /tmp/bak/ || cp test.txt /tmp LinuxTrainingAcademy.com #!/bin/bash HOST="google.com" ping -c $HOST && echo "$HOST reachable." LinuxTrainingAcademy.com #!/bin/bash HOST="google.com" ping -c $HOST || echo "$HOST unreachable." LinuxTrainingAcademy.com The semicolon ● Separate commands with a semicolon to ensure they all get executed cp test.txt /tmp/bak/ ; cp test.txt /tmp # Same as: cp test.txt /tmp/bak/ cp test.txt /tmp LinuxTrainingAcademy.com Exit Command ● Explicitly define the return code ○ ○ ○ ○ ○ ● exit exit exit exit 255 etc The default value is that of the last command executed LinuxTrainingAcademy.com #!/bin/bash HOST="google.com" ping -c $HOST if [ "$?" -ne "0" ] then echo "$HOST unreachable." exit fi exit LinuxTrainingAcademy.com Summary ● ● ● ● ● ● ● All command return an exit status - 255 = success Other than = error condition $? contains the exit status Decision making - if, &&,|| exit LinuxTrainingAcademy.com Exit Status DEMO LinuxTrainingAcademy.com ... /tmp LinuxTrainingAcademy.com Exit Command ● Explicitly define the return code ○ ○ ○ ○ ○ ● exit exit exit exit 255 etc The default value is that of the last command executed LinuxTrainingAcademy.com... the exit status of a command How to make decisions based on the status How to use exit statuses in your own scripts LinuxTrainingAcademy.com Exit Status / Return Code ● ● ● ● ● ● Every command returns... "$HOST unreachable." exit fi exit LinuxTrainingAcademy.com Summary ● ● ● ● ● ● ● All command return an exit status - 255 = success Other than = error condition $? contains the exit status Decision

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

TỪ KHÓA LIÊN QUAN