惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
P
Privacy & Cybersecurity Law Blog
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
S
Securelist
N
News | PayPal Newsroom
I
Intezer
H
Hacker News: Front Page
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
A
About on SuperTechFans
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Project Zero
Project Zero
Cloudbric
Cloudbric
N
News and Events Feed by Topic
大猫的无限游戏
大猫的无限游戏
Attack and Defense Labs
Attack and Defense Labs
博客园 - 叶小钗
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
博客园 - Franky
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
I
InfoQ
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
Scott Helme
Scott Helme

博客园 - taizi

Redhat5 网络配置(转) RPM 用法 Beginning Linux Programming----The Standard I/O Library Beginning Linux Programming----Low-level File Access (3) Beginning Linux Programming----Low-level File Access (2) Beginning Linux Programming----Low-level File Access (1) Beginning Linux Programming----Library Functions Beginning Linux Programming----System Calls and Device Drivers 细细品味UNIX的数据流重定向(摘) /dev/null 和/dev/zero(摘) Beginning Linux Programming----Linux File Structure Beginning Linux Programming----shell脚本程序实例 Beginning Linux Programming----dialog Utility Beginning Linux Programming----Here Documents Beginning Linux Programming----Command Execution,Arithmetic Expansion,Parameter Expansion Beginning Linux Programming----Grep Beginning Linux Programming----Find Bash提示小窍门 Beginning Linux Programming----Commands(2)
Beginning Linux Programming----Debugging Scripts
taizi · 2008-03-16 · via 博客园 - taizi

Debugging Scripts

调试shell脚本通常十分容易,除了没有特定的工具来帮助我们调试。在这一部分我们将快速地概括一下调式脚本的基本方法。

当一个错误发生时,通常shell将打印出错误所在行的行号。如果错误不是可以直接就能找到的话,我们可以添加额外的echo程序语句以显示变量的内容,并且可以简单将代码片段输入到交互式的shell中进行测试。

因为脚本运行通常是被解释运行的,因此修改和重试一个脚本并不会有额外的编译开销。追踪更复杂的错误的主要方法就是设置各式各样的shell选项。而要做到这种要求,你可以在调用shell后使用命令行选项,或者使用set指令。下表总结了所有的选项:

Command Line Optionset OptionDescription
sh -n <script>set -o noexec
set -n
只检查语法错误而不运行指令
sh -v <script>set -o verbose
set -v
在运行命令前先显示命令
sh -x <script>set -o xtrace
set -x
在命令运行之后再显示命令
sh -u <script>set -o nounset
set -u
当一个未定义的变量被使用时给出错误信息

我们可以设置set选项,-o表示打开,+o表示关闭,同样对缩写版本也是一样。我们可以使用xtrace选项进行简单的跟踪执行。作为最初的检查,我们可以使用命令行选项,但要做精细的调式,我们就需要在脚本中有问题的代码周围设置xtrace标志(设置跟踪状态运行与否)。执行跟踪将促使Shell在执行带有变量扩展的Shell脚本中的每一行之前将之打印出来。

使用下面的命令来设置xtrace为开:

set -o xtrace

而下面的命令则设置xtrace为关:

set +o xtrace

扩展的级别是通过在每一行开始的“+signs”的数量来表示的(默认情况下)。所以我们可以将+改为一些更有意义的内容,方法是在我们的Shell配置文件中设置Shell变量PS4的值。

在Shell中,我们还会发现我们可以在脚本的开始像下面这样设置来跟踪EXIT信号以发现程序是否退出:

trap 'echo Exiting: critical variable = $critical_variable' EXIT