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

045 debugging more tips kho tài liệu training

18 60 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 137,79 KB

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

Nội dung

Manual Copy and Paste● Open up a second terminal.. ● Copy and paste the commands into the terminal.. ● Can be helpful to use "set -x" on the command line... ● Controls what is displayed

Trang 1

Additional Debugging

Tips and Tricks

Trang 2

● Variables for debugging

● Manual debugging tips

● Syntax Highlighting

● More Bash built-ins

● File types

Trang 3

Manual Debugging

● You can create your own debugging code

● Use a special variable like DEBUG

○ DEBUG=false

Trang 4

if $DEBUG

then

echo "Debug mode ON."

else

echo "Debug mode OFF."

fi

Trang 5

DEBUG=true

$DEBUG && echo "Debug mode ON."

#!/bin/bash

DEBUG=false

$DEBUG || echo "Debug mode OFF."

Trang 6

$DEBUG || echo "Debug mode OFF."

Trang 7

DEBUG="echo"

$DEBUG ls

#!/bin/bash

#DEBUG="echo"

$DEBUG ls

Trang 8

debug() {

echo "Executing: $@"

$@

}

debug ls

Trang 9

Manual Copy and Paste

● Open up a second terminal

● Copy and paste the commands into the

terminal

● Can be helpful to use "set -x" on the command line

Trang 10

● Syntax errors are common.

● Typos, missing brackets, missing quotes, etc

● Use an editor with syntax highlighting

○ vi / vim

○ emacs

○ nano

○ gedit

○ kate

Trang 11

● Controls what is displayed before a line when using the "-x" option

● The default value is "+"

● Bash Variables

○ BASH_SOURCE, LINENO, etc

PS4='+ $BASH_SOURCE : $LINENO '

Trang 12

PS4='+ $BASH_SOURCE : $LINENO : '

TEST_VAR="test"

echo "$TEST_VAR"

+ PS4='+ $BASH_SOURCE : $LINENO : '

+ /test.sh : 3 : TEST_VAR=test

+ /test.sh : 4 : echo test

test

Trang 13

#!/bin/bash -x

PS4='+ ${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}() '

debug() {

echo "Executing: $@"

$@

}

debug ls

Trang 14

● CRLF / Carriage Return, Line Feed

● cat -v script.sh

#!/bin/bash^M

# This file contains carriage returns.^M

echo "Hello world."^M

Trang 15

DOS vs Linux (Unix) File Types

#!/bin/bash^M

# This file contains carriage returns.^M

echo "Hello world."^M

bash: /test.sh: /bin/bash^M: bad

interpreter: No such file or directory

Trang 16

● file script.sh

○ script.sh: Bourne-Again shell script, ASCII text

executable, with CRLF line terminators

● dos2unix script.sh

● file script.sh

○ script.sh: Bourne-Again shell script, ASCII text

executable

Trang 17

How does this happen?

● Using a Windows editor and uploading to

Linux

○ Some editors can be configured to use just LF

● Pasting from Windows into a Linux terminal

● Pasting from a web browser into a terminal

Trang 18

● DEBUG variables

○ cat -v

○ dos2unix

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